diff --git a/src/Controllers/PostContentController.php b/src/Controllers/PostContentController.php new file mode 100644 index 00000000..1410f201 --- /dev/null +++ b/src/Controllers/PostContentController.php @@ -0,0 +1,46 @@ +postService = $postService; + $this->fileService = $fileService; + $this->httpHelper = $httpHelper; + $this->thumbnailService = $thumbnailService; + } + + public function registerRoutes(\Szurubooru\Router $router) + { + $router->get('/api/posts/:postName/content', [$this, 'getPostContent']); + $router->get('/api/posts/:postName/thumbnail/:size', [$this, 'getPostThumbnail']); + } + + public function getPostContent($postName) + { + $post = $this->postService->getByName($postName); + $source = $this->postService->getPostContentPath($post); + $this->fileService->serve($source); + } + + public function getPostThumbnail($postName, $size) + { + $post = $this->postService->getByName($postName); + $source = $this->postService->getPostThumbnailSourcePath($post); + if (!$this->fileService->exists($source)) + $source = $this->postService->getPostContentPath($post); + + $sizedSource = $this->thumbnailService->getOrGenerate($source, $size, $size); + $this->fileService->serve($sizedSource); + } +} diff --git a/src/Services/PostService.php b/src/Services/PostService.php index eb1211e4..4ea4d7a1 100644 --- a/src/Services/PostService.php +++ b/src/Services/PostService.php @@ -29,6 +29,18 @@ class PostService $this->authService = $authService; } + public function getByName($postName) + { + $transactionFunc = function() use ($postName) + { + $post = $this->postDao->findByName($postName); + if (!$post) + throw new \InvalidArgumentException('Post with name "' . $postName . '" was not found.'); + return $post; + }; + return $this->transactionManager->rollback($transactionFunc); + } + public function createPost(\Szurubooru\FormData\UploadFormData $formData) { $transactionFunc = function() use ($formData) @@ -52,6 +64,16 @@ class PostService return $this->transactionManager->commit($transactionFunc); } + public function getPostContentPath(\Szurubooru\Entities\Post $post) + { + return 'posts' . DIRECTORY_SEPARATOR . $post->getName(); + } + + public function getPostThumbnailSourcePath(\Szurubooru\Entities\Post $post) + { + return 'posts' . DIRECTORY_SEPARATOR . $post->getName() . '-custom-thumb'; + } + private function updatePostSafety(\Szurubooru\Entities\Post $post, $newSafety) { $post->setSafety($newSafety); @@ -167,14 +189,4 @@ class PostService return $name; } } - - private function getPostContentPath(\Szurubooru\Entities\Post $post) - { - return 'posts' . DIRECTORY_SEPARATOR . $post->getName(); - } - - private function getPostThumbnailSourcePath(\Szuruboor\Entities\Post $post) - { - return 'posts' . DIRECTORY_SEPARATOR . $post->getName() . '-custom-thumb'; - } } diff --git a/src/di.php b/src/di.php index 9f706e41..efb5c7c0 100644 --- a/src/di.php +++ b/src/di.php @@ -21,6 +21,7 @@ return [ $container->get(\Szurubooru\Controllers\UserController::class), $container->get(\Szurubooru\Controllers\UserAvatarController::class), $container->get(\Szurubooru\Controllers\PostController::class), + $container->get(\Szurubooru\Controllers\PostContentController::class), ]; }), ];