- CbrToCbzConverter.php
- import now convert .cbr to .cbz
- import improvement, multiple files
This commit is contained in:
Jérémy Guillot
2024-07-24 14:10:28 +02:00
parent 4484be4d4e
commit 7068bd1a34
14 changed files with 547 additions and 238 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Service;
use App\Entity\Chapter;
use App\Entity\Manga;
use App\Repository\ChapterRepository;
use Doctrine\ORM\EntityManagerInterface;
@@ -18,7 +19,6 @@ class MangaImportService
private readonly string $projectDir,
private readonly EntityManagerInterface $entityManager,
private readonly ChapterRepository $chapterRepository,
private readonly CbzService $cbzService,
private readonly Filesystem $filesystem,
private readonly SluggerInterface $slugger
)
@@ -28,42 +28,65 @@ class MangaImportService
/**
* @throws Exception
*/
#[NoReturn] public function importVolume(Manga $manga, int $volume, string $tempFilePath, string $originalFileName): void
public function importFile(Manga $manga, ?int $volume, ?Chapter $chapter, string $tempFilePath): void
{
// Extraire les métadonnées du fichier CBZ
$metadata = $this->cbzService->extractMetadata($tempFilePath, $originalFileName);
if ($chapter !== null) {
$this->importChapter($manga, $chapter, $tempFilePath);
} elseif ($volume !== null) {
$this->importVolume($manga, $volume, $tempFilePath);
} else {
throw new \RuntimeException("Impossible de déterminer s'il s'agit d'un volume ou d'un chapitre.");
}
}
// Créer le nom de fichier et le chemin pour le stockage permanent
$permanentFileName = $this->createPermanentFileName($manga, $volume, $metadata);
/**
* @throws Exception
*/
private function importVolume(Manga $manga, int $volume, string $tempFilePath): void
{
$permanentFileName = $this->createPermanentFileName($manga, $volume);
$mangaDirectory = $this->createMangaDirectory($manga);
$permanentFilePath = $this->projectDir . '/' . $mangaDirectory .'/volume_' . sprintf('%02d', $volume) . '/' . $permanentFileName;
// Vérifier si le fichier existe déjà
if ($this->filesystem->exists($permanentFilePath)) {
throw new \RuntimeException("Un fichier pour ce volume/chapitre existe déjà.");
throw new \RuntimeException("Un fichier pour ce volume existe déjà.");
}
// Déplacer le fichier vers l'emplacement permanent
$this->filesystem->mkdir(dirname($permanentFilePath), 0755);
$this->filesystem->rename($tempFilePath, $permanentFilePath, true);
// Mettre à jour ou créer les entités Chapter
if (isset($metadata['chapter'])) {
// Si c'est un chapitre spécifique
$this->updateChapter($manga, $volume, $metadata['chapter'], $permanentFilePath);
} else {
// Si c'est un volume entier, mettre à jour tous les chapitres du volume
$this->updateVolumeChapters($manga, $volume, $permanentFilePath);
}
$this->updateVolumeChapters($manga, $volume, $permanentFilePath);
$this->entityManager->flush();
}
private function createPermanentFileName(Manga $manga, int $volume, array $metadata): string
/**
* @throws Exception
*/
private function importChapter(Manga $manga, Chapter $chapter, string $tempFilePath): void
{
$volume = $chapter->getVolume();
$permanentFileName = $this->createPermanentFileName($manga, $volume, $chapter->getNumber());
$mangaDirectory = $this->createMangaDirectory($manga);
$permanentFilePath = $this->projectDir . '/' . $mangaDirectory .'/volume_' . sprintf('%02d', $volume) . '/' . $permanentFileName;
if ($this->filesystem->exists($permanentFilePath)) {
throw new \RuntimeException("Un fichier pour ce chapitre existe déjà.");
}
$this->filesystem->mkdir(dirname($permanentFilePath), 0755);
$this->filesystem->rename($tempFilePath, $permanentFilePath, true);
$chapter->setCbzPath($permanentFilePath);
$this->entityManager->flush();
}
private function createPermanentFileName(Manga $manga, int $volume, ?float $chapterNumber = null): string
{
$baseFileName = $this->slugger->slug($manga->getTitle()) . '_vol' . sprintf('%02d', $volume);
if (isset($metadata['chapter'])) {
$baseFileName .= '_ch' . $metadata['chapter'];
if ($chapterNumber !== null) {
$baseFileName .= '_ch' . $chapterNumber;
}
return $baseFileName . '.cbz';
}
@@ -77,21 +100,6 @@ class MangaImportService
return $directoryPath;
}
private function updateChapter(Manga $manga, int $volume, float $chapterNumber, string $cbzPath): void
{
$chapter = $this->chapterRepository->findOneBy([
'manga' => $manga,
'volume' => $volume,
'number' => $chapterNumber
]);
if (!$chapter) {
throw new \RuntimeException("Le chapitre $chapterNumber du volume $volume n'existe pas en base de données.");
}
$chapter->setCbzPath($cbzPath);
}
private function updateVolumeChapters(Manga $manga, int $volume, string $cbzPath): void
{
$chapters = $this->chapterRepository->findBy([