Added:
- manga import - read from cbz - save cbz from scrapping - menu interactions
This commit is contained in:
197
src/Controller/ImportController.php
Normal file
197
src/Controller/ImportController.php
Normal file
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Repository\ChapterRepository;
|
||||
use App\Repository\MangaRepository;
|
||||
use App\Service\CbzService;
|
||||
use App\Service\MangaImportService;
|
||||
use App\Service\NotificationService;
|
||||
use Exception;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\String\Slugger\SluggerInterface;
|
||||
|
||||
class ImportController extends AbstractController
|
||||
{
|
||||
private const UPLOADS_DIRECTORY = 'public/uploads';
|
||||
|
||||
public function __construct(
|
||||
private readonly string $projectDir,
|
||||
private readonly CbzService $cbzService,
|
||||
private readonly MangaImportService $mangaImportService,
|
||||
// private SluggerInterface $slugger,
|
||||
private NotificationService $notificationService,
|
||||
private MangaRepository $mangaRepository,
|
||||
private ChapterRepository $chapterRepository
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#[Route('/import', name: 'app_import')]
|
||||
public function index(Request $request, SessionInterface $session): Response
|
||||
{
|
||||
if ($request->isMethod('post')) {
|
||||
$file = $request->files->get('file');
|
||||
if ($file && $file->getClientOriginalExtension() === 'cbz') {
|
||||
$originalFileName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
|
||||
$filename = uniqid() . '.' . $file->getClientOriginalExtension();
|
||||
|
||||
try {
|
||||
$file->move($this->projectDir . '/' . self::UPLOADS_DIRECTORY, $filename);
|
||||
$session->set('import_file_path', $this->projectDir . '/' .self::UPLOADS_DIRECTORY . '/' . $filename);
|
||||
$session->set('import_original_file_name', $originalFileName);
|
||||
return $this->redirectToRoute('import_match');
|
||||
} catch (FileException $e) {
|
||||
$this->notificationService->sendUpdate([
|
||||
'type' => 'error',
|
||||
'message' => 'Une erreur est survenue lors de l\'import du fichier.'
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
$this->notificationService->sendUpdate([
|
||||
'type' => 'error',
|
||||
'message' => 'Le fichier doit être au format CBZ.'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('import/index.html.twig');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
#[Route('/import/match', name: 'import_match')]
|
||||
public function match(Request $request, SessionInterface $session): Response
|
||||
{
|
||||
$filePath = $session->get('import_file_path');
|
||||
$originalFileName = $session->get('import_original_file_name');
|
||||
if (!$filePath || !$originalFileName) {
|
||||
return $this->redirectToRoute('app_import');
|
||||
}
|
||||
|
||||
$metadata = $this->cbzService->extractMetadata($filePath, $originalFileName);
|
||||
if($metadata['title'] === '' || is_null($metadata['title'])){
|
||||
$this->notificationService->sendUpdate([
|
||||
'type' => 'error',
|
||||
'message' => 'Impossible de détecter le titre du manga.'
|
||||
]);
|
||||
return $this->redirectToRoute('app_import');
|
||||
}
|
||||
|
||||
$mangas = $this->mangaRepository->findBySlug($metadata['title']);
|
||||
|
||||
$mangasChapters = [];
|
||||
foreach ($mangas as $manga) {
|
||||
if(!is_null($metadata['chapter'])){
|
||||
$chapters = $this->chapterRepository->findBy([
|
||||
'manga' => $manga,
|
||||
'number' => $metadata['chapter']
|
||||
]);
|
||||
$chapters = [$chapters[0]->getVolume() => $chapters];
|
||||
}else{
|
||||
$chapters = $this->chapterRepository->findBy([
|
||||
'manga' => $manga,
|
||||
'volume' => (int) $metadata['volume']
|
||||
]);
|
||||
|
||||
$chapters = [$metadata['volume'] => $chapters];
|
||||
}
|
||||
$mangasChapters[$manga->getSlug()] = $chapters;
|
||||
}
|
||||
|
||||
if(empty($mangas)) {
|
||||
$this->notificationService->sendUpdate([
|
||||
'type' => 'error',
|
||||
'message' => 'Aucun manga trouvé avec ce titre.'
|
||||
]);
|
||||
return $this->redirectToRoute('app_manga_new', ['query' => $metadata['title']]);
|
||||
}
|
||||
|
||||
if ($request->isMethod('post')) {
|
||||
$session->set('import_metadata', $request->request->all());
|
||||
return $this->redirectToRoute('import_confirm');
|
||||
}
|
||||
|
||||
return $this->render('import/match.html.twig', [
|
||||
'mangas' => $mangas,
|
||||
'volume' => $metadata['volume'],
|
||||
'chapters' => $mangasChapters
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/import/confirm', name: 'import_confirm')]
|
||||
public function confirm(Request $request, SessionInterface $session): Response
|
||||
{
|
||||
if (!$request->isMethod('POST')) {
|
||||
return $this->redirectToRoute('app_import');
|
||||
}
|
||||
|
||||
$action = $request->request->get('action');
|
||||
$mangaSlug = $request->request->get('manga_slug');
|
||||
$volume = $request->request->get('volume');
|
||||
|
||||
if ($action === 'confirm') {
|
||||
// Logique de confirmation
|
||||
$manga = $this->mangaRepository->findOneBy(['slug' => $mangaSlug]);
|
||||
if (!$manga) {
|
||||
$this->notificationService->sendUpdate([
|
||||
'type' => 'error',
|
||||
'message' => 'Manga non trouvé.'
|
||||
]);
|
||||
return $this->redirectToRoute('app_import');
|
||||
}
|
||||
|
||||
$filePath = $session->get('import_file_path');
|
||||
if (!$filePath) {
|
||||
$this->notificationService->sendUpdate([
|
||||
'type' => 'error',
|
||||
'message' => 'Fichier d\'import non trouvé.'
|
||||
]);
|
||||
return $this->redirectToRoute('app_import');
|
||||
}
|
||||
$originalFileName = $session->get('import_original_file_name');
|
||||
|
||||
// Ici, vous pouvez ajouter la logique pour importer effectivement le fichier
|
||||
// Par exemple :
|
||||
// $this->mangaImportService->importVolume($manga, $volume, $filePath);
|
||||
|
||||
try {
|
||||
$this->mangaImportService->importVolume($manga, (int)$volume, $filePath, $originalFileName);
|
||||
} catch (\Exception $e) {
|
||||
$this->notificationService->sendUpdate([
|
||||
'type' => 'error',
|
||||
'message' => 'Erreur lors de l\'import : ' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
|
||||
$this->notificationService->sendUpdate([
|
||||
'type' => 'success',
|
||||
'message' => 'Import confirmé avec succès.'
|
||||
]);
|
||||
|
||||
return $this->redirectToRoute('app_manga_show', ['mangaSlug' => $mangaSlug]);
|
||||
} elseif ($action === 'refuse') {
|
||||
// Logique de refus
|
||||
$filePath = $session->get('import_file_path');
|
||||
if ($filePath && file_exists($filePath)) {
|
||||
unlink($filePath); // Supprime le fichier temporaire
|
||||
}
|
||||
$session->remove('import_file_path');
|
||||
$session->remove('import_original_file_name');
|
||||
|
||||
$this->notificationService->sendUpdate([
|
||||
'type' => 'info',
|
||||
'message' => 'Import refusé. Le fichier a été supprimé.'
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_import');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user