public function executeRequest(RequestInterface $request) : ResponseInterface { try { $file = $this->fileRepository->getByFullPath($request['path']); $file->setPath(FilePathValue::get($request['new_path'])); $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 MoveFileHandler.', new ServerErrorResponse([], $request)); } }
public function it_can_execute_a_request(RequestInterface $request, File $file) { $path = '/path/to/a/file.ext'; $newPath = '/path/deux'; $request->offsetGet('path')->willReturn($path); $request->offsetGet('new_path')->willReturn($newPath); $request->getAcceptContentType()->willReturn('*/*'); $file->setPath(FilePathValue::get($newPath))->shouldBeCalled(); $this->fileRepository->getByFullPath($path)->willReturn($file); $this->fileRepository->updateMetaData($file)->shouldBeCalled(); $response = $this->executeRequest($request); $response->shouldHaveType(ResponseInterface::class); $response->getResponseName()->shouldReturn(MoveFileHandler::MESSAGE); $response['data']->shouldBe($file); }
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); }
/** @return string[] */ public function getDirectoriesInPath(string $path) : array { $query = $this->executeSql(' SELECT path FROM files WHERE mime_type LIKE "image/%" AND path LIKE CONCAT(:path, "%") AND path != :path GROUP BY path ORDER BY path ASC ', ['path' => $path]); /** @var FilePathValue[] $directories */ $directories = []; while ($directory = $query->fetchColumn()) { if (count($directories) > 0 && strpos($directory, end($directories)->toString()) === 0) { continue; } $directories[] = FilePathValue::get($directory); } return $directories; }
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()); }
public function it_can_change_its_path() { $newPath = FilePathValue::get('/'); $this->setPath($newPath)->shouldReturn($this); $this->getPath()->shouldReturn($newPath); }
private function getFileNameIndex(FilePathValue $path, string $fileName, string $extension) : int { $nameRegex = preg_quote($fileName) . '(_(?P<idx>[0-9]+))?\\.' . preg_quote($extension ?? ''); $query = $this->executeSql(' SELECT name FROM files WHERE path = :path AND name REGEXP :name ORDER BY name ASC ', ['path' => $path->toString(), 'name' => $nameRegex]); if ($query->rowCount() === 0) { return 0; } $max = 0; while ($row = $query->fetchColumn()) { if ($row === $fileName . '.' . $extension) { continue; } preg_match('#' . $nameRegex . '#', $row, $match); $max = max($max, intval($match['idx'], 1)); } return $max + 1; }
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); }