Exemple #1
0
 /**
  * {@inheritDoc}
  */
 public function move($pk, $targetPk, $position = 'first', $targetObjectKey = null, $overwrite = false)
 {
     if ($pk) {
         $path = $this->getPathFromPK($pk);
     } else {
         $path = '/';
     }
     $target = is_numeric($targetPk['id']) ? $this->webFilesystem->getPath($targetPk['id']) : $targetPk['id'];
     $target = $target . '/' . basename($path);
     if (!$overwrite && $this->webFilesystem->has($target)) {
         return ['targetExists' => true];
     }
     $this->checkAccess($path);
     $this->checkAccess($target);
     return $this->webFilesystem->move($path, $target);
 }
Exemple #2
0
 /**
  * @ApiDoc(
  *  section="File Manager",
  *  description="Uploads a file to $path with $name as name"
  * )
  *
  * @Rest\RequestParam(name="path", requirements=".+", strict=true, description="The target path")
  * @Rest\RequestParam(name="name", requirements=".*", strict=false, description="The file name if you want a different")
  * @ #Rest\RequestParam(name="overwrite", requirements=".*", default="false", description="If the target should be overwritten")
  * @Rest\RequestParam(name="file", strict=false, description="The file")
  *
  * @Rest\Post("/admin/file/upload")
  *
  * @param Request $request
  * @param ParamFetcher $paramFetcher
  *
  * @return string
  * @throws FileUploadException
  * @throws InvalidArgumentException
  * @throws AccessDeniedException
  */
 public function doUploadAction(Request $request, ParamFetcher $paramFetcher)
 {
     $path = $paramFetcher->get('path');
     $overwriteName = $paramFetcher->get('name');
     //        $overwrite = filter_var($paramFetcher->get('overwrite'), FILTER_VALIDATE_BOOLEAN);
     /** @var $file UploadedFile */
     $file = $request->files->get('file');
     if (null == $file) {
         throw new InvalidArgumentException("There is no file uploaded.");
     }
     $name = $file->getClientOriginalName();
     if ($overwriteName) {
         $name = $overwriteName;
     }
     if ($file->getError()) {
         $error = sprintf('Failed to upload the file %s to %s. Error: %s', $name, $path, $file->getErrorMessage());
         throw new FileUploadException($error);
     }
     $newPath = $path == '/' ? '/' . $name : $path . '/' . $name;
     if ($this->webFilesystem->has($newPath)) {
         //            if (!$overwrite) {
         if ($this->webFilesystem->has($newPath)) {
             $content = $this->webFilesystem->read($newPath);
             $check = "file-is-being-uploaded-by-" . hash('sha512', $this->pageStack->getSession()->getId());
             if ($content != $check) {
                 //not our file, so cancel
                 throw new FileUploadException(sprintf('The target file is currently being uploaded by someone else.'));
             }
         } else {
             throw new FileUploadException(sprintf('The target file has not be initialized.'));
         }
         //            }
     }
     $fileToAdd = ['path' => $path];
     $aclRequest = ACLRequest::create('jarves/file')->setPrimaryObjectItem($fileToAdd)->onlyUpdateMode();
     if (!$this->acl->check($aclRequest)) {
         throw new AccessDeniedException(sprintf('No access to file `%s`', $path));
     }
     $content = file_get_contents($file->getPathname());
     $result = $this->webFilesystem->write($newPath, $content);
     @unlink($file->getPathname());
     if ($result) {
         $this->newFeed($newPath, 'uploaded', 'to ' . $newPath);
     }
     return $newPath;
 }