Example #1
0
 /**
  * Adds an uploaded file to the database. $file can be one of the following:
  * - A string: expected to be absolute path + filename. An Exception will be thrown if the file doesn't exist or is
  *      not readable for the apache user. File extension will be taken from the name, mime type will be determined
  *      by Symfony MimeTypeGuesser.
  * - An instance of SplFileInfo. File extension will be taken from the name, mime type will be determined
  *      by Symfony MimeTypeGuesser.
  * - An instance of Symfony types File or UploadedFile. Attributes will be determined by using the types member
  *      functions.
  *
  * The file will be copied for storage, the original file will not be removed.
  *
  * The attributes mime type, extension, title (slug), file name and order can optionally be configured. If null,
  * they are generated automatically from the file.
  * Note: $title will always be slugified.
  *
  * The optional parameter $garbage sets the initial state of the garbage collector mark (default false). If it
  * remains true, the entry will be deleted by the garbage collector in the future (1 day minimum). This can be
  * useful for forms with ajax upload ahead of the total submit, when an uploaded file is not yet connected to a
  * parent object and the form might be cancelled leaving the file object unconnected and unused.
  *
  * @param string|\SplFileInfo|File|UploadedFile $file           The file to add to the database
  * @param string|null                           $mimeType       [optional] Mime type, overrides automatically
  *                                                              guessed mime type
  * @param string|null                           $fileExtension  [optional] File extension, overrides automatically
  *                                                              determined extension
  * @param string|null                           $slug           [optional] slug, overrides automatically determined
  *                                                              slug (from file name)
  * @param string|null                           $fileName       [optional] File name, overrides automatically
  *                                                              determined file name
  * @param int|null                              $order          [optional] File order index
  * @param bool                                  $garbage        [optional] Initial mark for garbage collector
  * @throws \Exception   Thrown if file does not exist, is not readable, cannot be copied or parameter $file is of
  *                      unknown type.
  * @return EnhavoFile   The generated doctrine entity object (already persisted).
  */
 public function storeFile($file, $mimeType = null, $fileExtension = null, $slug = null, $fileName = null, $order = null, $garbage = false)
 {
     $fileInfo = $this->getFileInformation($file);
     if (!$fileInfo) {
         throw new \InvalidArgumentException("Invalid format on file parameter; possible formats: Symfony\\Component\\HttpFoundation\\File\\UploadedFile, Symfony\\Component\\HttpFoundation\\File\\File, \\SplFileInfo or string (absolute path + filename)");
     }
     $slugifier = new Slugifier();
     if ($mimeType == null) {
         $mimeType = $fileInfo['mime_type'];
     }
     if ($fileExtension == null) {
         $fileExtension = $fileInfo['extension'];
     }
     if ($slug == null) {
         $slug = $fileInfo['filename'];
     }
     $slug = $slugifier->slugify($slug) . '.' . $fileInfo['extension'];
     if ($fileName == null) {
         $fileName = $fileInfo['basename'];
     }
     $entityFile = new EnhavoFile();
     $entityFile->setMimeType($mimeType);
     $entityFile->setExtension($fileExtension);
     $entityFile->setSlug($slug);
     $entityFile->setFilename($fileName);
     $entityFile->setOrder($order);
     $entityFile->setGarbage($garbage);
     $this->manager->persist($entityFile);
     $this->manager->flush();
     try {
         copy($fileInfo['pathname'], $this->getDirectory($entityFile) . '/' . $entityFile->getId());
     } catch (\Exception $exception) {
         $this->manager->remove($entityFile);
         $this->manager->flush();
         throw $exception;
     }
     return $entityFile;
 }