Moved post content controller to routes

This commit is contained in:
Marcin Kurczewski 2014-11-21 11:27:51 +01:00
parent 7ff961fc21
commit 193d1c5f7a
3 changed files with 65 additions and 21 deletions

View file

@ -1,45 +1,45 @@
<?php <?php
namespace Szurubooru\Controllers; namespace Szurubooru\Routes\Posts;
use Szurubooru\Config; use Szurubooru\Config;
use Szurubooru\Dao\PublicFileDao; use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Entities\Post; use Szurubooru\Entities\Post;
use Szurubooru\Helpers\MimeHelper; use Szurubooru\Helpers\MimeHelper;
use Szurubooru\Router; use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Services\NetworkingService; use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\PostService; use Szurubooru\Services\PostService;
use Szurubooru\Services\PostThumbnailService;
final class PostContentController extends AbstractController class GetPostContent extends AbstractRoute
{ {
private $config; private $config;
private $fileDao; private $fileDao;
private $postService; private $postService;
private $networkingService; private $networkingService;
private $postThumbnailService;
public function __construct( public function __construct(
Config $config, Config $config,
PublicFileDao $fileDao, PublicFileDao $fileDao,
PostService $postService, PostService $postService,
NetworkingService $networkingService, NetworkingService $networkingService)
PostThumbnailService $postThumbnailService)
{ {
$this->config = $config; $this->config = $config;
$this->fileDao = $fileDao; $this->fileDao = $fileDao;
$this->postService = $postService; $this->postService = $postService;
$this->networkingService = $networkingService; $this->networkingService = $networkingService;
$this->postThumbnailService = $postThumbnailService;
} }
public function registerRoutes(Router $router) public function getMethods()
{ {
$router->get('/api/posts/:postName/content', [$this, 'getPostContent']); return ['GET'];
$router->get('/api/posts/:postName/thumbnail/:size', [$this, 'getPostThumbnail']);
} }
public function getPostContent($postName) public function getUrl()
{ {
$post = $this->postService->getByName($postName); return '/api/posts/:postName/content';
}
public function work()
{
$post = $this->postService->getByName($this->getArgument('postName'));
$customFileName = sprintf('%s_%s.%s', $customFileName = sprintf('%s_%s.%s',
$this->config->basic->serviceName, $this->config->basic->serviceName,
@ -54,11 +54,4 @@ final class PostContentController extends AbstractController
$this->networkingService->serveFile($this->fileDao->getFullPath($post->getContentPath()), $customFileName); $this->networkingService->serveFile($this->fileDao->getFullPath($post->getContentPath()), $customFileName);
} }
public function getPostThumbnail($postName, $size)
{
$post = $this->postService->getByName($postName);
$thumbnailName = $this->postThumbnailService->generateIfNeeded($post, $size, $size);
$this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName));
}
} }

View file

@ -0,0 +1,50 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Config;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Entities\Post;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PostThumbnailService;
class GetPostThumbnail extends AbstractRoute
{
private $config;
private $fileDao;
private $postService;
private $networkingService;
private $postThumbnailService;
public function __construct(
Config $config,
PublicFileDao $fileDao,
PostService $postService,
NetworkingService $networkingService,
PostThumbnailService $postThumbnailService)
{
$this->config = $config;
$this->fileDao = $fileDao;
$this->postService = $postService;
$this->networkingService = $networkingService;
$this->postThumbnailService = $postThumbnailService;
}
public function getMethods()
{
return ['GET'];
}
public function getUrl()
{
return '/api/posts/:postName/thumbnail/:size';
}
public function work()
{
$size = $this->getArgument('size');
$post = $this->postService->getByName($this->getArgument('postName'));
$thumbnailName = $this->postThumbnailService->generateIfNeeded($post, $size, $size);
$this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName));
}
}

View file

@ -60,7 +60,6 @@ return [
$container->get(\Szurubooru\Controllers\UserController::class), $container->get(\Szurubooru\Controllers\UserController::class),
$container->get(\Szurubooru\Controllers\UserAvatarController::class), $container->get(\Szurubooru\Controllers\UserAvatarController::class),
$container->get(\Szurubooru\Controllers\PostController::class), $container->get(\Szurubooru\Controllers\PostController::class),
$container->get(\Szurubooru\Controllers\PostContentController::class),
$container->get(\Szurubooru\Controllers\PostNotesController::class), $container->get(\Szurubooru\Controllers\PostNotesController::class),
$container->get(\Szurubooru\Controllers\ScoreController::class), $container->get(\Szurubooru\Controllers\ScoreController::class),
$container->get(\Szurubooru\Controllers\TagController::class), $container->get(\Szurubooru\Controllers\TagController::class),
@ -80,6 +79,8 @@ return [
$container->get(\Szurubooru\Routes\Favorites\GetFavoriteUsers::class), $container->get(\Szurubooru\Routes\Favorites\GetFavoriteUsers::class),
$container->get(\Szurubooru\Routes\Favorites\AddToFavorites::class), $container->get(\Szurubooru\Routes\Favorites\AddToFavorites::class),
$container->get(\Szurubooru\Routes\Favorites\RemoveFromFavorites::class), $container->get(\Szurubooru\Routes\Favorites\RemoveFromFavorites::class),
$container->get(\Szurubooru\Routes\Posts\GetPostContent::class),
$container->get(\Szurubooru\Routes\Posts\GetPostThumbnail::class),
]; ];
}), }),
]; ];