Exemplo n.º 1
0
 public function getPath(File $file, $absolute = true)
 {
     if (!$file->getId()) {
         throw new \RuntimeException('File must be persisted');
     }
     $basename = $file->getId();
     if ($this->preserveExtension) {
         $extension = $file->getExtension();
         if ($extension) {
             $basename .= '.' . $extension;
         }
     }
     return $this->getDir($file, $absolute) . '/' . $basename;
 }
Exemplo n.º 2
0
 /**
  * @param string $filesystemName
  * @return File
  */
 public function write(File $file, $filesystemName, $content)
 {
     if ($file->getId()) {
         throw new FileAlreadyExistsException('File already persisted to database with id ' . $file->getId());
     }
     // get target filesystem
     $filesystem = $this->filesystemMap->get($filesystemName);
     // create file
     $file->setFilesystem($filesystemName);
     // check if hash already exists
     $persistedFile = $this->entityManager->getRepository('FileStorageBundle:File')->findOneByHash($file->getHash());
     if ($persistedFile && $persistedFile->getSize() === $file->getSize()) {
         $file = $persistedFile;
     } else {
         // register uploaded file
         $this->entityManager->persist($file);
         $this->entityManager->flush();
         // send file to filesystem
         $filesystem->write($file->getId(), $content, true);
     }
     return $file;
 }