This commit is contained in:
2025-07-28 17:38:40 +02:00
parent 7d55ac027a
commit 5762df8df9
12 changed files with 89 additions and 713 deletions

View File

@ -0,0 +1,26 @@
<?php
namespace App\Controller;
use App\Service\FileService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class FileController extends AbstractController
{
#[Route('/user/file/{id}', name: 'app_files', methods: ['GET'])]
public function browse(int $id, Request $request, FileService $fileService): JsonResponse
{
$relativePath = $request->query->get('path', '');
try {
$files = $fileService->list($id, '');
return $this->json(['files' => $files]);
} catch (\Exception $e) {
return $this->json(['error' => $e->getMessage()], 400);
}
}
}

View File

@ -12,6 +12,7 @@ use Doctrine\ORM\Mapping as ORM;
class Project
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
@ -40,13 +41,6 @@ class Project
return $this->id;
}
public function setId(int $id): static
{
$this->id = $id;
return $this;
}
public function getTitle(): ?string
{
return $this->title;

View File

@ -6,6 +6,7 @@ use App\Entity\Project;
use App\Entity\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
@ -25,6 +26,10 @@ class ProjectType extends AbstractType
'label' => 'Titre',
])
->add('status', ChoiceType::class, [
'choices' => ['Brouillon' => 0],
])
->add('users', EntityType::class, [
'label' => 'Propriétaires',
'class' => User::class,

View File

@ -0,0 +1,48 @@
<?php
namespace App\Service;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class FileService
{
private string $basePath;
public function __construct()
{
// Répertoire en dur dans le projet, pas dans /public
$this->basePath = realpath(__DIR__.'/../../uploads');
if (!$this->basePath) {
throw new \RuntimeException('Répertoire /uploads introuvable.');
}
}
/**
* Liste les fichiers/dossiers pour un projet donné
*/
public function list(string $projectId, string $relativePath = ''): array
{
$targetPath = $this->basePath.'/'.$projectId.'/'.ltrim($relativePath, '/');
$realPath = realpath($targetPath);
// Sécurité : protection contre les accès hors du dossier projet
if (!$realPath || !str_starts_with($realPath, $this->basePath.'/'.$projectId)) {
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($this->basePath.'/'.$projectId, '', $file->getRealPath()), '/'),
];
}
return $results;
}
}