2026-03-29 08:21:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
|
|
use App\Entity\Import;
|
|
|
|
|
use App\Entity\User;
|
|
|
|
|
use App\Message\ProcessImportMessage;
|
2026-03-31 19:34:05 +00:00
|
|
|
use App\Repository\ImportRepository;
|
2026-03-29 08:21:02 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
use League\Flysystem\FilesystemOperator;
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
|
|
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
|
|
|
|
|
|
|
|
class ImportController extends AbstractController
|
|
|
|
|
{
|
2026-03-31 19:34:05 +00:00
|
|
|
#[Route('/api/imports/latest', methods: ['GET'])]
|
|
|
|
|
#[IsGranted('ROLE_USER')]
|
|
|
|
|
public function latest(ImportRepository $importRepository): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
/** @var User $user */
|
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
|
|
|
|
|
$import = $importRepository->findLatestForUser($user);
|
|
|
|
|
|
|
|
|
|
if (!$import) {
|
|
|
|
|
return $this->json(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->json([
|
|
|
|
|
'id' => $import->getId(),
|
|
|
|
|
'status' => $import->getStatus(),
|
|
|
|
|
'totalFilms' => $import->getTotalFilms(),
|
2026-04-01 17:30:15 +00:00
|
|
|
'processedFilms' => $import->getProcessedFilms(),
|
2026-03-31 19:34:05 +00:00
|
|
|
'failedFilms' => $import->getFailedFilms(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 08:21:02 +00:00
|
|
|
#[Route('/api/imports', methods: ['POST'])]
|
|
|
|
|
#[IsGranted('ROLE_USER')]
|
|
|
|
|
public function create(
|
|
|
|
|
Request $request,
|
|
|
|
|
FilesystemOperator $defaultStorage,
|
|
|
|
|
EntityManagerInterface $em,
|
2026-03-31 19:34:05 +00:00
|
|
|
ImportRepository $importRepository,
|
2026-03-29 08:21:02 +00:00
|
|
|
MessageBusInterface $bus,
|
|
|
|
|
): JsonResponse {
|
2026-03-31 19:34:05 +00:00
|
|
|
/** @var User $user */
|
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
|
|
|
|
|
if ($importRepository->hasActiveImport($user)) {
|
|
|
|
|
return $this->json(['error' => 'Un import est déjà en cours.'], Response::HTTP_CONFLICT);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 08:21:02 +00:00
|
|
|
$file = $request->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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$import = new Import();
|
|
|
|
|
$import->setUser($user);
|
2026-03-29 08:27:57 +00:00
|
|
|
$import->setFilePath('pending');
|
2026-03-29 08:21:02 +00:00
|
|
|
|
|
|
|
|
$em->persist($import);
|
|
|
|
|
$em->flush();
|
|
|
|
|
|
2026-04-05 18:37:26 +00:00
|
|
|
try {
|
|
|
|
|
$filePath = sprintf('imports/%d/%d.csv', $user->getId(), $import->getId());
|
|
|
|
|
$defaultStorage->write($filePath, file_get_contents($file->getPathname()));
|
2026-03-29 08:21:02 +00:00
|
|
|
|
2026-04-05 18:37:26 +00:00
|
|
|
$import->setFilePath($filePath);
|
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
|
|
$bus->dispatch(new ProcessImportMessage($import->getId()));
|
|
|
|
|
} catch (\Throwable) {
|
|
|
|
|
$import->setStatus(Import::STATUS_FAILED);
|
|
|
|
|
$em->flush();
|
2026-03-29 08:21:02 +00:00
|
|
|
|
2026-04-05 18:37:26 +00:00
|
|
|
return $this->json(['error' => 'An error occurred while starting the import.'], Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
|
}
|
2026-03-29 08:21:02 +00:00
|
|
|
|
|
|
|
|
return $this->json([
|
|
|
|
|
'id' => $import->getId(),
|
|
|
|
|
'status' => $import->getStatus(),
|
|
|
|
|
], Response::HTTP_CREATED);
|
|
|
|
|
}
|
|
|
|
|
}
|