/**
  * 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));
         }
     }
 }
 /**
  * @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;
 }
Example #3
0
 /**
  * Creates a thumbnail with a fixed width. Height is calculated accordingly. Returns the a File
  * entity that represents the calculated thumbnail. False if not.
  *
  * @param string  $pathname     path to the source file
  * @param integer $desiredWidth the desired width of the output thumbnail. Height will be adjusted accordingly.
  *
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  * @throws FileNotFoundException
  *
  * @return HttpFile
  */
 public function createScaledThumb(string $pathname, int $desiredWidth)
 {
     if (!is_string($pathname)) {
         throw new \InvalidArgumentException(sprintf('Pathname parameter must be of type string!'));
     }
     $fileStorage = $this->configuration->createFileStorage($this->entityManager);
     if (is_file($pathname)) {
         // local file handling
         $srcFile = new HttpFile($pathname);
         $srcFileContent = file_get_contents($srcFile->getPathname());
     } elseif ($srcFile = $fileStorage->getFile($pathname)) {
         // backend file storage handling
         $srcFileContent = $fileStorage->getContent($srcFile);
     } else {
         throw new FileNotFoundException(sprintf('Cannot create thumbnail for \'%s\'. File not found.', $pathname));
     }
     // do not process unsupported formats
     if (!$this->isSupportedImageFormat($srcFile->getMimeType())) {
         return false;
     }
     // before creating a thumbnail, try to find an already existing thumbnail for the requested file
     return ($thumbnail = $this->findThumbnail($srcFile->getBasename())) ? $thumbnail : $this->createThumbnail($srcFileContent, $srcFile->getBasename(), $desiredWidth);
 }