diff --git a/src/Controller/FilesController.php b/src/Controller/FilesController.php index 5b4d318..5e1d645 100755 --- a/src/Controller/FilesController.php +++ b/src/Controller/FilesController.php @@ -2,19 +2,19 @@ namespace App\Controller; +use App\Form\CreateDirectoryType; +use App\Form\RenameType; 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\HeaderUtils; use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Request; 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; use Symfony\Component\Security\Http\Attribute\IsGranted; #[Route('/files', 'app_files_')] @@ -38,7 +38,8 @@ class FilesController extends AbstractController $realFiles = []; foreach ($files as $file) { - if (!str_starts_with($file['path'], '.')) { + $filename = basename($file['path']); + if (!str_starts_with($filename, '.')) { $realFiles[] = [ 'type' => $file['type'], 'path' => $file['path'], @@ -46,7 +47,7 @@ class FilesController extends AbstractController 'size' => $file['fileSize'] ?? null, 'url' => $file['type'] === 'file' ? $this->generateUrl('app_files_app_file_proxy', ['filename' => $file['path']], UrlGeneratorInterface::ABSOLUTE_URL) - : $this->generateUrl('app_files_index', ['path' => $path . '/' . $file['path']]), + : $this->generateUrl('app_files_index', ['path' => $file['path']]), ]; } } @@ -99,7 +100,7 @@ class FilesController extends AbstractController { $file = $this->normalizePath($filename); - if ($file !== '' && $defaultAdapter->fileExists($file)) { + if ($file !== '' && !str_starts_with($file, '.') && $defaultAdapter->fileExists($file)) { $defaultAdapter->delete($file); $this->addFlash('success', 'Le fichier a bien été supprimé.'); @@ -119,7 +120,7 @@ class FilesController extends AbstractController $path = $this->normalizePath($path); - if ($path !== '' && $defaultAdapter->directoryExists($path)) { + if ($path !== '' && !str_starts_with($path, '.') && $defaultAdapter->directoryExists($path)) { $defaultAdapter->deleteDirectory($path); $this->addFlash('success', 'Le dossier a bien été supprimé.'); @@ -130,6 +131,124 @@ class FilesController extends AbstractController return $this->redirectToRoute('app_files_index'); } + /** + * @throws FilesystemException + */ + #[Route('/rename', name: 'rename')] + public function rename(#[MapQueryParameter('path')] string $filepath, Request $request, Filesystem $defaultAdapter): Response + { + $filepath = $this->normalizePath($filepath); + + if ($filepath === '' || str_starts_with($filepath, '.') || !$defaultAdapter->fileExists($filepath)) { + throw $this->createNotFoundException("Ce fichier n'existe pas !"); + } + + $data = [ + 'newName' => pathinfo($filepath, PATHINFO_BASENAME), + ]; + $form = $this->createForm(RenameType::class, $data); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $data = $form->getData(); + + $newName = $data['newName']; + + $newPath = dirname($filepath) . '/' . $newName; + + $defaultAdapter->move($filepath, $newPath); + + $this->addFlash('success', 'Le fichier a bien été renommé.'); + + return $this->redirectToRoute('app_files_index', [ + 'path' => dirname($filepath), + ]); + } + + return $this->render('files/rename.html.twig', [ + 'form' => $form->createView(), + 'filepath' => $filepath, + 'type' => 'fichier', + ]); + } + + /** + * @throws FilesystemException + */ + #[Route('/create-directory', name: 'create_directory')] + public function createDirectory(Request $request, Filesystem $defaultAdapter, #[MapQueryParameter('base')] string $basePath): Response + { + $basePath = $this->normalizePath($basePath); + $form = $this->createForm(CreateDirectoryType::class); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $data = $form->getData(); + + $name = $data['name']; + + $defaultAdapter->createDirectory($basePath . '/' . $name); + + $defaultAdapter->write($basePath . '/' . $name . '/.gitkeep', ''); + + $this->addFlash('success', 'Le dossier a bien été créé.'); + + return $this->redirectToRoute('app_files_index', [ + 'path' => $basePath, + ]); + } + + return $this->render('files/create_directory.html.twig', [ + 'form' => $form->createView(), + 'basePath' => $basePath, + ]); + } + + /** + * @throws FilesystemException + */ + #[Route('/rename-directory', name: 'rename-directory')] + public function renameDirectory(#[MapQueryParameter('path')] string $filepath, Request $request, Filesystem $defaultAdapter): Response + { + $filepath = $this->normalizePath($filepath); + + if ($filepath === '' || str_starts_with($filepath, '.') || !$defaultAdapter->directoryExists($filepath)) { + throw $this->createNotFoundException("Ce dossier n'existe pas !"); + } + + $data = [ + 'newName' => pathinfo($filepath, PATHINFO_BASENAME), + ]; + $form = $this->createForm(RenameType::class, $data); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $data = $form->getData(); + + $newName = $data['newName']; + + $newPath = dirname($filepath) . '/' . $newName; + + $defaultAdapter->move($filepath, $newPath); + + $this->addFlash('success', 'Le dossier a bien été renommé.'); + + return $this->redirectToRoute('app_files_index', [ + 'path' => dirname($filepath), + ]); + } + + return $this->render('files/rename.html.twig', [ + 'form' => $form->createView(), + 'filepath' => $filepath, + 'type' => 'dossier', + ]); + } + + private function normalizePath(string $path): string { // On retire les slashs en début et fin de chaîne diff --git a/src/Form/CreateDirectoryType.php b/src/Form/CreateDirectoryType.php new file mode 100644 index 0000000..9db6f2b --- /dev/null +++ b/src/Form/CreateDirectoryType.php @@ -0,0 +1,22 @@ +add('name', null, [ + 'label' => 'Nom du dossier', + ]) + ->add('submit', SubmitType::class, [ + 'label' => 'Créer', + ]) + ; + } +} \ No newline at end of file diff --git a/src/Form/RenameType.php b/src/Form/RenameType.php new file mode 100644 index 0000000..1d7b78d --- /dev/null +++ b/src/Form/RenameType.php @@ -0,0 +1,29 @@ +add('newName', null, [ + 'label' => 'Nouveau nom', + ]) + ->add('submit', SubmitType::class, [ + 'label' => 'Renommer', + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + ]); + } +} diff --git a/templates/files/create_directory.html.twig b/templates/files/create_directory.html.twig new file mode 100644 index 0000000..a42d360 --- /dev/null +++ b/templates/files/create_directory.html.twig @@ -0,0 +1,15 @@ +{% extends 'base.html.twig' %} + +{% block body %} +
+
+

