/**
  * @covers: RI\FileManagerBundle\Manager\UploadDirectoryManager::getNewPath
  * @covers: RI\FileManagerBundle\Manager\UploadDirectoryManager::getAbsoluteUploadDirPath
  * @covers: RI\FileManagerBundle\Manager\UploadDirectoryManager::generateNewFileName
  * @covers: RI\FileManagerBundle\Manager\UploadDirectoryManager::createDestinationDir
  */
 public function testGetNewPath_IfFileExist()
 {
     $date = new \DateTime();
     $expectedPath = sprintf('/upload/%s/%s/%s/7/79/790f1bfea8585c9bc2e7b6267cb212e1.jpg', $date->format('y'), $date->format('m'), $date->format('d'));
     $expectedPathSecondFile = sprintf('/upload/%s/%s/%s/7/79/8b912a2bf1302f5a1170d3e57d8f8caf.jpg', $date->format('y'), $date->format('m'), $date->format('d'));
     $this->assertEquals($expectedPath, $this->uploadDirectoryManager->getNewPath('hello_world.jpg'));
     copy(__DIR__ . '/hello_world.jpg', sprintf('/tmp/web/upload/%s/%s/%s/7/79/790f1bfea8585c9bc2e7b6267cb212e1.jpg', $date->format('y'), $date->format('m'), $date->format('d')));
     $this->assertEquals($expectedPathSecondFile, $this->uploadDirectoryManager->getNewPath('hello_world.jpg'));
 }
Esempio n. 2
0
 /**
  * @param $filePath
  *
  * @return string
  * @throws CopyMoveSelectionException
  */
 private function copyFileOnDisk($filePath)
 {
     $filename = basename($filePath);
     $newFileName = $this->uploadDirectoryManager->getNewPath($filename);
     $uploadDir = $this->uploadDirectoryManager->getAbsoluteUploadDir();
     if (!copy($uploadDir . $filePath, $uploadDir . $newFileName)) {
         throw new CopyMoveSelectionException('File not coppied ' . $filePath);
     }
     return $newFileName;
 }
Esempio n. 3
0
 /**
  * @param string       $filename
  * @param UploadedFile $uploadedFile
  * @param Directory    $directory
  *
  * @return File
  */
 public function save($filename, UploadedFile $uploadedFile, Directory $directory = null)
 {
     if (!$this->isAllowMimeTypes($uploadedFile)) {
         throw new UploadException(sprintf('File type %s is not allowed to upload', $uploadedFile->getMimeType()));
     }
     $newFilePath = $this->uploadDirectoryManager->getNewPath($filename);
     $size = $uploadedFile->getSize();
     $mimeType = $uploadedFile->getMimeType();
     $uploadedFile->move($this->webDir . dirname($newFilePath), basename($newFilePath));
     $path = $this->webDir . $newFilePath;
     if ($this->doResize) {
         $this->resizeImage($path);
     }
     $fileParams = $this->createFileParams($size, $mimeType, $path);
     $file = new File();
     $file->setName($filename);
     $file->setDirectory($directory);
     $file->setPath($newFilePath);
     $file->setChecksum($this->getChecksum($newFilePath));
     $file->setParams($fileParams);
     $this->entityManager->persist($file);
     $this->entityManager->flush();
     return $file;
 }