public function testGetPath_PreserveExtension()
 {
     $file = new File();
     $file->setName('example.txt');
     $pathStrategy = new ChunkPathStrategy(['baseDir' => '/var', 'preserveExtension' => true]);
     $this->setPrivateProperty($file, 'id', 1);
     // 000001
     $this->assertEquals('/var/001/000/1.txt', $pathStrategy->getPath($file));
     $this->setPrivateProperty($file, 'id', 12);
     // 000012
     $this->assertEquals('/var/012/000/12.txt', $pathStrategy->getPath($file));
     $this->setPrivateProperty($file, 'id', 123);
     // 000123
     $this->assertEquals('/var/123/000/123.txt', $pathStrategy->getPath($file));
     $this->setPrivateProperty($file, 'id', 1234);
     // 001234
     $this->assertEquals('/var/234/001/1234.txt', $pathStrategy->getPath($file));
     $this->setPrivateProperty($file, 'id', 12345);
     // 012345
     $this->assertEquals('/var/345/012/12345.txt', $pathStrategy->getPath($file));
     $this->setPrivateProperty($file, 'id', 123456);
     // 123456
     $this->assertEquals('/var/456/123/123456.txt', $pathStrategy->getPath($file));
     $this->setPrivateProperty($file, 'id', 1234567);
     // 1234567
     $this->assertEquals('/var/567/234/1234567.txt', $pathStrategy->getPath($file));
 }
 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;
 }
 /**
  * @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;
 }
Beispiel #4
0
 public function testGetExtension()
 {
     $file = new File();
     $file->setName('example.TXT');
     $this->assertEquals('txt', $file->getExtension());
 }