From d0da729ca081b09204ad7dfcde4e4c1824766a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Provazn=C3=ADk?= <41260648+paproc@users.noreply.github.com> Date: Fri, 3 Jan 2025 19:08:05 +0000 Subject: [PATCH 1/4] galery default --- .../ImageGallery/ImageGalleryControl.php | 21 ++++++++++---- app/Components/ImageGallery/default.latte | 2 +- .../Vyfuk/DefaultModule/DefaultPresenter.php | 28 +++++++++++++++++++ app/RouterFactory.php | 13 +++++---- www/vyfuk/.htaccess | 2 +- 5 files changed, 53 insertions(+), 13 deletions(-) diff --git a/app/Components/ImageGallery/ImageGalleryControl.php b/app/Components/ImageGallery/ImageGalleryControl.php index abde0afce..8cdc2c922 100644 --- a/app/Components/ImageGallery/ImageGalleryControl.php +++ b/app/Components/ImageGallery/ImageGalleryControl.php @@ -9,7 +9,6 @@ use Nette\Caching\Storage; use Nette\DI\Container; use Nette\Utils\Finder; -use Nette\Utils\Image; use Nette\Utils\UnknownImageFileException; class ImageGalleryControl extends DIComponent @@ -44,11 +43,21 @@ public static function getImages(string $path, string $wwwDir): array foreach ($iterator as $file) { $imageInfo = getimagesize($file->getPathname()); $wwwPath = substr($file->getPathname(), strlen($wwwDir)); - $images[] = [ - 'src' => $wwwPath, - 'width' => $imageInfo[0], - 'height' => $imageInfo[1], - ]; + if (strpos($wwwPath, '/media/') === 0) { + $images[] = [ + 'src' => $wwwPath, + 'width' => $imageInfo[0], + 'height' => $imageInfo[1], + 'srcset' => $wwwPath . ' ' . $imageInfo[0] . 'w,' . str_replace('/media/', '/media/preview/', $wwwPath) . ' 1024w', + 'previewSrc' => str_replace('/media/', '/media/preview/', $wwwPath), + ]; + } else { + $images[] = [ + 'src' => $wwwPath, + 'width' => $imageInfo[0], + 'height' => $imageInfo[1], + ]; + } } usort($images, function ($a, $b) { diff --git a/app/Components/ImageGallery/default.latte b/app/Components/ImageGallery/default.latte index c876c1e64..185c7eb64 100644 --- a/app/Components/ImageGallery/default.latte +++ b/app/Components/ImageGallery/default.latte @@ -5,7 +5,7 @@ data-pswp-width="{$image['width']}" data-pswp-height="{$image['height']}" target="_blank"> - + {/foreach} diff --git a/app/Modules/Vyfuk/DefaultModule/DefaultPresenter.php b/app/Modules/Vyfuk/DefaultModule/DefaultPresenter.php index 1f46aff73..10eceea42 100644 --- a/app/Modules/Vyfuk/DefaultModule/DefaultPresenter.php +++ b/app/Modules/Vyfuk/DefaultModule/DefaultPresenter.php @@ -7,6 +7,9 @@ use App\Models\Downloader\ProblemService; use App\Models\Downloader\EventService; use Fykosak\FKSDBDownloaderCore\Requests\SeriesResultsRequest; +use Nette\Application\Responses\FileResponse; +use Nette\DI\Container; +use Nette\Utils\Image; class DefaultPresenter extends BasePresenter { @@ -29,6 +32,13 @@ public function injectEventServicesAndCache(EventService $eventService): void $this->eventService = $eventService; } + private readonly string $wwwDir; + + public function injectWwwDir(Container $container): void + { + $this->wwwDir = $container->getParameters()['wwwDir']; + } + public function renderDefault(): void { $this->template->newsList = $this->loadNews(); @@ -79,4 +89,22 @@ public function loadNews(): array return $newsList; } + + public function renderPreview(string $path): void + { + $basepath = realpath($this->wwwDir . '/media'); + $path = realpath($basepath . '/' . $path); + if ($path === false || strpos($path, $basepath . '/') !== 0) { + $this->error(); + } + $path = substr($path, strlen($basepath)); + if (!is_file($basepath . '/preview/' . $path)) { + $img = Image::fromFile($basepath . '/' . $path); + if (!is_dir(dirname($basepath . '/preview/' . $path))) { + mkdir(dirname($basepath . '/preview/' . $path), recursive: true); + } + $img->resize(1024, 1024)->save($basepath . '/preview/' . $path); + } + $this->sendResponse(new FileResponse($basepath . '/preview/' . $path)); + } } diff --git a/app/RouterFactory.php b/app/RouterFactory.php index 3b865d6f2..917da16df 100755 --- a/app/RouterFactory.php +++ b/app/RouterFactory.php @@ -282,14 +282,17 @@ public static function createVyfukRouter(?array $domainList, array $routerMappin 'presenter' => 'Problems', 'action' => 'default', null => self::useTranslateFilter($domainList, $routerMapping['default']), - ]); - - $router->withModule('Default') - ->addRoute('///[/]', [ + ]) + ->addRoute('///media/preview/', [ + 'presenter' => 'Default', + 'action' => 'preview', + null => self::useTranslateFilter($domainList, $routerMapping['default']), + ])->addRoute('///[/]', [ 'presenter' => 'Default', 'action' => 'default', null => self::useTranslateFilter($domainList, $routerMapping['default']), - ]); + ]) + ; return $router; } diff --git a/www/vyfuk/.htaccess b/www/vyfuk/.htaccess index b35c4ec3a..1ffbc6cb0 100644 --- a/www/vyfuk/.htaccess +++ b/www/vyfuk/.htaccess @@ -21,7 +21,7 @@ Require all granted # front controller RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d - RewriteRule !\.(pdf|js|mjs|ico|gif|jpg|jpeg|png|webp|svg|css|rar|zip|7z|tar\.gz|map|eot|ttf|otf|woff|woff2)$ index.php [L] + RewriteRule !\.(pdf|js|mjs|ico|svg|css|rar|zip|7z|tar\.gz|map|eot|ttf|otf|woff|woff2)$ index.php [L] # enable gzip compression From 10d16514c5f76f9aeac53974a671d241a9457c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Provazn=C3=ADk?= <41260648+paproc@users.noreply.github.com> Date: Sun, 12 Jan 2025 00:34:47 +0000 Subject: [PATCH 2/4] lazy images --- app/Components/ImageGallery/default.latte | 2 +- app/Components/ImageGallery/oneLine.latte | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Components/ImageGallery/default.latte b/app/Components/ImageGallery/default.latte index 185c7eb64..8126051b1 100644 --- a/app/Components/ImageGallery/default.latte +++ b/app/Components/ImageGallery/default.latte @@ -5,7 +5,7 @@ data-pswp-width="{$image['width']}" data-pswp-height="{$image['height']}" target="_blank"> - + {/foreach} diff --git a/app/Components/ImageGallery/oneLine.latte b/app/Components/ImageGallery/oneLine.latte index 3593c703a..fc82d5c41 100644 --- a/app/Components/ImageGallery/oneLine.latte +++ b/app/Components/ImageGallery/oneLine.latte @@ -6,7 +6,7 @@
- + {if $iterator->isLast() && count($images) > count($previewImages)}
From 9f013bea3bd9b43d3eb0ceb7d312cc4f48ffbadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Provazn=C3=ADk?= <41260648+paproc@users.noreply.github.com> Date: Sun, 12 Jan 2025 01:20:49 +0000 Subject: [PATCH 3/4] all pages --- app/Modules/Core/BasePresenter.php | 28 +++++++++++++++++ .../Vyfuk/DefaultModule/DefaultPresenter.php | 28 ----------------- app/RouterFactory.php | 31 ++++++++++++++----- www/dsef/.htaccess | 2 +- www/fof/.htaccess | 2 +- www/fol/.htaccess | 2 +- www/fykos/.htaccess | 2 +- 7 files changed, 55 insertions(+), 40 deletions(-) diff --git a/app/Modules/Core/BasePresenter.php b/app/Modules/Core/BasePresenter.php index 7b97a2f69..36a1c63f5 100644 --- a/app/Modules/Core/BasePresenter.php +++ b/app/Modules/Core/BasePresenter.php @@ -13,8 +13,11 @@ use Fykosak\Utils\Localization\UnsupportedLanguageException; use Fykosak\Utils\UI\Navigation\NavItem; use Fykosak\Utils\UI\PageTitle; +use Nette\Application\Responses\FileResponse; use Nette\Application\UI\Presenter; use Nette\Application\UI\Template; +use Nette\DI\Container; +use Nette\Utils\Image; abstract class BasePresenter extends Presenter { @@ -142,4 +145,29 @@ protected function createComponentPdfGallery(): PdfGalleryControl { return new PdfGalleryControl($this->getContext()); } + + private readonly string $wwwDir; + + public function injectWwwDir(Container $container): void + { + $this->wwwDir = $container->getParameters()['wwwDir']; + } + + public function actionPreview(string $path): void + { + $basepath = realpath($this->wwwDir . '/media'); + $path = realpath($basepath . '/' . $path); + if ($path === false || strpos($path, $basepath . '/') !== 0) { + $this->error(); + } + $path = substr($path, strlen($basepath)); + if (!is_file($basepath . '/preview/' . $path)) { + $img = Image::fromFile($basepath . '/' . $path); + if (!is_dir(dirname($basepath . '/preview/' . $path))) { + mkdir(dirname($basepath . '/preview/' . $path), recursive: true); + } + $img->resize(1024, 1024)->save($basepath . '/preview/' . $path); + } + $this->sendResponse(new FileResponse($basepath . '/preview/' . $path)); + } } diff --git a/app/Modules/Vyfuk/DefaultModule/DefaultPresenter.php b/app/Modules/Vyfuk/DefaultModule/DefaultPresenter.php index 10eceea42..1f46aff73 100644 --- a/app/Modules/Vyfuk/DefaultModule/DefaultPresenter.php +++ b/app/Modules/Vyfuk/DefaultModule/DefaultPresenter.php @@ -7,9 +7,6 @@ use App\Models\Downloader\ProblemService; use App\Models\Downloader\EventService; use Fykosak\FKSDBDownloaderCore\Requests\SeriesResultsRequest; -use Nette\Application\Responses\FileResponse; -use Nette\DI\Container; -use Nette\Utils\Image; class DefaultPresenter extends BasePresenter { @@ -32,13 +29,6 @@ public function injectEventServicesAndCache(EventService $eventService): void $this->eventService = $eventService; } - private readonly string $wwwDir; - - public function injectWwwDir(Container $container): void - { - $this->wwwDir = $container->getParameters()['wwwDir']; - } - public function renderDefault(): void { $this->template->newsList = $this->loadNews(); @@ -89,22 +79,4 @@ public function loadNews(): array return $newsList; } - - public function renderPreview(string $path): void - { - $basepath = realpath($this->wwwDir . '/media'); - $path = realpath($basepath . '/' . $path); - if ($path === false || strpos($path, $basepath . '/') !== 0) { - $this->error(); - } - $path = substr($path, strlen($basepath)); - if (!is_file($basepath . '/preview/' . $path)) { - $img = Image::fromFile($basepath . '/' . $path); - if (!is_dir(dirname($basepath . '/preview/' . $path))) { - mkdir(dirname($basepath . '/preview/' . $path), recursive: true); - } - $img->resize(1024, 1024)->save($basepath . '/preview/' . $path); - } - $this->sendResponse(new FileResponse($basepath . '/preview/' . $path)); - } } diff --git a/app/RouterFactory.php b/app/RouterFactory.php index 917da16df..c947b879c 100755 --- a/app/RouterFactory.php +++ b/app/RouterFactory.php @@ -162,6 +162,10 @@ public static function createFolRouter(?array $domainList, array $routerMapping) ]); $router->withModule('Default') + ->addRoute('///media/preview/', [ + 'presenter' => 'Default', + 'action' => 'preview', + ]) ->addRoute('///[/]', [ 'presenter' => 'Default', 'action' => 'default', @@ -183,6 +187,10 @@ public static function createFofRouter(?array $domainList, array $routerMapping) ]); $router->withModule('Default') + ->addRoute('///media/preview/', [ + 'presenter' => 'Default', + 'action' => 'preview', + ]) ->addRoute('///(international|erasmus)', [ 'presenter' => 'Erasmus', 'lang' => 'en', @@ -202,6 +210,10 @@ public static function createDsefRouter(?array $domainList, array $routerMapping $router = new RouteList(); $router->withModule('Default') + ->addRoute('///media/preview/', [ + 'presenter' => 'Default', + 'action' => 'preview', + ]) ->addRoute('///[/]', [ 'presenter' => 'Default', 'action' => 'default', @@ -228,6 +240,10 @@ public static function createFykosRouter(?array $domainList, array $routerMappin 'presenter' => 'Results', 'action' => 'default', null => self::useTranslateFilter($domainList, $routerMapping['default']) + ]) + ->addRoute('///media/preview/', [ + 'presenter' => 'Default', + 'action' => 'preview', ]); $router->withModule('Default') @@ -252,10 +268,10 @@ public static function createFykosRouter(?array $domainList, array $routerMappin ]); $router->withModule('Events') - ->addRoute('///akce/tsaf/-', [ - 'presenter' => 'Tsaf', - 'action' => 'detail' - ]); + ->addRoute('///akce/tsaf/-', [ + 'presenter' => 'Tsaf', + 'action' => 'detail' + ]); $router->addRoute('////[[/]]', [ 'presenter' => 'Default', @@ -286,13 +302,12 @@ public static function createVyfukRouter(?array $domainList, array $routerMappin ->addRoute('///media/preview/', [ 'presenter' => 'Default', 'action' => 'preview', - null => self::useTranslateFilter($domainList, $routerMapping['default']), - ])->addRoute('///[/]', [ + ]) + ->addRoute('///[/]', [ 'presenter' => 'Default', 'action' => 'default', null => self::useTranslateFilter($domainList, $routerMapping['default']), - ]) - ; + ]); return $router; } diff --git a/www/dsef/.htaccess b/www/dsef/.htaccess index 68f2bae54..fcdc37ee4 100644 --- a/www/dsef/.htaccess +++ b/www/dsef/.htaccess @@ -21,7 +21,7 @@ Require all granted # front controller RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d - RewriteRule !\.(pdf|js|mjs|ico|gif|jpg|jpeg|png|webp|svg|css|rar|zip|7z|tar\.gz|map|eot|ttf|otf|woff|woff2)$ index.php [L] + RewriteRule !\.(pdf|js|mjs|ico|svg|css|rar|zip|7z|tar\.gz|map|eot|ttf|otf|woff|woff2)$ index.php [L] # enable gzip compression diff --git a/www/fof/.htaccess b/www/fof/.htaccess index 68f2bae54..fcdc37ee4 100644 --- a/www/fof/.htaccess +++ b/www/fof/.htaccess @@ -21,7 +21,7 @@ Require all granted # front controller RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d - RewriteRule !\.(pdf|js|mjs|ico|gif|jpg|jpeg|png|webp|svg|css|rar|zip|7z|tar\.gz|map|eot|ttf|otf|woff|woff2)$ index.php [L] + RewriteRule !\.(pdf|js|mjs|ico|svg|css|rar|zip|7z|tar\.gz|map|eot|ttf|otf|woff|woff2)$ index.php [L] # enable gzip compression diff --git a/www/fol/.htaccess b/www/fol/.htaccess index 68f2bae54..fcdc37ee4 100644 --- a/www/fol/.htaccess +++ b/www/fol/.htaccess @@ -21,7 +21,7 @@ Require all granted # front controller RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d - RewriteRule !\.(pdf|js|mjs|ico|gif|jpg|jpeg|png|webp|svg|css|rar|zip|7z|tar\.gz|map|eot|ttf|otf|woff|woff2)$ index.php [L] + RewriteRule !\.(pdf|js|mjs|ico|svg|css|rar|zip|7z|tar\.gz|map|eot|ttf|otf|woff|woff2)$ index.php [L] # enable gzip compression diff --git a/www/fykos/.htaccess b/www/fykos/.htaccess index 43fb51256..cfa7c46ed 100644 --- a/www/fykos/.htaccess +++ b/www/fykos/.htaccess @@ -36,7 +36,7 @@ Require all granted RewriteRule ^o-nas/sin-slavy /o-nas/byvali-organizatori [R=301,L] RewriteRule ^en / [R=301,L] - RewriteRule !\.(pdf|js|mjs|ico|gif|jpg|jpeg|png|webp|svg|css|rar|zip|7z|tar\.gz|map|eot|ttf|otf|woff|woff2|mp4)$ index.php [L] + RewriteRule !\.(pdf|js|mjs|ico|svg|css|rar|zip|7z|tar\.gz|map|eot|ttf|otf|woff|woff2|mp4)$ index.php [L] From d54b143a5ff445463929a00389480bb876c39426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Provazn=C3=ADk?= <41260648+paproc@users.noreply.github.com> Date: Tue, 14 Jan 2025 19:31:26 +0000 Subject: [PATCH 4/4] more specific name --- app/Components/ImageGallery/ImageGalleryControl.php | 2 +- app/Modules/Core/BasePresenter.php | 13 +++---------- app/RouterFactory.php | 10 +++++----- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/app/Components/ImageGallery/ImageGalleryControl.php b/app/Components/ImageGallery/ImageGalleryControl.php index 8cdc2c922..e43f4b8c3 100644 --- a/app/Components/ImageGallery/ImageGalleryControl.php +++ b/app/Components/ImageGallery/ImageGalleryControl.php @@ -43,7 +43,7 @@ public static function getImages(string $path, string $wwwDir): array foreach ($iterator as $file) { $imageInfo = getimagesize($file->getPathname()); $wwwPath = substr($file->getPathname(), strlen($wwwDir)); - if (strpos($wwwPath, '/media/') === 0) { + if (str_starts_with($wwwPath, '/media/')) { $images[] = [ 'src' => $wwwPath, 'width' => $imageInfo[0], diff --git a/app/Modules/Core/BasePresenter.php b/app/Modules/Core/BasePresenter.php index 36a1c63f5..a09f02d37 100644 --- a/app/Modules/Core/BasePresenter.php +++ b/app/Modules/Core/BasePresenter.php @@ -146,18 +146,11 @@ protected function createComponentPdfGallery(): PdfGalleryControl return new PdfGalleryControl($this->getContext()); } - private readonly string $wwwDir; - - public function injectWwwDir(Container $container): void - { - $this->wwwDir = $container->getParameters()['wwwDir']; - } - - public function actionPreview(string $path): void + final public function actionImageGaleryComponentImagePreview(string $path): void { - $basepath = realpath($this->wwwDir . '/media'); + $basepath = realpath($this->context->getParameter('wwwDir') . '/media'); $path = realpath($basepath . '/' . $path); - if ($path === false || strpos($path, $basepath . '/') !== 0) { + if ($path === false || !str_starts_with($path, $basepath . '/')) { $this->error(); } $path = substr($path, strlen($basepath)); diff --git a/app/RouterFactory.php b/app/RouterFactory.php index c947b879c..5306ae023 100755 --- a/app/RouterFactory.php +++ b/app/RouterFactory.php @@ -164,7 +164,7 @@ public static function createFolRouter(?array $domainList, array $routerMapping) $router->withModule('Default') ->addRoute('///media/preview/', [ 'presenter' => 'Default', - 'action' => 'preview', + 'action' => 'imageGaleryComponentImagePreview', ]) ->addRoute('///[/]', [ 'presenter' => 'Default', @@ -189,7 +189,7 @@ public static function createFofRouter(?array $domainList, array $routerMapping) $router->withModule('Default') ->addRoute('///media/preview/', [ 'presenter' => 'Default', - 'action' => 'preview', + 'action' => 'imageGaleryComponentImagePreview', ]) ->addRoute('///(international|erasmus)', [ 'presenter' => 'Erasmus', @@ -212,7 +212,7 @@ public static function createDsefRouter(?array $domainList, array $routerMapping $router->withModule('Default') ->addRoute('///media/preview/', [ 'presenter' => 'Default', - 'action' => 'preview', + 'action' => 'imageGaleryComponentImagePreview', ]) ->addRoute('///[/]', [ 'presenter' => 'Default', @@ -243,7 +243,7 @@ public static function createFykosRouter(?array $domainList, array $routerMappin ]) ->addRoute('///media/preview/', [ 'presenter' => 'Default', - 'action' => 'preview', + 'action' => 'imageGaleryComponentImagePreview', ]); $router->withModule('Default') @@ -301,7 +301,7 @@ public static function createVyfukRouter(?array $domainList, array $routerMappin ]) ->addRoute('///media/preview/', [ 'presenter' => 'Default', - 'action' => 'preview', + 'action' => 'imageGaleryComponentImagePreview', ]) ->addRoute('///[/]', [ 'presenter' => 'Default',