refactor(reader): serve pages as static files instead of base64
Replace the per-page API call (base64 payload) with static image URLs
served directly by Caddy from public/images/pages/{chapterId}/.
- LocalImageStorage now stores to public/images/ (was MANGA_DATA_PATH)
- LegacyChapterRepository returns /images/pages/{id}/{file} URLs,
uses getimagesize() instead of loading file content into memory
- Delete GetChapterPage query/handler/response, ChapterPageResource,
ChapterPageProvider, PageContent model
- Remove getPageContent() from ChapterRepositoryInterface
- Frontend: loadChapter() fetches chapter + all pages in parallel,
ReaderPage uses URL instead of base64 data URI, InfiniteReader drops
lazy-load observer side effect, readerStore drops loadedPages/preload
- GetChapterPagesTest: extract fixture images from CBZ at runtime,
ignore tests/Fixtures/pages/ in .gitignore
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
6875ad4222
commit
322c396165
@@ -6,12 +6,9 @@ namespace App\Tests\Domain\Reader\Adapter;
|
||||
|
||||
use App\Domain\Reader\Domain\Contract\Repository\ChapterRepositoryInterface;
|
||||
use App\Domain\Reader\Domain\Exception\ChapterNotFoundException;
|
||||
use App\Domain\Reader\Domain\Exception\PageNotFoundException;
|
||||
use App\Domain\Reader\Domain\Model\ChapterContext;
|
||||
use App\Domain\Reader\Domain\Model\Page;
|
||||
use App\Domain\Reader\Domain\Model\PageContent;
|
||||
use App\Domain\Reader\Domain\ValueObject\ChapterId;
|
||||
use App\Domain\Reader\Domain\ValueObject\PageNumber;
|
||||
|
||||
final class InMemoryChapterRepository implements ChapterRepositoryInterface
|
||||
{
|
||||
@@ -94,28 +91,4 @@ final class InMemoryChapterRepository implements ChapterRepositoryInterface
|
||||
return $nextChapter ? new ChapterId($nextChapter) : null;
|
||||
}
|
||||
|
||||
public function getPageContent(ChapterId $chapterId, PageNumber $pageNumber): PageContent
|
||||
{
|
||||
if (!isset($this->chapters[$chapterId->getValue()])) {
|
||||
throw ChapterNotFoundException::forChapter($chapterId);
|
||||
}
|
||||
|
||||
$pages = $this->chapters[$chapterId->getValue()]['pages'];
|
||||
$index = $pageNumber->getValue() - 1;
|
||||
|
||||
if (!isset($pages[$index])) {
|
||||
throw PageNotFoundException::forPage($chapterId, $pageNumber);
|
||||
}
|
||||
|
||||
$page = $pages[$index];
|
||||
|
||||
return new PageContent(
|
||||
$page->getId(),
|
||||
$page->getPageNumber(),
|
||||
base64_encode('fake-image-content'),
|
||||
'image/jpeg',
|
||||
800,
|
||||
600
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Domain\Reader\Application\QueryHandler;
|
||||
|
||||
use App\Domain\Reader\Application\Query\GetChapterPage;
|
||||
use App\Domain\Reader\Application\QueryHandler\GetChapterPageHandler;
|
||||
use App\Domain\Reader\Domain\Exception\ChapterNotFoundException;
|
||||
use App\Domain\Reader\Domain\Exception\PageNotFoundException;
|
||||
use App\Domain\Reader\Domain\Model\ChapterContext;
|
||||
use App\Domain\Reader\Domain\Model\Page;
|
||||
use App\Domain\Reader\Domain\ValueObject\ChapterId;
|
||||
use App\Domain\Reader\Domain\ValueObject\PageNumber;
|
||||
use App\Tests\Domain\Reader\Adapter\InMemoryChapterRepository;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class GetChapterPageHandlerTest extends TestCase
|
||||
{
|
||||
private InMemoryChapterRepository $repository;
|
||||
private GetChapterPageHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = new InMemoryChapterRepository();
|
||||
$this->handler = new GetChapterPageHandler($this->repository);
|
||||
|
||||
// Préparation des données de test
|
||||
$chapterId = new ChapterId('chapter-1');
|
||||
$context = new ChapterContext(
|
||||
$chapterId,
|
||||
null,
|
||||
null,
|
||||
'Test Manga',
|
||||
1.0,
|
||||
'Chapter 1',
|
||||
'path/to/cbz',
|
||||
1,
|
||||
10,
|
||||
true,
|
||||
new \DateTimeImmutable()
|
||||
);
|
||||
|
||||
$pages = [];
|
||||
for ($i = 1; $i <= 10; $i++) {
|
||||
$pages[] = new Page(
|
||||
sprintf('page-%d', $i),
|
||||
new PageNumber($i),
|
||||
sprintf('/api/chapters/chapter-1/pages/%d', $i),
|
||||
800,
|
||||
600
|
||||
);
|
||||
}
|
||||
|
||||
$this->repository->addChapter($chapterId, $context, $pages);
|
||||
}
|
||||
|
||||
public function testItThrowsExceptionWhenChapterDoesNotExist(): void
|
||||
{
|
||||
$this->expectException(ChapterNotFoundException::class);
|
||||
$this->handler->handle(new GetChapterPage('invalid-id', 1));
|
||||
}
|
||||
|
||||
public function testItThrowsExceptionWhenPageNumberExceedsTotalPages(): void
|
||||
{
|
||||
$this->expectException(PageNotFoundException::class);
|
||||
$this->handler->handle(new GetChapterPage('chapter-1', 11));
|
||||
}
|
||||
|
||||
public function testItReturnsPageContentSuccessfully(): void
|
||||
{
|
||||
$response = $this->handler->handle(new GetChapterPage('chapter-1', 5));
|
||||
|
||||
$this->assertEquals('page-5', $response->getId());
|
||||
$this->assertEquals(5, $response->getPageNumber());
|
||||
$this->assertNotEmpty($response->getBase64Content());
|
||||
$this->assertEquals('image/jpeg', $response->getMimeType());
|
||||
$this->assertEquals(['width' => 800, 'height' => 600], $response->getDimensions());
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Feature\Reader;
|
||||
|
||||
use App\Factory\ChapterFactory;
|
||||
use App\Factory\MangaFactory;
|
||||
use App\Tests\Feature\AbstractApiTestCase;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Zenstruck\Foundry\Test\ResetDatabase;
|
||||
|
||||
final class GetChapterPageTest extends AbstractApiTestCase
|
||||
{
|
||||
use ResetDatabase;
|
||||
|
||||
private int $chapterId;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// Création d'un manga et d'un chapitre avec les factories
|
||||
$manga = MangaFactory::createOne([
|
||||
'title' => 'Test Manga',
|
||||
'slug' => 'test-manga'
|
||||
]);
|
||||
|
||||
$chapter = ChapterFactory::createOne([
|
||||
'manga' => $manga,
|
||||
'title' => 'Chapter 1',
|
||||
'number' => 1.0,
|
||||
'volume' => 1,
|
||||
'visible' => true,
|
||||
'cbzPath' => __DIR__ . '/../../Fixtures/chapter.cbz'
|
||||
]);
|
||||
|
||||
$this->chapterId = $chapter->getId();
|
||||
}
|
||||
|
||||
public function testItReturnsNotFoundWhenChapterDoesNotExist(): void
|
||||
{
|
||||
$response = static::createClient()->request('GET', '/api/reader/chapter/999/page/1');
|
||||
|
||||
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
|
||||
$this->assertJsonContains([
|
||||
'detail' => 'Le chapitre 999 n\'existe pas'
|
||||
]);
|
||||
}
|
||||
|
||||
public function testItReturnsNotFoundWhenPageDoesNotExist(): void
|
||||
{
|
||||
$response = static::createClient()->request('GET', sprintf('/api/reader/chapter/%d/page/999', $this->chapterId));
|
||||
|
||||
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
|
||||
$this->assertJsonContains([
|
||||
'detail' => sprintf('La page 999 du chapitre %d n\'existe pas', $this->chapterId)
|
||||
]);
|
||||
}
|
||||
|
||||
public function testItReturnsPageContentSuccessfully(): void
|
||||
{
|
||||
$response = static::createClient()->request('GET', sprintf('/api/reader/chapter/%d/page/1', $this->chapterId));
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
|
||||
|
||||
// $this->assertJsonContains([
|
||||
// 'id' => '01.jpg',
|
||||
// 'pageNumber' => 1,
|
||||
// 'mimeType' => 'image/jpeg',
|
||||
// 'dimensions' => [
|
||||
// 'hydra:member' => [
|
||||
// 800,
|
||||
// 1169
|
||||
// ]
|
||||
// ]
|
||||
// ]);
|
||||
|
||||
$content = $response->toArray();
|
||||
$this->assertArrayHasKey('base64Content', $content);
|
||||
$this->assertNotEmpty($content['base64Content']);
|
||||
$this->assertTrue(base64_decode($content['base64Content'], true) !== false);
|
||||
}
|
||||
}
|
||||
@@ -9,18 +9,27 @@ use App\Factory\MangaFactory;
|
||||
use App\Tests\Feature\AbstractApiTestCase;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Zenstruck\Foundry\Test\ResetDatabase;
|
||||
use ZipArchive;
|
||||
|
||||
final class GetChapterPagesTest extends AbstractApiTestCase
|
||||
{
|
||||
use ResetDatabase;
|
||||
|
||||
private int $chapterId;
|
||||
private string $pagesDirectory;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// Création d'un manga et d'un chapitre avec les factories
|
||||
// Extraire quelques images du CBZ dans un dossier temporaire
|
||||
$this->pagesDirectory = sys_get_temp_dir() . '/mangarr-test-pages-' . uniqid();
|
||||
mkdir($this->pagesDirectory);
|
||||
$zip = new ZipArchive();
|
||||
$zip->open(__DIR__ . '/../../Fixtures/chapter.cbz');
|
||||
$zip->extractTo($this->pagesDirectory, ['007.jpg', '008.jpg']);
|
||||
$zip->close();
|
||||
|
||||
$manga = MangaFactory::createOne([
|
||||
'title' => 'Test Manga',
|
||||
'slug' => 'test-manga'
|
||||
@@ -32,12 +41,22 @@ final class GetChapterPagesTest extends AbstractApiTestCase
|
||||
'number' => 1.0,
|
||||
'volume' => 1,
|
||||
'visible' => true,
|
||||
'cbzPath' => __DIR__ . '/../../Fixtures/chapter.cbz'
|
||||
'pagesDirectory' => $this->pagesDirectory
|
||||
]);
|
||||
|
||||
$this->chapterId = $chapter->getId();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
foreach (glob($this->pagesDirectory . '/*') as $file) {
|
||||
unlink($file);
|
||||
}
|
||||
rmdir($this->pagesDirectory);
|
||||
}
|
||||
|
||||
public function testItReturnsNotFoundWhenChapterDoesNotExist(): void
|
||||
{
|
||||
$response = static::createClient()->request('GET', '/api/reader/chapter/999/pages');
|
||||
|
||||
Reference in New Issue
Block a user