diff --git a/src/Controller/ImportController.php b/src/Controller/ImportController.php new file mode 100644 index 0000000..15a292f --- /dev/null +++ b/src/Controller/ImportController.php @@ -0,0 +1,66 @@ +files->get('file'); + + if (!$file) { + return $this->json(['error' => 'No file provided.'], Response::HTTP_UNPROCESSABLE_ENTITY); + } + + if ('csv' !== $file->getClientOriginalExtension()) { + return $this->json(['error' => 'Only CSV files are accepted.'], Response::HTTP_UNPROCESSABLE_ENTITY); + } + + if ($file->getSize() > 5 * 1024 * 1024) { + return $this->json(['error' => 'File too large (max 5 MB).'], Response::HTTP_UNPROCESSABLE_ENTITY); + } + + /** @var User $user */ + $user = $this->getUser(); + + $import = new Import(); + $import->setUser($user); + + $em->persist($import); + $em->flush(); + + $filePath = sprintf('imports/%d/%d.csv', $user->getId(), $import->getId()); + $defaultStorage->write($filePath, file_get_contents($file->getPathname())); + + $import->setFilePath($filePath); + $em->flush(); + + $bus->dispatch(new ProcessImportMessage($import->getId())); + + return $this->json([ + 'id' => $import->getId(), + 'status' => $import->getStatus(), + ], Response::HTTP_CREATED); + } +}