- turbo + code adaptation
- cover & thumbnails download
This commit is contained in:
Jérémy Guillot
2024-07-06 21:25:07 +02:00
parent 7dee9d71be
commit 54c581b229
35 changed files with 1126 additions and 573 deletions

View File

@@ -2,7 +2,10 @@
namespace App\Service;
use App\Entity\Manga;
use Exception;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\String\Slugger\SluggerInterface;
use ZipArchive;
@@ -133,4 +136,68 @@ class CbzService
sort($images);
return $images;
}
public function createVolumeArchive(array $chapters): string
{
$tempFile = tempnam(sys_get_temp_dir(), 'volume_cbz_');
$zip = new ZipArchive();
if ($zip->open($tempFile, ZipArchive::CREATE) !== TRUE) {
throw new \RuntimeException("Impossible de créer le fichier ZIP temporaire.");
}
foreach ($chapters as $chapter) {
$chapterZip = new ZipArchive();
if ($chapterZip->open($chapter->getCbzPath()) === TRUE) {
for ($i = 0; $i < $chapterZip->numFiles; $i++) {
$filename = $chapterZip->getNameIndex($i);
$fileContent = $chapterZip->getFromIndex($i);
$zip->addFromString("Chapter " . $chapter->getNumber() . "/" . $filename, $fileContent);
}
$chapterZip->close();
}
}
$zip->close();
return $tempFile;
}
public function generateFileName(Manga $manga, ?int $volume = null, ?float $chapterNumber = null): string
{
$sluggedTitle = $this->slugger->slug($manga->getTitle())->lower();
if ($volume !== null) {
return sprintf("%s_volume_%02d.cbz", $sluggedTitle, $volume);
} elseif ($chapterNumber !== null) {
return sprintf("%s_chapter_%s.cbz", $sluggedTitle, number_format($chapterNumber, 2));
} else {
throw new \InvalidArgumentException("Either volume or chapter number must be provided");
}
}
public function createBinaryFileResponse(string $filePath, string $fileName): BinaryFileResponse
{
$response = new BinaryFileResponse($filePath);
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$fileName
);
return $response;
}
public function areAllChaptersCbzIdentical(array $chapters): bool
{
if (empty($chapters)) {
return false;
}
$firstCbzPath = $chapters[0]->getCbzPath();
return array_reduce($chapters, function ($carry, $chapter) use ($firstCbzPath) {
return $carry && $chapter->getCbzPath() === $firstCbzPath;
}, true);
}
public function doAllChaptersHaveCbz(array $chapters): bool
{
return array_reduce($chapters, function ($carry, $chapter) {
return $carry && $chapter->getCbzPath() !== null;
}, true);
}
}

View File

@@ -6,7 +6,6 @@ use App\Entity\Chapter;
use App\Entity\Manga;
use App\Entity\ContentSource;
use App\Event\PageScrappingProgressEvent;
use App\EventSubscriber\MangaScrapedEvent;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use GuzzleHttp\Client;