From 7c574fc9a50ffe9f8d17f15dcbc78485faec1395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Melaine=20G=C3=A9rard?= Date: Sat, 11 Jan 2025 18:23:21 +0100 Subject: [PATCH] :sparkles: Ajout de la suppression des fichiers/dossiers --- .env | 2 + config/packages/asset_mapper.yaml | 2 +- config/routes.yaml | 2 +- config/services.yaml | 1 + src/Controller/FilesController.php | 68 +++++++++++++++++++++++++----- templates/files/index.html.twig | 10 ++++- uploads/.gitkeep | 0 7 files changed, 71 insertions(+), 14 deletions(-) delete mode 100755 uploads/.gitkeep diff --git a/.env b/.env index 495d52a..a7fd998 100755 --- a/.env +++ b/.env @@ -39,3 +39,5 @@ MESSENGER_TRANSPORT_DSN=doctrine://default?auto_setup=0 ###> symfony/mailer ### MAILER_DSN=null://null ###< symfony/mailer ### + +BASE_PREFIX=kumora \ No newline at end of file diff --git a/config/packages/asset_mapper.yaml b/config/packages/asset_mapper.yaml index 41ba603..b173c85 100755 --- a/config/packages/asset_mapper.yaml +++ b/config/packages/asset_mapper.yaml @@ -4,7 +4,7 @@ framework: paths: - assets/ missing_import_mode: strict - public_prefix: /kumora/assets + public_prefix: "%env(BASE_PREFIX)%/assets" when@prod: framework: diff --git a/config/routes.yaml b/config/routes.yaml index 25bcff8..45506cc 100755 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -3,4 +3,4 @@ controllers: path: ../src/Controller/ namespace: App\Controller type: attribute - prefix: /kumora + prefix: '%base.prefix%' diff --git a/config/services.yaml b/config/services.yaml index 2d6a76f..974cf96 100755 --- a/config/services.yaml +++ b/config/services.yaml @@ -4,6 +4,7 @@ # Put parameters here that don't need to change on each machine where the app is deployed # https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration parameters: + base.prefix: '%env(BASE_PREFIX)%' services: # default configuration for services in *this* file diff --git a/src/Controller/FilesController.php b/src/Controller/FilesController.php index 02acf9d..5b4d318 100755 --- a/src/Controller/FilesController.php +++ b/src/Controller/FilesController.php @@ -6,9 +6,12 @@ use League\Flysystem\Filesystem; use League\Flysystem\FilesystemException; use League\Flysystem\FilesystemReader; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\File\UploadedFile; +use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; +use Symfony\Component\HttpKernel\Attribute\MapUploadedFile; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\HttpFoundation\HeaderUtils; @@ -24,17 +27,12 @@ class FilesController extends AbstractController #[Route('/', name: 'index')] public function index(Filesystem $defaultAdapter, UrlGeneratorInterface $urlGenerator, #[MapQueryParameter('path')] string $path = ''): Response { - // On retire les slashs en début et fin de chaîne - $path = trim($path, '/'); - // On retire les chemins relatifs - $path = str_replace('..', '', $path); - $path = str_replace('//', '/', $path); + $path = $this->normalizePath($path); if ($path !== '' && !$defaultAdapter->directoryExists($path)) { throw $this->createNotFoundException("Ce dossier n'existe pas !"); } - $files = $defaultAdapter->listContents('/' . $path); $realFiles = []; @@ -71,25 +69,75 @@ class FilesController extends AbstractController #[Route('/file-proxy', name: 'app_file_proxy')] public function fileProxy(Filesystem $defaultAdapter, #[MapQueryParameter('filename')]string $filename) { - $mimetype = $defaultAdapter->mimeType($filename); + $file = $this->normalizePath($filename); + $mimetype = $defaultAdapter->mimeType($file); if ($mimetype === '') { $mimetype = 'application/octet-stream'; } - $response = new StreamedResponse(static function () use ($filename, $defaultAdapter): void { + $response = new StreamedResponse(static function () use ($file, $defaultAdapter): void { $outputStream = fopen('php://output', 'w'); - $fileStream = $defaultAdapter->readStream($filename); + $fileStream = $defaultAdapter->readStream($file); stream_copy_to_stream($fileStream, $outputStream); }); $response->headers->set('Content-Type', $mimetype); $disposition = HeaderUtils::makeDisposition( HeaderUtils::DISPOSITION_ATTACHMENT, - basename($filename) + basename($file) ); $response->headers->set('Content-Disposition', $disposition); return $response; + } + /** + * @throws FilesystemException + */ + #[Route('/file-delete', name: 'delete')] + public function fileDelete(Filesystem $defaultAdapter, #[MapQueryParameter('filename')] string $filename): RedirectResponse + { + $file = $this->normalizePath($filename); + + if ($file !== '' && $defaultAdapter->fileExists($file)) { + $defaultAdapter->delete($file); + + $this->addFlash('success', 'Le fichier a bien été supprimé.'); + } else { + $this->addFlash('error', 'Le fichier n\'existe pas.'); + } + + return $this->redirectToRoute('app_files_index'); + } + + /** + * @throws FilesystemException + */ + #[Route('/directory-delete', name: 'delete_directory')] + public function directoryDelete(Filesystem $defaultAdapter, #[MapQueryParameter('path')] string $path): RedirectResponse + { + $path = $this->normalizePath($path); + + + if ($path !== '' && $defaultAdapter->directoryExists($path)) { + $defaultAdapter->deleteDirectory($path); + + $this->addFlash('success', 'Le dossier a bien été supprimé.'); + } else { + $this->addFlash('error', 'Le dossier n\'existe pas.'); + } + + return $this->redirectToRoute('app_files_index'); + } + + private function normalizePath(string $path): string + { + // On retire les slashs en début et fin de chaîne + $path = trim($path, '/'); + // On retire les chemins relatifs + $path = str_replace('..', '', $path); + $path = str_replace('//', '/', $path); + + return $path; } } diff --git a/templates/files/index.html.twig b/templates/files/index.html.twig index 19fa9c5..308c3b3 100755 --- a/templates/files/index.html.twig +++ b/templates/files/index.html.twig @@ -53,11 +53,17 @@ {% if file.type == 'file' %} - + {% else %} - + {% endif %} diff --git a/uploads/.gitkeep b/uploads/.gitkeep deleted file mode 100755 index e69de29..0000000