Added post content and thumbnail presenter

This commit is contained in:
Marcin Kurczewski 2014-09-17 12:59:33 +02:00
parent 4ac91374b9
commit 55f4f4430b
3 changed files with 69 additions and 10 deletions

View file

@ -0,0 +1,46 @@
<?php
namespace Szurubooru\Controllers;
final class PostContentController extends AbstractController
{
private $postService;
private $fileService;
private $httpHelper;
private $thumbnailService;
public function __construct(
\Szurubooru\Services\PostService $postService,
\Szurubooru\Services\FileService $fileService,
\Szurubooru\Helpers\HttpHelper $httpHelper,
\Szurubooru\Services\ThumbnailService $thumbnailService)
{
$this->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);
}
}

View file

@ -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';
}
}

View file

@ -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),
];
}),
];