コード例 #1
0
 /**
  * Removes the associated file from the persistent storage.
  *
  * @param LifecycleEventArgs $args
  *
  * @throws FileDeleteFailedException
  */
 public function preRemove(LifecycleEventArgs $args)
 {
     $entity = $args->getEntity();
     if ($entity instanceof File) {
         try {
             $fileStorage = $this->configuration->createFileStorage($args->getEntityManager());
             /*
              * @important be error tolerant even if physical file cannot be removed for whatever reason
              *
              * Returns false if file cannot be deleted but does not specify any reason. Unless an exception
              * is thrown, remove operation is assumed OK.
              */
             $fileStorage->remove($entity);
         } catch (\Exception $e) {
             throw new FileDeleteFailedException(sprintf('Cannot delete file \'%s\' with key %s. %s', $entity->getOrigFilename(), $entity->getKey(), $e));
         }
     }
 }
コード例 #2
0
 /**
  * @param Request $request
  * @param string  $key file key
  *
  * @return $this
  */
 public function handle(Request $request, $key)
 {
     // get request param download if specified
     $forceDownload = $request->get('download');
     $this->forceDownload = $forceDownload === 1 || $forceDownload === 'true' ?: false;
     // unless otherwise set, this handler will trigger a 404 file not found exception
     $this->setResponseCode(404);
     $this->responseMessage[] = sprintf('The requested resource \'%s\' was not found on this server.', $this->filename);
     // try to find the requested File entity
     $this->file = $file = $this->entityManager->getRepository('ReskumeFileBundle:File')->findOneBy(array('key' => $key));
     if ($file instanceof File) {
         $fileStorage = $this->configuration->createFileStorage($this->entityManager);
         if ($this->fileContent = $fileStorage->getContent($file)) {
             $this->setResponseCode(200);
             $this->responseMessage = array();
         }
     }
     return $this;
 }
コード例 #3
0
 /**
  * Move an uploaded files to destination directory.
  *
  * @param UploadedFile $file
  *
  * @throws MimeTypeNotAllowedException
  * @throws FileMoveFailedException
  *
  * @return $this
  */
 private function moveFile(UploadedFile $file)
 {
     $uploadDir = $this->configuration->getTmpUploadDir();
     if (!$this->configuration->isAllowedMimeType($file) === true) {
         $this->setResponseDetails(400, 'MIME type not allowed.');
         throw new MimeTypeNotAllowedException(sprintf('The uploaded file\'s \'%s\' MIME type \'%s\' is not allowed.', $file->getClientOriginalName(), $file->getMimeType()));
     }
     try {
         $origFilename = $file->getClientOriginalName();
         // preserver original client filename name
         $tmpFile = $file->move($uploadDir);
         $movedUploadedFile = new UploadedFile($tmpFile->getPathname(), $origFilename);
         $this->uploadedFile[$file->getBasename()] = $movedUploadedFile;
     } catch (\Exception $e) {
         $this->setResponseDetails(500, 'File move failed.');
         throw new FileMoveFailedException(sprintf('Cannot move file \'%s\' to destination dir \'%s\'.', $file->getBaseName(), $uploadDir));
     }
     return $this;
 }
コード例 #4
0
 /**
  * @param string $content
  * @param string $basename
  * @param int $desiredWidth
  *
  * @return HttpFile
  */
 private function createThumbnail(string $content, string $basename, int $desiredWidth) : HttpFile
 {
     try {
         // create from source image content
         $sourceImage = imagecreatefromstring($content);
         $width = imagesx($sourceImage);
         $height = imagesy($sourceImage);
         // find the "desired height" of this thumbnail, relative to the desired width
         $desiredHeight = (int) floor($height * ($desiredWidth / $width));
         // create a new, "virtual" image
         $virtualImage = imagecreatetruecolor($desiredWidth, $desiredHeight);
         // copy source image at a resized size
         imagecopyresampled($virtualImage, $sourceImage, 0, 0, 0, 0, $desiredWidth, $desiredHeight, $width, $height);
         // create the physical thumbnail image in the thumbnail directory
         $outPathname = sprintf('%s/%s%s', $this->configuration->getThumbDir(), static::THUMB_PREFIX, $basename);
         imagejpeg($virtualImage, $outPathname);
     } catch (\Exception $e) {
         throw new \RuntimeException(sprintf('Cannot create thumbnail from input file. %s', $e));
     }
     return $this->thumb = new HttpFile($outPathname);
 }