Créer un dossier dans /{{ basePath }}

+ {{ form(form) }} +
+
+{% endblock %} + +{% block title %} + Créer un dossier +{% endblock %} + diff --git a/templates/files/index.html.twig b/templates/files/index.html.twig index 308c3b3..6900701 100755 --- a/templates/files/index.html.twig +++ b/templates/files/index.html.twig @@ -8,6 +8,11 @@
{{ include('partials/alerts.html.twig') }}
+
+ Créer un dossier +
{% include 'partials/breadbrumb.html.twig' %}
@@ -51,19 +56,22 @@ {% if file.type == 'file' %} - - - + + {% else %} - - + + }) }}" class="hover:text-red-700 duration-300" title="Permet de supprimer le dossier"> {% endif %} diff --git a/templates/files/rename.html.twig b/templates/files/rename.html.twig new file mode 100644 index 0000000..b915bce --- /dev/null +++ b/templates/files/rename.html.twig @@ -0,0 +1,15 @@ +{% extends 'base.html.twig' %} + +{% block body %} +
+
+

Renommer le {{ type }} {{ filepath }}

+ {{ form(form) }} +
+
+{% endblock %} + +{% block title %} + Renommer un {{ type }} +{% endblock %} + diff --git a/templates/partials/breadbrumb.html.twig b/templates/partials/breadbrumb.html.twig index 39fb689..8b5f4ba 100755 --- a/templates/partials/breadbrumb.html.twig +++ b/templates/partials/breadbrumb.html.twig @@ -8,7 +8,9 @@ {% if path != '' %} {% set pathSplitted = path|split('/') %} + {% set base = '' %} {% for pa in pathSplitted %} + {% set base = base ~ '/' ~ pa %} {% if loop.last %}
  • @@ -22,7 +24,7 @@
  • diff --git a/uploads/.gitkeep b/uploads/.gitkeep new file mode 100644 index 0000000..e69de29