Example #1
0
 public function executeRequest(RequestInterface $request) : ResponseInterface
 {
     try {
         $file = $this->fileRepository->getByFullPath($request['path']);
         $file->setName(FileNameValue::get($request['filename']));
         $this->fileRepository->updateMetaData($file);
         return new Response(self::MESSAGE, ['data' => $file], $request);
     } catch (NoUniqueResultException $e) {
         return new NotFoundResponse([], $request);
     } catch (\Throwable $e) {
         $this->log(LogLevel::ERROR, $e->getMessage());
         throw new ResponseException('An error occurred during RenameFileHandler.', new ServerErrorResponse([], $request));
     }
 }
Example #2
0
 public function it_can_execute_a_request(RequestInterface $request, File $file)
 {
     $path = '/path/to/a/file.ext';
     $filename = 'path.deux';
     $request->offsetGet('path')->willReturn($path);
     $request->offsetGet('filename')->willReturn($filename);
     $request->getAcceptContentType()->willReturn('*/*');
     $file->setName(FileNameValue::get($filename))->shouldBeCalled();
     $this->fileRepository->getByFullPath($path)->willReturn($file);
     $this->fileRepository->updateMetaData($file)->shouldBeCalled();
     $response = $this->executeRequest($request);
     $response->shouldHaveType(ResponseInterface::class);
     $response->getResponseName()->shouldReturn(RenameFileHandler::MESSAGE);
     $response['data']->shouldBe($file);
 }
Example #3
0
 public function it_can_generate_an_image_response(ResponseInterface $response, File $file, ImageInterface $image)
 {
     $responseMock = 'i-am-not-really-an-image';
     $fileName = FileNameValue::get('image.png');
     $fileMimeType = MimeTypeValue::get('image/png');
     $response->offsetGet('image')->willReturn($image);
     $response->offsetGet('file')->willReturn($file);
     $file->getName()->willReturn($fileName);
     $file->getMimeType()->willReturn($fileMimeType);
     $this->imageEditor->output($file, $image)->willReturn($responseMock);
     $httpResponse = $this->generateResponse($response);
     $httpResponse->shouldHaveType(HttpResponse::class);
     $httpResponse->getStatusCode()->shouldReturn(200);
     $httpResponse->getHeaderLine('Content-Type')->shouldReturn($fileMimeType->toString());
     $httpResponse->getBody()->getContents()->shouldReturn($responseMock);
 }
Example #4
0
 public function it_can_create_an_image(File $oldFile)
 {
     $name = FileNameValue::get('oldName.jpg');
     $path = FilePathValue::get('/path/to/something');
     $mime = MimeTypeValue::get('image/jpg');
     $oldFile->getName()->willReturn($name);
     $oldFile->getPath()->willReturn($path);
     $oldFile->getMimeType()->willReturn($mime);
     $imageContents = tmpfile();
     $this->fileRepository->createFromUpload(new Argument\Token\TypeToken(File::class));
     $newImage = $this->createImage($oldFile, $imageContents);
     $newImage->getUuid()->shouldHaveType(Uuid::class);
     $newImage->getName()->shouldReturn($name);
     $newImage->getPath()->shouldReturn($path);
     $newImage->getMimeType()->shouldReturn($mime);
     $newImage->getStream()->shouldReturn($imageContents);
 }
Example #5
0
 public function let()
 {
     $this->file = (new File(Uuid::uuid4(), FileNameValue::get('file.ext'), FilePathValue::get('/path/to'), MimeTypeValue::get('text/text'), tmpfile()))->metaDataSetInsertTimestamp(new \DateTimeImmutable())->metaDataSetUpdateTimestamp(new \DateTimeImmutable());
 }
Example #6
0
 public function it_can_generate_a_response(ResponseInterface $response, File $file)
 {
     $fileName = 'file.ext';
     $mimeType = 'text/xml';
     $file->getStream()->willReturn(tmpfile());
     $file->getName()->willReturn(FileNameValue::get($fileName));
     $file->getMimeType()->willReturn(MimeTypeValue::get($mimeType));
     $response->offsetGet('data')->willReturn($file);
     $httpResponse = $this->generateResponse($response);
     $httpResponse->getHeaderLine('Content-Type')->shouldReturn($mimeType);
     $httpResponse->getHeaderLine('Content-Disposition')->shouldReturn('attachment; filename="' . $fileName . '"');
 }
Example #7
0
 public function it_can_change_its_name()
 {
     $newName = FileNameValue::get('other.txt');
     $this->setName($newName)->shouldReturn($this);
     $this->getName()->shouldReturn($newName);
 }
Example #8
0
 private function getUniqueFileName(FilePathValue $path, FileNameValue $name) : FileNameValue
 {
     $nameInfo = pathinfo($name->toString());
     $index = $this->getFileNameIndex($path, $nameInfo['filename'], $nameInfo['extension']);
     if ($index === 0) {
         return $name;
     }
     return FileNameValue::get($nameInfo['filename'] . '_' . strval($index) . '.' . ($nameInfo['extension'] ?? ''));
 }
Example #9
0
 public function it_will_roll_back_after_failed_update_file_meta_data(File $file)
 {
     $uuid = Uuid::uuid4();
     $name = FileNameValue::get('file.name');
     $path = FilePathValue::get('/uploads');
     $mime = MimeTypeValue::get('text/xml');
     $file->getUuid()->willReturn($uuid);
     $file->getName()->willReturn($name);
     $file->setName($name)->willReturn($file);
     $file->getPath()->willReturn($path);
     $file->getMimeType()->willReturn($mime);
     $this->pdo->beginTransaction()->shouldBeCalled();
     $this->pdo->prepare(new Argument\Token\StringContainsToken('UPDATE files'))->willThrow(new \RuntimeException());
     $this->objectRepository->update(File::TYPE, $uuid)->shouldNotBeCalled();
     $this->pdo->rollBack()->shouldBeCalled();
     $file->metaDataSetUpdateTimestamp(new Argument\Token\TypeToken(\DateTimeImmutable::class))->shouldNotBeCalled();
     $this->shouldThrow(\RuntimeException::class)->duringUpdateMetaData($file);
 }