diff --git a/config/packages/oneup_uploader.yaml b/config/packages/oneup_uploader.yaml index e8c97c1..fc316f6 100644 --- a/config/packages/oneup_uploader.yaml +++ b/config/packages/oneup_uploader.yaml @@ -3,4 +3,6 @@ oneup_uploader: avatar: frontend: dropzone logo: - frontend: dropzone \ No newline at end of file + frontend: dropzone + file: + frontend: dropzone \ No newline at end of file diff --git a/src/Controller/FileController.php b/src/Controller/FileController.php index d3c8d85..4e7c0a4 100644 --- a/src/Controller/FileController.php +++ b/src/Controller/FileController.php @@ -3,7 +3,9 @@ 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; @@ -45,11 +47,44 @@ class FileController extends AbstractController } } - #[Route('/user/file/{domain}/{id}/uploadmodal', name: 'app_files_uploadmodal', methods: ['GET'])] - public function upload(string $domain, int $id, Request $request): Response + #[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]); } diff --git a/templates/file/browse.html.twig b/templates/file/browse.html.twig index 0e6d996..f6f6164 100644 --- a/templates/file/browse.html.twig +++ b/templates/file/browse.html.twig @@ -11,11 +11,7 @@ {% if editable %}