Files
ninedad/src/Controller/FileController.php

74 lines
2.3 KiB
PHP
Raw Normal View History

2025-07-28 17:38:40 +02:00
<?php
namespace App\Controller;
use App\Service\FileService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
2025-07-29 08:14:40 +02:00
use Symfony\Component\HttpFoundation\Response;
2025-07-28 17:38:40 +02:00
use Symfony\Component\Routing\Annotation\Route;
class FileController extends AbstractController
{
2025-07-29 08:14:40 +02:00
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
2025-07-28 17:38:40 +02:00
{
$relativePath = $request->query->get('path', '');
2025-07-29 08:14:40 +02:00
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);
}
2025-07-28 17:38:40 +02:00
try {
2025-07-29 08:14:40 +02:00
$this->fileService->delete($domain, (string) $id, $relativePath);
2025-07-28 17:38:40 +02:00
2025-07-29 08:14:40 +02:00
return $this->json(['success' => true]);
2025-07-28 17:38:40 +02:00
} catch (\Exception $e) {
return $this->json(['error' => $e->getMessage()], 400);
}
}
}