Jarves\Filesystem\WebFilesystem::paste PHP Method

paste() public method

If the source is a folder, it copies recursively.
public paste ( array | string $source, string $target, string $action = 'move' ) : boolean
$source array | string
$target string
$action string move|copy
return boolean
    public function paste($source, $target, $action = 'move')
    {
        $files = (array) $source;
        $action = strtolower($action);
        $result = false;
        foreach ($files as $file) {
            $oldFile = str_replace('..', '', $file);
            $oldFile = str_replace(chr(0), '', $oldFile);
            $oldFs = $this->getAdapter($oldFile);
            //if the $target is a folder with trailing slash, we move/copy the files _into_ it otherwise we replace.
            $newPath = '/' === substr($target, -1) ? $target . basename($file) : $target;
            $newFs = $this->getAdapter($newPath);
            $file = null;
            if ($newFs === $oldFs) {
                $file = $this->getFile($oldFile);
                $result = $newFs->{$action}($this->normalizePath($oldFile), $this->normalizePath($newPath));
            } else {
                //we need to move a folder from one file layer to another.
                $file = $oldFs->getFile($this->normalizePath($oldFile));
                if ($file->getType() == 'file') {
                    $content = $oldFs->read($this->normalizePath($oldFile));
                    $newFs->write($this->normalizePath($newPath), $content);
                } else {
                    if ('/' === $oldFs->getMountPath()) {
                        /** @var Local $oldFs */
                        //just directly upload the stuff
                        $this->copyFolder($oldFs->getRoot() . $oldFile, $newPath);
                    } else {
                        //we need to copy all files down to our local hdd temporarily
                        //and upload then
                        $folder = $this->downloadFolder($oldFile);
                        $this->copyFolder($folder, $newPath);
                        $this->cacheFilesystem->delete($folder);
                    }
                }
                if ('move' === $action) {
                    $oldFs->delete($this->normalizePath($oldFile));
                    if ($this) {
                        $file->setPath($this->normalizePath($newPath));
                        $file = $this->wrap($file);
                        $file->save();
                    }
                }
                $result = true;
            }
            if ('move' === $action) {
                $file = $this->wrap($file);
                $file->setPath($this->normalizePath($newPath));
                $file->save();
            }
        }
        return $result;
    }

Usage Example

コード例 #1
0
ファイル: FileController.php プロジェクト: jarves/jarves
 /**
  * @ApiDoc(
  *  section="File Manager",
  *  description="Moves or copies files in /web to $target in /web"
  * )
  *
  * @Rest\RequestParam(name="files", requirements=".*", map=true, strict=true, description="The file paths")
  * @Rest\RequestParam(name="target", requirements=".*", strict=true, description="The target file path")
  * @Rest\RequestParam(name="overwrite", requirements=".*", strict=true, default="false", description="If the target should be overwritten")
  * @Rest\RequestParam(name="move", requirements=".*", strict=true, default="false", description="If files should be moved (cut&paste) or copied (copy&paste)")
  *
  * @Rest\Post("/admin/file/paste")
  *
  * @param ParamFetcher $paramFetcher
  *
  * @return array|bool returns [targetExists => true] when a target exists and $overwrite=false, otherwise true/false.
  */
 public function pasteAction(ParamFetcher $paramFetcher)
 {
     $files = $paramFetcher->get('files');
     $target = $paramFetcher->get('target');
     $overwrite = filter_var($paramFetcher->get('overwrite'), FILTER_VALIDATE_BOOLEAN);
     $move = filter_var($paramFetcher->get('move'), FILTER_VALIDATE_BOOLEAN);
     $this->checkAccess($target);
     foreach ($files as $file) {
         $this->checkAccess($file);
         $newPath = $target . '/' . basename($file);
         if (!$overwrite && $this->webFilesystem->has($newPath)) {
             return ['targetExists' => true];
         }
         $this->newFeed($file, $move ? 'moved' : 'copied', sprintf('from %s to %s', $file, $newPath));
     }
     return $this->webFilesystem->paste($files, $target, $move ? 'move' : 'copy');
 }