diff --git a/config/routes.php b/config/routes.php index 192d3ae..37d4feb 100644 --- a/config/routes.php +++ b/config/routes.php @@ -29,3 +29,4 @@ $app->get('/', App\Action\HomePageAction::class, 'home'); $app->get('/api/galleries', App\Action\ListGalleriesAction::class, 'api.galleries'); $app->get('/image/{slug}/{image}[/{thumb}]', App\Action\GetImageAction::class, 'download.image'); +$app->get('/export-album/{slug}', App\Action\ExportAlbumAction::class, 'export.album'); diff --git a/deploy.php b/deploy.php index 4bb3650..2f4dfe6 100644 --- a/deploy.php +++ b/deploy.php @@ -8,8 +8,9 @@ set('ssh_multiplexing', true); set('repository', 'https://gogs.ragnarok.yvan.hu/yvan/gallery-api.git'); set('shared_dirs', [ -// 'vendor', + 'data/export', 'data/galleries', + 'data/tmp', ]); set('shared_files', [ 'config/autoload/doctrine.local.php', diff --git a/public/export b/public/export new file mode 120000 index 0000000..2cc0b7b --- /dev/null +++ b/public/export @@ -0,0 +1 @@ +../data/export \ No newline at end of file diff --git a/src/App/Action/ExportAlbumAction.php b/src/App/Action/ExportAlbumAction.php new file mode 100644 index 0000000..2b491ad --- /dev/null +++ b/src/App/Action/ExportAlbumAction.php @@ -0,0 +1,27 @@ +galleryService = $galleryService; + } + + public function process(ServerRequestInterface $request, DelegateInterface $delegate) + { + $slug = $request->getAttribute('slug', false); + $this->galleryService->getExportFileName($slug); + return new Response\RedirectResponse("/export/{$slug}.zip"); + } + +} diff --git a/src/App/Action/ExportAlbumFactory.php b/src/App/Action/ExportAlbumFactory.php new file mode 100644 index 0000000..b9e75e9 --- /dev/null +++ b/src/App/Action/ExportAlbumFactory.php @@ -0,0 +1,15 @@ +get(GalleryService::class); + return new ExportAlbumAction($galleryService); + } +} diff --git a/src/App/ConfigProvider.php b/src/App/ConfigProvider.php index d046489..2e25d6e 100644 --- a/src/App/ConfigProvider.php +++ b/src/App/ConfigProvider.php @@ -44,6 +44,7 @@ class ConfigProvider Action\ListGalleriesAction::class => Action\ListGalleriesFactory::class, Action\GetImageAction::class => Action\GetImageFactory::class, + Action\ExportAlbumAction::class => Action\ExportAlbumFactory::class, Service\GalleryService::class => Service\GalleryServiceFactory::class, ], diff --git a/src/App/Service/GalleryService.php b/src/App/Service/GalleryService.php index 2b95d98..28b14ed 100644 --- a/src/App/Service/GalleryService.php +++ b/src/App/Service/GalleryService.php @@ -3,7 +3,6 @@ namespace App\Service; - use App\Entity\Image; use Doctrine\ORM\EntityManager; use Imagine\Gd\Imagine; @@ -17,6 +16,7 @@ class GalleryService const CONFIG_FILE = 'data/galleries/config.yaml'; const IMAGE_BASEDIR = 'data/galleries/%s/'; + const EXPORT_DIR = 'data/export/'; const THUMBNAIL_SIZE = 500; protected $contentTypeMap = [ @@ -36,25 +36,38 @@ class GalleryService $this->em = $entityManager; } - public function exportGallery(string $slug) + public function getExportFileName(string $slug) { $config = $this->getConfig(); - if (!isset($config['galleries'][$slug])) { return false; } - $dir = $config['galleries'][$slug]['dir']; - $images = array_map('basename', glob(sprintf(self::IMAGE_BASEDIR . "*.{jpg,jpeg,png}", $dir), GLOB_BRACE)); + return $this->ensureExportFileExists($dir, "{$slug}.zip"); + } + + private function ensureExportFileExists(string $dir, string $filename) + { + $exportName = self::EXPORT_DIR . $filename; + if(file_exists($exportName)) { + return $exportName; + } + + $images = array_map('basename', glob($this->getImageDir($dir) . "*.{jpg,jpeg,png}", GLOB_BRACE)); $zipName = tempnam("data/tmp", "export"); $zipArchive = new \ZipArchive(); $zipArchive->open($zipName, \ZipArchive::CREATE); foreach ($images as $image) { - $zipArchive->addFile("{$dir}/{$image}"); + ini_set("max_execution_time", 60); + $zipArchive->addFile( + sprintf(self::IMAGE_BASEDIR,$dir) . $image, + "{$dir}/{$image}" + ); } $zipArchive->close(); - return $zipName; + rename($zipName, $exportName); + return $exportName; } public function loadGalleryData($includeHidden = false)