feat: GetPage endpoint
This commit is contained in:
parent
55945adc53
commit
33f5a5568a
@@ -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]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user