From c1e1ab081f3c3ddd29fae2d57d084f976b753e20 Mon Sep 17 00:00:00 2001 From: jank1619 Date: Fri, 20 Dec 2024 07:03:38 +0000 Subject: [PATCH 1/5] style(home.html.twig): adjust centering of container element (#2) Co-authored-by: Jan Klattenhoff Reviewed-on: https://git.simonis.lol/jank1619/file-explorer/pulls/2 --- templates/home.html.twig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/home.html.twig b/templates/home.html.twig index f450b25..a84c679 100644 --- a/templates/home.html.twig +++ b/templates/home.html.twig @@ -3,7 +3,7 @@ {% block title %}Home{% endblock %} {% block body %} -
+
{% include '_partials/_table.html.twig' %}
-{% endblock %} \ No newline at end of file +{% endblock %} -- 2.47.2 From b189753c1e86c25b0dbf22c672ac759a496d76ff Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Sat, 21 Dec 2024 14:49:27 +0100 Subject: [PATCH 2/5] feat: add ServeFileController to serve files dynamically --- composer.json | 1 + composer.lock | 169 ++++++++++++++++++++++++- src/Controller/ServeFileController.php | 29 +++++ 3 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 src/Controller/ServeFileController.php diff --git a/composer.json b/composer.json index 4460c9e..b80b360 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "symfony/flex": "^2", "symfony/form": "^7.1", "symfony/framework-bundle": "7.1.*", + "symfony/mime": "7.1.*", "symfony/runtime": "7.1.*", "symfony/stimulus-bundle": "^2.22", "symfony/twig-bundle": "7.1.*", diff --git a/composer.lock b/composer.lock index 8706be0..432b350 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8278464727736866734f277693979fb2", + "content-hash": "46ba6e6e4f31d38d71c65f5acfdff136", "packages": [ { "name": "composer/semver", @@ -1993,6 +1993,90 @@ ], "time": "2024-11-13T14:25:32+00:00" }, + { + "name": "symfony/mime", + "version": "v7.1.6", + "source": { + "type": "git", + "url": "https://github.com/symfony/mime.git", + "reference": "caa1e521edb2650b8470918dfe51708c237f0598" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/mime/zipball/caa1e521edb2650b8470918dfe51708c237f0598", + "reference": "caa1e521edb2650b8470918dfe51708c237f0598", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "symfony/polyfill-intl-idn": "^1.10", + "symfony/polyfill-mbstring": "^1.0" + }, + "conflict": { + "egulias/email-validator": "~3.0.0", + "phpdocumentor/reflection-docblock": "<3.2.2", + "phpdocumentor/type-resolver": "<1.4.0", + "symfony/mailer": "<6.4", + "symfony/serializer": "<6.4.3|>7.0,<7.0.3" + }, + "require-dev": { + "egulias/email-validator": "^2.1.10|^3.1|^4", + "league/html-to-markdown": "^5.0", + "phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0", + "symfony/dependency-injection": "^6.4|^7.0", + "symfony/process": "^6.4|^7.0", + "symfony/property-access": "^6.4|^7.0", + "symfony/property-info": "^6.4|^7.0", + "symfony/serializer": "^6.4.3|^7.0.3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Mime\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Allows manipulating MIME messages", + "homepage": "https://symfony.com", + "keywords": [ + "mime", + "mime-type" + ], + "support": { + "source": "https://github.com/symfony/mime/tree/v7.1.6" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-10-25T15:11:02+00:00" + }, { "name": "symfony/options-resolver", "version": "v7.1.9", @@ -2222,6 +2306,89 @@ ], "time": "2024-09-09T11:45:10+00:00" }, + { + "name": "symfony/polyfill-intl-idn", + "version": "v1.31.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-intl-idn.git", + "reference": "c36586dcf89a12315939e00ec9b4474adcb1d773" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-intl-idn/zipball/c36586dcf89a12315939e00ec9b4474adcb1d773", + "reference": "c36586dcf89a12315939e00ec9b4474adcb1d773", + "shasum": "" + }, + "require": { + "php": ">=7.2", + "symfony/polyfill-intl-normalizer": "^1.10" + }, + "suggest": { + "ext-intl": "For best performance" + }, + "type": "library", + "extra": { + "thanks": { + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Intl\\Idn\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Laurent Bassin", + "email": "laurent@bassin.info" + }, + { + "name": "Trevor Rowbotham", + "email": "trevor.rowbotham@pm.me" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "idn", + "intl", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-intl-idn/tree/v1.31.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-09-09T11:45:10+00:00" + }, { "name": "symfony/polyfill-intl-normalizer", "version": "v1.31.0", diff --git a/src/Controller/ServeFileController.php b/src/Controller/ServeFileController.php new file mode 100644 index 0000000..a5e1fcb --- /dev/null +++ b/src/Controller/ServeFileController.php @@ -0,0 +1,29 @@ +fileSystemService->getFile($filePath); + $path = $file->getPath() . '/' . $file->getName(); + + $response = new BinaryFileResponse($path); + $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $file->getName()); + + return $response; + } +} -- 2.47.2 From 54bcfd3775b0b0097667310ecd0fc0deffcfc86c Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Sat, 21 Dec 2024 14:57:16 +0100 Subject: [PATCH 3/5] build(composer): add ext-fileinfo to composer dependencies --- composer.json | 3 ++- src/Controller/ServeFileController.php | 1 - src/Objects/DirContent.php | 9 ++++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index b80b360..e26274b 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "symfony/yaml": "7.1.*", "symfonycasts/tailwind-bundle": "^0.6.1", "twig/extra-bundle": "^2.12|^3.0", - "twig/twig": "^2.12|^3.0" + "twig/twig": "^2.12|^3.0", + "ext-fileinfo": "*" }, "config": { "allow-plugins": { diff --git a/src/Controller/ServeFileController.php b/src/Controller/ServeFileController.php index a5e1fcb..22e535c 100644 --- a/src/Controller/ServeFileController.php +++ b/src/Controller/ServeFileController.php @@ -5,7 +5,6 @@ namespace App\Controller; use App\Service\FileSystemService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\BinaryFileResponse; -use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\Routing\Attribute\Route; diff --git a/src/Objects/DirContent.php b/src/Objects/DirContent.php index 91d8d01..3079713 100644 --- a/src/Objects/DirContent.php +++ b/src/Objects/DirContent.php @@ -14,6 +14,7 @@ readonly class DirContent private string $type, private string $path, private string $content, + private string $mimeType, ) { } @@ -24,7 +25,8 @@ readonly class DirContent $fileInfo->getSize() ?? 0, $fileInfo->getType() ?? 'N/A', $fileInfo->getPath(), - $content + $content, + mime_content_type($fileInfo->getFilename()), ); } @@ -57,6 +59,11 @@ readonly class DirContent return $this->content; } + public function getMimeType(): string + { + return $this->mimeType; + } + private function getHumanReadableSize(): string { $bytes = $this->size; -- 2.47.2 From 987e49cdec03431edea932d93ae2f99f76c0c1e1 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Sat, 21 Dec 2024 17:11:01 +0100 Subject: [PATCH 4/5] feat: add image rendering in file display template --- src/Objects/DirContent.php | 2 +- templates/file.html.twig | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Objects/DirContent.php b/src/Objects/DirContent.php index 3079713..1e1a83c 100644 --- a/src/Objects/DirContent.php +++ b/src/Objects/DirContent.php @@ -26,7 +26,7 @@ readonly class DirContent $fileInfo->getType() ?? 'N/A', $fileInfo->getPath(), $content, - mime_content_type($fileInfo->getFilename()), + mime_content_type($fileInfo->getPath() . '/' . $fileInfo->getFilename()), ); } diff --git a/templates/file.html.twig b/templates/file.html.twig index 6ed481f..81be9b4 100644 --- a/templates/file.html.twig +++ b/templates/file.html.twig @@ -15,7 +15,11 @@
-

{{ file.content|raw }}

+ {% if file.mimeType starts with 'image' %} + + {% else %} +

{{ file.content|raw }}

+ {% endif %}
{% endblock %} \ No newline at end of file -- 2.47.2 From d6aefc423c2f4018c2ccb3fc4630947e941cfc7f Mon Sep 17 00:00:00 2001 From: jank1619 Date: Sat, 21 Dec 2024 17:37:05 +0000 Subject: [PATCH 5/5] Update src/Controller/ServeFileController.php --- src/Controller/ServeFileController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/ServeFileController.php b/src/Controller/ServeFileController.php index 22e535c..0db9c3d 100644 --- a/src/Controller/ServeFileController.php +++ b/src/Controller/ServeFileController.php @@ -14,7 +14,7 @@ class ServeFileController extends AbstractController { } - #[Route("/serve-file/{filePath}", name: "serve_file")] + #[Route("/serve/{filePath}", name: "serve_file")] public function __invoke(string $filePath): BinaryFileResponse { $file = $this->fileSystemService->getFile($filePath); -- 2.47.2