Ajout de l'upload de fichier

This commit is contained in:
Melaine Gérard 2025-01-11 19:52:25 +01:00
parent 327c354ac4
commit 800dbc2686
9 changed files with 196 additions and 4 deletions

View File

@ -1,5 +1,14 @@
{ {
"controllers": { "controllers": {
"@symfony/ux-dropzone": {
"dropzone": {
"enabled": true,
"fetch": "eager",
"autoimport": {
"@symfony/ux-dropzone/dist/style.min.css": true
}
}
},
"@symfony/ux-turbo": { "@symfony/ux-turbo": {
"turbo-core": { "turbo-core": {
"enabled": true, "enabled": true,

View File

@ -15,7 +15,7 @@
"oneup/flysystem-bundle": "^4.12.3", "oneup/flysystem-bundle": "^4.12.3",
"phpdocumentor/reflection-docblock": "^5.6.1", "phpdocumentor/reflection-docblock": "^5.6.1",
"phpstan/phpdoc-parser": "^2.0", "phpstan/phpdoc-parser": "^2.0",
"symfony/apache-pack": "^1.0", "symfony/apache-pack": "^1.0.1",
"symfony/asset": "7.2.*", "symfony/asset": "7.2.*",
"symfony/asset-mapper": "7.2.*", "symfony/asset-mapper": "7.2.*",
"symfony/console": "7.2.*", "symfony/console": "7.2.*",
@ -43,6 +43,7 @@
"symfony/translation": "7.2.*", "symfony/translation": "7.2.*",
"symfony/twig-bundle": "7.2.*", "symfony/twig-bundle": "7.2.*",
"symfony/uid": "7.2.*", "symfony/uid": "7.2.*",
"symfony/ux-dropzone": "^2.22.1",
"symfony/ux-icons": "^2.22.1", "symfony/ux-icons": "^2.22.1",
"symfony/ux-turbo": "^2.22.1", "symfony/ux-turbo": "^2.22.1",
"symfony/ux-twig-component": "^2.22.1", "symfony/ux-twig-component": "^2.22.1",

81
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "3032276cf7af61e3028a0f94b2488b8d", "content-hash": "34cf52bb9dcb8783d8297d6634221de2",
"packages": [ "packages": [
{ {
"name": "composer/semver", "name": "composer/semver",
@ -7420,6 +7420,85 @@
], ],
"time": "2024-09-25T14:21:43+00:00" "time": "2024-09-25T14:21:43+00:00"
}, },
{
"name": "symfony/ux-dropzone",
"version": "v2.22.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/ux-dropzone.git",
"reference": "4debd59383bd98141c90bef5a98107710117af80"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/ux-dropzone/zipball/4debd59383bd98141c90bef5a98107710117af80",
"reference": "4debd59383bd98141c90bef5a98107710117af80",
"shasum": ""
},
"require": {
"php": ">=8.1",
"symfony/config": "^5.4|^6.0|^7.0",
"symfony/dependency-injection": "^5.4|^6.0|^7.0",
"symfony/form": "^5.4|^6.0|^7.0",
"symfony/http-kernel": "^5.4|^6.0|^7.0",
"symfony/options-resolver": "^5.4|^6.0|^7.0"
},
"require-dev": {
"symfony/framework-bundle": "^5.4|^6.0|^7.0",
"symfony/phpunit-bridge": "^5.4|^6.0|^7.0",
"symfony/twig-bundle": "^5.4|^6.0|^7.0",
"symfony/var-dumper": "^5.4|^6.0|^7.0",
"twig/twig": "^2.14.7|^3.0.4"
},
"type": "symfony-bundle",
"extra": {
"thanks": {
"url": "https://github.com/symfony/ux",
"name": "symfony/ux"
}
},
"autoload": {
"psr-4": {
"Symfony\\UX\\Dropzone\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Titouan Galopin",
"email": "galopintitouan@gmail.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "File input dropzones for Symfony Forms",
"homepage": "https://symfony.com",
"keywords": [
"symfony-ux"
],
"support": {
"source": "https://github.com/symfony/ux-dropzone/tree/v2.22.1"
},
"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-12-05T16:05:57+00:00"
},
{ {
"name": "symfony/ux-icons", "name": "symfony/ux-icons",
"version": "v2.22.1", "version": "v2.22.1",

View File

@ -19,4 +19,5 @@ return [
Symfony\UX\TwigComponent\TwigComponentBundle::class => ['all' => true], Symfony\UX\TwigComponent\TwigComponentBundle::class => ['all' => true],
TalesFromADev\Twig\Extra\Tailwind\Bridge\Symfony\Bundle\TalesFromADevTwigExtraTailwindBundle::class => ['all' => true], TalesFromADev\Twig\Extra\Tailwind\Bridge\Symfony\Bundle\TalesFromADevTwigExtraTailwindBundle::class => ['all' => true],
TalesFromADev\FlowbiteBundle\TalesFromADevFlowbiteBundle::class => ['all' => true], TalesFromADev\FlowbiteBundle\TalesFromADevFlowbiteBundle::class => ['all' => true],
Symfony\UX\Dropzone\DropzoneBundle::class => ['all' => true],
]; ];

View File

@ -4,9 +4,11 @@ namespace App\Controller;
use App\Form\CreateDirectoryType; use App\Form\CreateDirectoryType;
use App\Form\RenameType; use App\Form\RenameType;
use App\Form\UploadType;
use League\Flysystem\Filesystem; use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemException; use League\Flysystem\FilesystemException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -108,7 +110,9 @@ class FilesController extends AbstractController
$this->addFlash('error', 'Le fichier n\'existe pas.'); $this->addFlash('error', 'Le fichier n\'existe pas.');
} }
return $this->redirectToRoute('app_files_index'); return $this->redirectToRoute('app_files_index', [
'path' => dirname($file),
]);
} }
/** /**
@ -128,7 +132,9 @@ class FilesController extends AbstractController
$this->addFlash('error', 'Le dossier n\'existe pas.'); $this->addFlash('error', 'Le dossier n\'existe pas.');
} }
return $this->redirectToRoute('app_files_index'); return $this->redirectToRoute('app_files_index', [
'path' => dirname($path),
]);
} }
/** /**
@ -248,6 +254,46 @@ class FilesController extends AbstractController
]); ]);
} }
/**
* @throws FilesystemException
*/
#[Route('/upload', name: 'upload')]
public function upload(#[MapQueryParameter('path')] string $path, Request $request, Filesystem $defaultAdapter): Response
{
$path = $this->normalizePath($path);
$form = $this->createForm(UploadType::class);
if ($path !== '' && !$defaultAdapter->directoryExists($path)) {
throw $this->createNotFoundException("Ce dossier n'existe pas !");
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$files = $data['files'];
/**
* @var UploadedFile $file
*/
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$defaultAdapter->write($path . '/' . $filename, $file->getContent());
}
$this->addFlash('success', 'Les ' . count($files) . ' fichiers ont bien été envoyés.');
return $this->redirectToRoute('app_files_index', [
'path' => $path,
]);
}
return $this->render('files/upload.html.twig', [
'form' => $form->createView(),
'path' => $path,
]);
}
private function normalizePath(string $path): string private function normalizePath(string $path): string
{ {

35
src/Form/UploadType.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\UX\Dropzone\Form\DropzoneType;
class UploadType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('files', DropzoneType::class, [
'label' => 'Fichiers à envoyer',
'attr' => [
'placeholder' => 'Déposez vos fichiers ici',
],
'multiple' => true,
])
->add('submit', SubmitType::class, [
'label' => 'Uploader',
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
]);
}
}

View File

@ -295,6 +295,9 @@
"ref": "0df5844274d871b37fc3816c57a768ffc60a43a5" "ref": "0df5844274d871b37fc3816c57a768ffc60a43a5"
} }
}, },
"symfony/ux-dropzone": {
"version": "v2.22.1"
},
"symfony/ux-icons": { "symfony/ux-icons": {
"version": "2.22", "version": "2.22",
"recipe": { "recipe": {

View File

@ -12,6 +12,9 @@
<a href="{{ path('app_files_create_directory', { <a href="{{ path('app_files_create_directory', {
base: path base: path
}) }}" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">Créer un dossier</a> }) }}" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">Créer un dossier</a>
<a href="{{ path('app_files_upload', {
path: path
}) }}" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800">Ajouter des fichiers</a>
</div> </div>
<div class="mt-4"> <div class="mt-4">
{% include 'partials/breadbrumb.html.twig' %} {% include 'partials/breadbrumb.html.twig' %}

View File

@ -0,0 +1,15 @@
{% extends 'base.html.twig' %}
{% block body %}
<div class="container mx-auto px-16 mt-4">
<div class="block p-6 bg-white border border-gray-200 rounded-lg shadow dark:bg-gray-800 dark:border-gray-700">
<h3 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">Ajouter des fichiers dans /{{ path }}</h3>
{{ form(form) }}
</div>
</div>
{% endblock %}
{% block title %}
Ajouter des fichiers
{% endblock %}