/** * @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')); }
/** * @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; }
/** * @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; }
/** * @covers RI\FileManagerBundle\Model\Selection\CopySelectionModel::copy * @covers RI\FileManagerBundle\Model\Selection\CopySelectionModel::copyFolders * @covers RI\FileManagerBundle\Model\Selection\CopySelectionModel::getDirectoryById * @covers RI\FileManagerBundle\Model\Selection\CopySelectionModel::getFileById * @covers RI\FileManagerBundle\Model\Selection\CopySelectionModel::copyFiles * @covers RI\FileManagerBundle\Model\Selection\CopySelectionModel::copyFile * @covers RI\FileManagerBundle\Model\Selection\CopySelectionModel::copyFileOnDisk * @covers RI\FileManagerBundle\Model\Selection\CopySelectionModel::copyFolderRecursive */ public function testCopy_ShouldReturnTrue() { $this->setMocks(); $this->directoryDataProviderMock->expects($this->once())->method('getDirectoryParentsList')->with($this->directories[self::DIR_ID_1])->will($this->returnValue(array())); $this->directoryRepositoryMock->expects($this->at(0))->method('find')->with(self::DIR_ID_1)->will($this->returnValue($this->directories[self::DIR_ID_1])); $this->directoryRepositoryMock->expects($this->at(1))->method('find')->with(self::DIR_ID_2)->will($this->returnValue($this->directories[self::DIR_ID_2])); $this->fileRepositoryMock->expects($this->at(0))->method('fetchFromDir')->with($this->directories[self::DIR_ID_2])->will($this->returnValue($this->files[self::FILE_ID_2])); $this->fileRepositoryMock->expects($this->at(1))->method('find')->with(self::FILE_ID_1)->will($this->returnValue($this->files[self::FILE_ID_1])); $this->fileRepositoryMock->expects($this->at(2))->method('find')->with(self::FILE_ID_2)->will($this->returnValue($this->files[self::FILE_ID_2])); $this->entityManagerMock->expects($this->any())->method('persist'); $this->uploadDirectoryManagerMock->expects($this->at(0))->method('getNewPath')->with('abc.jpg')->will($this->returnValue('/../../web/abc_1.jpg')); $this->uploadDirectoryManagerMock->expects($this->at(1))->method('getAbsoluteUploadDir')->will($this->returnValue(__DIR__)); $this->uploadDirectoryManagerMock->expects($this->at(2))->method('getNewPath')->with('xyz.jpg')->will($this->returnValue('/../../web/xyz_1.jpg')); $this->uploadDirectoryManagerMock->expects($this->at(3))->method('getAbsoluteUploadDir')->will($this->returnValue(__DIR__)); $result = $this->CopySelectionModel->copy(self::DIR_ID_1, array(self::FILE_ID_1, self::FILE_ID_2), array(self::DIR_ID_2)); $this->assertTrue($result); }