From 9073737e367a84f1dac700cd3649d95f5785d3b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Melaine=20G=C3=A9rard?= Date: Wed, 19 Feb 2025 14:43:19 +0100 Subject: [PATCH] :sparkles: Ajout affichage taille dossier --- src/Controller/FilesController.php | 26 +++++++++++++++++++++----- templates/files/index.html.twig | 2 -- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Controller/FilesController.php b/src/Controller/FilesController.php index 5ddf76f..7cd6b88 100755 --- a/src/Controller/FilesController.php +++ b/src/Controller/FilesController.php @@ -33,7 +33,8 @@ class FilesController extends AbstractController { public function __construct( private readonly EntityManagerInterface $entityManager, - private readonly ParentDirectoryRepository $parentDirectoryRepository + private readonly ParentDirectoryRepository $parentDirectoryRepository, + private readonly Filesystem $defaultAdapter, ) { } @@ -42,7 +43,7 @@ class FilesController extends AbstractController */ #[Route('/', name: 'index')] #[IsGranted('ROLE_USER')] - public function index(Filesystem $defaultAdapter, UrlGeneratorInterface $urlGenerator, #[MapQueryParameter('path')] string $path = ''): Response + public function index(UrlGeneratorInterface $urlGenerator, #[MapQueryParameter('path')] string $path = ''): Response { $path = $this->normalizePath($path); $this->getUser(); @@ -51,7 +52,7 @@ class FilesController extends AbstractController $parentDir = $this->parentDirectoryRepository->findOneBy(['name' => $pathExploded[0]]); - if (null === $parentDir || !$defaultAdapter->directoryExists($path)) { + if (null === $parentDir || !$this->defaultAdapter->directoryExists($path)) { throw $this->createNotFoundException("Ce dossier n'existe pas !"); } @@ -60,7 +61,7 @@ class FilesController extends AbstractController } } - $files = $defaultAdapter->listContents('/' . $path); + $files = $this->defaultAdapter->listContents('/' . $path); $realFiles = []; @@ -87,7 +88,7 @@ class FilesController extends AbstractController 'type' => $file['type'], 'path' => $file['path'], 'last_modified' => $file['lastModified'], - 'size' => $file['fileSize'] ?? null, + 'size' => $file['fileSize'] ?? $this->calculateSize($file), 'url' => 'file' === $file['type'] ? $this->generateUrl('app_files_app_file_proxy', ['filename' => $file['path'], 'preview' => false], UrlGeneratorInterface::ABSOLUTE_URL) : $this->generateUrl('app_files_index', ['path' => $file['path']]), @@ -528,4 +529,19 @@ class FilesController extends AbstractController 'form' => $form->createView(), ]); } + + private function calculateSize($file): int + { + $folderPath = $file['path']; + // On récupère tout les fichiers dans le dossier + $files = $this->defaultAdapter->listContents($folderPath, true); + + $size = 0; + + foreach ($files as $file) { + $size += $file['fileSize'] ?? 0; + } + + return $size; + } } diff --git a/templates/files/index.html.twig b/templates/files/index.html.twig index b1c6f9f..56851b1 100755 --- a/templates/files/index.html.twig +++ b/templates/files/index.html.twig @@ -57,9 +57,7 @@ {{ file.path|basename }} - {% if file.type == 'file' %} {{ file.size|show_size }} - {% endif %} {{ file.last_modified|time_diff }}