From 40ac17d892ebaf1c0b64ab62a9a8255995db21d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danyi=20D=C3=A1vid?= Date: Fri, 21 Jul 2017 21:15:09 +0200 Subject: [PATCH] * cleanup --- config/routes.php | 1 - deploy.php | 2 +- src/App/Action/GetGalleryImagesAction.php | 4 +--- src/App/Action/GetImageAction.php | 21 ++++++++++++++++++--- src/App/Action/ListGalleriesAction.php | 4 +--- src/App/Service/GalleryService.php | 2 +- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/config/routes.php b/config/routes.php index bd0f4f0..db29a93 100644 --- a/config/routes.php +++ b/config/routes.php @@ -27,7 +27,6 @@ */ $app->get('/', App\Action\HomePageAction::class, 'home'); -$app->get('/api/ping', App\Action\PingAction::class, 'api.ping'); $app->get('/api/galleries', App\Action\ListGalleriesAction::class, 'api.galleries'); $app->get('/api/gallery/{slug}', App\Action\GetGalleryImagesAction::class, 'api.gallery'); $app->get('/api/image/{slug}/{image}[/thumb]', App\Action\GetImageAction::class, 'api.image'); diff --git a/deploy.php b/deploy.php index 5b6debb..929063c 100644 --- a/deploy.php +++ b/deploy.php @@ -8,7 +8,7 @@ set('ssh_multiplexing', true); set('repository', 'ssh://gogs@gogs.ragnarok.yvan.hu:2206/yvan/gallery-api.git'); set('shared_dirs', [ - 'vendor', +// 'vendor', 'data/galleries', ]); set('writable_dirs', []); diff --git a/src/App/Action/GetGalleryImagesAction.php b/src/App/Action/GetGalleryImagesAction.php index 40fb2e5..a493c20 100644 --- a/src/App/Action/GetGalleryImagesAction.php +++ b/src/App/Action/GetGalleryImagesAction.php @@ -20,8 +20,6 @@ class GetGalleryImagesAction implements ServerMiddlewareInterface public function process(ServerRequestInterface $request, DelegateInterface $delegate) { $slug = $request->getAttribute('slug', false); - return new JsonResponse([ - 'images' => $this->galleryService->getGallery($slug), - ]); + return new JsonResponse($this->galleryService->getGallery($slug)); } } diff --git a/src/App/Action/GetImageAction.php b/src/App/Action/GetImageAction.php index f1624b2..c247f85 100644 --- a/src/App/Action/GetImageAction.php +++ b/src/App/Action/GetImageAction.php @@ -6,7 +6,9 @@ use App\Service\GalleryService; use Interop\Http\ServerMiddleware\DelegateInterface; use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface; use Psr\Http\Message\ServerRequestInterface; +use Zend\Diactoros\Response; use Zend\Diactoros\Response\JsonResponse; +use Zend\Diactoros\Stream; class GetImageAction implements ServerMiddlewareInterface { @@ -20,8 +22,21 @@ class GetImageAction implements ServerMiddlewareInterface public function process(ServerRequestInterface $request, DelegateInterface $delegate) { $slug = $request->getAttribute('slug', false); - return new JsonResponse([ - 'images' => $this->galleryService->getGallery($slug), - ]); + $image = $request->getAttribute('image', false); + $thumb = $request->getAttribute('thumb', false); + + $imageFileName = $this->galleryService->getImage($slug, $image, $thumb); + $stream = new Stream(fopen($imageFileName, 'r')); + $response = new Response(); + return $response + ->withHeader('Content-Type', (new \finfo(FILEINFO_MIME))->file($imageFileName)) + ->withHeader('Content-Disposition', 'attachment; filename=' . basename($imageFileName)) + ->withHeader('Content-Transfer-Encoding', 'Binary') + ->withHeader('Content-Description', 'File Transfer') + ->withHeader('Pragma', 'public') + ->withHeader('Expires', '0') + ->withHeader('Cache-Control', 'must-revalidate') + ->withBody($stream) + ->withHeader('Content-Length', "{$stream->getSize()}"); } } diff --git a/src/App/Action/ListGalleriesAction.php b/src/App/Action/ListGalleriesAction.php index 63264ce..41a7179 100644 --- a/src/App/Action/ListGalleriesAction.php +++ b/src/App/Action/ListGalleriesAction.php @@ -19,8 +19,6 @@ class ListGalleriesAction implements ServerMiddlewareInterface public function process(ServerRequestInterface $request, DelegateInterface $delegate) { - return new JsonResponse([ - 'galleries' => $this->galleryService->listGalleries() - ]); + return new JsonResponse($this->galleryService->listGalleries()); } } diff --git a/src/App/Service/GalleryService.php b/src/App/Service/GalleryService.php index 3cae114..3bd397f 100644 --- a/src/App/Service/GalleryService.php +++ b/src/App/Service/GalleryService.php @@ -69,7 +69,7 @@ class GalleryService ? $galleryDir . $image : $this->getResizedImageName($galleryDir, $image, $size); - return fopen($imageFileName, 'r'); + return $imageFileName; } protected function getResizedImageName($galleryDirectory, $imageName, $size)