feat: GetPage endpoint

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-02-16 18:22:20 +01:00
parent 55945adc53
commit 33f5a5568a
14 changed files with 710 additions and 0 deletions

View File

@@ -13,6 +13,8 @@ use App\Domain\Reader\Domain\ValueObject\PageNumber;
use App\Entity\Chapter as ChapterEntity;
use Doctrine\ORM\EntityManagerInterface;
use ZipArchive;
use App\Domain\Reader\Domain\Exception\PageNotFoundException;
use App\Domain\Reader\Domain\Model\PageContent;
readonly class LegacyChapterRepository implements ChapterRepositoryInterface
{
@@ -100,6 +102,10 @@ readonly class LegacyChapterRepository implements ChapterRepositoryInterface
'id' => $chapterId->getValue()
]);
if (!$chapter) {
throw ChapterNotFoundException::forChapter($chapterId);
}
$cbzPath = $chapter->getCbzPath();
if (!$cbzPath) {
@@ -156,4 +162,62 @@ readonly class LegacyChapterRepository implements ChapterRepositoryInterface
return $nextChapter ? new ChapterId((string) $nextChapter->getId()) : null;
}
public function getPageContent(ChapterId $chapterId, PageNumber $pageNumber): PageContent
{
$chapter = $this->entityManager->getRepository(ChapterEntity::class)->findOneBy([
'id' => $chapterId->getValue()
]);
if (!$chapter) {
throw ChapterNotFoundException::forChapter($chapterId);
}
$cbzPath = $chapter->getCbzPath();
if (!$cbzPath || !file_exists($cbzPath)) {
throw ChapterNotFoundException::forChapter($chapterId);
}
$zip = new ZipArchive();
$zip->open($cbzPath);
if ($pageNumber->getValue() > $zip->numFiles) {
$zip->close();
throw PageNotFoundException::forPage($chapterId, $pageNumber);
}
$index = $pageNumber->getValue() - 1;
$stat = $zip->statIndex($index);
if ($stat === false) {
$zip->close();
throw PageNotFoundException::forPage($chapterId, $pageNumber);
}
$imageContent = $zip->getFromIndex($index);
if ($imageContent === false) {
$zip->close();
throw PageNotFoundException::forPage($chapterId, $pageNumber);
}
$imageSize = @getimagesizefromstring($imageContent);
if ($imageSize === false) {
$zip->close();
throw PageNotFoundException::forPage($chapterId, $pageNumber);
}
$mimeType = $imageSize['mime'] ?? 'image/jpeg';
$zip->close();
return new PageContent(
$stat['name'],
$pageNumber,
base64_encode($imageContent),
$mimeType,
$imageSize[0],
$imageSize[1]
);
}
}