Files
ninedad/src/Controller/FileController.php
2025-07-29 08:14:40 +02:00

74 lines
2.3 KiB
PHP

<?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\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}', name: 'app_files', methods: ['GET'])]
public function browse(string $domain, int $id, 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' => true,
]);
} catch (\Exception $e) {
$this->addFlash('danger', $e->getMessage());
return $this->redirectToRoute('app_files', [
'domain' => $domain,
'id' => $id,
'editable' => true,
]);
}
}
#[Route('/user/file/{domain}/{id}/uploadmodal', name: 'app_files_uploadmodal', methods: ['GET'])]
public function upload(string $domain, int $id, Request $request): Response
{
$relativePath = $request->query->get('path', '');
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);
}
}
}