This commit is contained in:
2025-07-31 08:04:11 +02:00
parent 327e382694
commit f467b867da
10 changed files with 48 additions and 382 deletions

View File

@ -1,109 +0,0 @@
<?php
namespace App\Controller;
use App\Service\FileService;
use Oneup\UploaderBundle\Uploader\Response\ResponseInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class FileController extends AbstractController
{
private FileService $fileService;
public function __construct(FileService $fileService)
{
$this->fileService = $fileService;
}
#[Route('/user/file/{domain}/{id}/{editable}', name: 'app_files', methods: ['GET'])]
public function browse(string $domain, int $id, int $editable, Request $request): Response
{
$relativePath = $request->query->get('path', '');
try {
$files = $this->fileService->list($domain, (string) $id, $relativePath);
return $this->render('file/browse.html.twig', [
'domain' => $domain,
'id' => $id,
'files' => $files,
'path' => $relativePath,
'editable' => $editable,
]);
} catch (\Exception $e) {
$this->addFlash('danger', $e->getMessage());
dd($e->getMessage());
return $this->redirectToRoute('app_files', [
'domain' => $domain,
'id' => $id,
'editable' => $editable,
]);
}
}
#[Route('/user/uploadmodal/{domain}/{id}', name: 'app_files_uploadmodal', methods: ['GET'])]
public function uploadmodal(string $domain, int $id, Request $request): Response
{
$relativePath = $request->query->get('path', '');
return $this->render('file\upload.html.twig', [
'useheader' => false,
'usemenu' => false,
'usesidebar' => false,
'endpoint' => 'file',
'domain' => $domain,
'id' => $id,
'path' => $relativePath,
]);
}
#[Route('/user/uploadfile', name: 'app_files_uploadfile', methods: ['POST'])]
public function upload(Request $request): Response|ResponseInterface
{
/** @var UploadedFile $file */
$file = $request->files->get('file');
$domain = $request->query->get('domain');
$id = $request->query->get('id');
$relativePath = $request->query->get('path', '');
if (!$file || !$domain || !$id) {
return new Response('Invalid parameters', 400);
}
$baseDir = $this->getParameter('kernel.project_dir').'/uploads/'.$domain.'/'.$id.'/'.ltrim($relativePath, '/');
if (!is_dir($baseDir)) {
mkdir($baseDir, 0775, true);
}
$originalName = $file->getClientOriginalName();
$file->move($baseDir, $originalName);
return new JsonResponse(['success' => true]);
}
#[Route('/user/file/{domain}/{id}/delete', name: 'app_files_delete', methods: ['POST'])]
public function delete(string $domain, int $id, Request $request): JsonResponse
{
$data = json_decode($request->getContent(), true);
$relativePath = $data['path'] ?? null;
if (!$relativePath) {
return $this->json(['error' => 'Chemin non fourni.'], 400);
}
try {
$this->fileService->delete($domain, (string) $id, $relativePath);
return $this->json(['success' => true]);
} catch (\Exception $e) {
return $this->json(['error' => $e->getMessage()], 400);
}
}
}

View File

@ -6,7 +6,7 @@ use App\Entity\Project;
use App\Entity\User;
use App\Form\ProjectType;
use App\Repository\ProjectRepository;
use App\Service\FileService;
use Bnine\FilesBundle\Service\FileService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;

View File

@ -1,101 +0,0 @@
<?php
namespace App\Service;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
class FileService
{
private string $basePath;
private Filesystem $filesystem;
public function __construct(KernelInterface $kernel)
{
$this->filesystem = new Filesystem();
$projectDir = $kernel->getProjectDir(); // chemin racine du projet
$this->basePath = $projectDir.'/uploads';
if (!is_dir($this->basePath)) {
// On crée le dossier uploads s'il n'existe pas
try {
$this->filesystem->mkdir($this->basePath, 0775);
} catch (IOExceptionInterface $e) {
throw new \RuntimeException('Impossible de créer le dossier /uploads : '.$e->getMessage());
}
}
}
/**
* Initialise un répertoire pour une entité (ex: project/123)
*/
public function init(string $domain, string $id): void
{
$entityPath = $this->getEntityPath($domain, $id);
if (!is_dir($entityPath)) {
try {
$this->filesystem->mkdir($entityPath, 0775);
} catch (IOExceptionInterface $e) {
throw new \RuntimeException(sprintf('Impossible de créer le répertoire pour %s/%s : %s', $domain, $id, $e->getMessage()));
}
}
}
/**
* Liste les fichiers dun répertoire lié à une entité (ex: project/123)
*/
public function list(string $domain, string $id, string $relativePath = ''): array
{
$targetPath = $this->getEntityPath($domain, $id).'/'.ltrim($relativePath, '/');
$realPath = realpath($targetPath);
$baseEntityPath = $this->getEntityPath($domain, $id);
if (!$realPath || !str_starts_with($realPath, $baseEntityPath)) {
throw new NotFoundHttpException('Répertoire non autorisé ou inexistant.');
}
$finder = new Finder();
$finder->depth('== 0')->in($realPath);
$results = [];
foreach ($finder as $file) {
$results[] = [
'name' => $file->getFilename(),
'isDirectory' => $file->isDir(),
'path' => ltrim(str_replace($baseEntityPath, '', $file->getRealPath()), '/'),
];
}
return $results;
}
/**
* Supprime un fichier ou dossier (de façon sécurisée)
*/
public function delete(string $domain, string $id, string $relativePath): void
{
$baseEntityPath = $this->getEntityPath($domain, $id);
$targetPath = realpath($baseEntityPath.'/'.ltrim($relativePath, '/'));
if (!$targetPath || !str_starts_with($targetPath, $baseEntityPath)) {
throw new NotFoundHttpException('Fichier ou dossier non autorisé.');
}
try {
$this->filesystem->remove($targetPath);
} catch (IOExceptionInterface $e) {
throw new \RuntimeException('Erreur lors de la suppression : '.$e->getMessage());
}
}
/**
* Construit le chemin absolu dun domaine/id
*/
private function getEntityPath(string $domain, string $id): string
{
return $this->basePath.'/'.$domain.'/'.$id;
}
}