feat: Image saving for manga creation

This commit is contained in:
ext.jeremy.guillot@maxicoffee.domains
2025-02-11 00:40:47 +01:00
parent 50080f9779
commit 4017cabff2
9 changed files with 212 additions and 31 deletions

View File

@@ -5,14 +5,17 @@ namespace App\Domain\Manga\Application\CommandHandler;
use App\Domain\Manga\Application\Command\CreateMangaFromMangadex;
use App\Domain\Manga\Domain\Contract\Provider\MangaProviderInterface;
use App\Domain\Manga\Domain\Contract\Repository\MangaRepositoryInterface;
use App\Domain\Manga\Domain\Contract\Service\ImageProcessorInterface;
use App\Domain\Manga\Domain\Exception\MangaNotFoundException;
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
use App\Domain\Manga\Domain\Model\ValueObject\ImageUrls;
readonly class CreateMangaFromMangadexHandler
{
public function __construct(
private MangaProviderInterface $mangaProvider,
private MangaRepositoryInterface $mangaRepository
private MangaRepositoryInterface $mangaRepository,
private ImageProcessorInterface $imageProcessor
) {}
public function handle(CreateMangaFromMangadex $command): void
@@ -23,6 +26,19 @@ readonly class CreateMangaFromMangadexHandler
throw new MangaNotFoundException('Manga not found on Mangadex');
}
try {
// Télécharge l'image originale
$fullImagePath = $this->imageProcessor->downloadImage($manga->getImageUrl());
// Crée la miniature à partir de l'image originale
$thumbnailPath = $this->imageProcessor->createThumbnail($fullImagePath);
// Met à jour le manga avec les nouveaux chemins d'images
$manga->updateImageUrls(new ImageUrls($fullImagePath, $thumbnailPath));
} catch (\Exception $e) {
throw new \RuntimeException('Erreur lors du traitement de l\'image : ' . $e->getMessage());
}
$this->mangaRepository->save($manga);
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Domain\Manga\Domain\Contract\Service;
interface ImageProcessorInterface
{
/**
* @throws \Exception
*/
public function downloadImage(string $imageUrl): string;
/**
* @throws \Exception
*/
public function createThumbnail(string $originalImagePath): string;
}

View File

@@ -3,6 +3,7 @@
namespace App\Domain\Manga\Domain\Model;
use App\Domain\Manga\Domain\Model\ValueObject\ExternalId;
use App\Domain\Manga\Domain\Model\ValueObject\ImageUrls;
use App\Domain\Manga\Domain\Model\ValueObject\MangaId;
use App\Domain\Manga\Domain\Model\ValueObject\MangaSlug;
use App\Domain\Manga\Domain\Model\ValueObject\MangaTitle;
@@ -21,6 +22,7 @@ final class Manga
private ?ExternalId $externalId = null,
private ?string $imageUrl = null,
private ?float $rating = null,
private ?ImageUrls $imageUrls = null,
) {}
public function getId(): MangaId
@@ -78,6 +80,11 @@ final class Manga
return $this->rating;
}
public function getImageUrls(): ?ImageUrls
{
return $this->imageUrls;
}
public function setRating(float $rating): void
{
$this->rating = $rating;
@@ -87,4 +94,9 @@ final class Manga
{
$this->id = $id;
}
public function updateImageUrls(ImageUrls $imageUrls): void
{
$this->imageUrls = $imageUrls;
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Domain\Manga\Domain\Model\ValueObject;
readonly class ImageUrls
{
public function __construct(
private string $full,
private string $thumbnail
) {}
public function getFull(): string
{
return $this->full;
}
public function getThumbnail(): string
{
return $this->thumbnail;
}
}

View File

@@ -65,7 +65,8 @@ readonly class LegacyMangaRepository implements MangaRepositoryInterface
->setGenres($manga->getGenres())
->setStatus($manga->getStatus())
->setExternalId($manga->getExternalId()->getValue())
->setImageUrl($manga->getImageUrl())
->setImageUrl($manga->getImageUrls()->getFull())
->setThumbnailUrl($manga->getImageUrls()->getThumbnail())
->setMonitored(false);
if ($manga->getRating() !== null) {

View File

@@ -0,0 +1,91 @@
<?php
namespace App\Domain\Manga\Infrastructure\Service;
use App\Domain\Manga\Domain\Contract\Service\ImageProcessorInterface;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;
use GuzzleHttp\Client;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
class ImageProcessor implements ImageProcessorInterface
{
private const string BASE_PATH = '/images';
private const string FULL_IMAGES_DIR = self::BASE_PATH . '/full';
private const string THUMBNAILS_DIR = self::BASE_PATH . '/thumbnails';
private const int THUMBNAIL_WIDTH = 300;
private const int THUMBNAIL_HEIGHT = 440;
private const int FULL_IMAGE_QUALITY = 90;
private const int THUMBNAIL_QUALITY = 85;
private ImageManager $imageManager;
public function __construct(
private readonly string $publicDir,
private readonly Client $httpClient
) {
$this->imageManager = new ImageManager(new Driver());
$this->ensureDirectoriesExist();
}
public function downloadImage(string $imageUrl): string
{
try {
$response = $this->httpClient->get($imageUrl);
if ($response->getStatusCode() !== 200) {
throw new \RuntimeException('Échec du téléchargement de l\'image');
}
$uuid = Uuid::uuid4()->toString();
$extension = pathinfo($imageUrl, PATHINFO_EXTENSION) ?: 'jpg';
$filename = sprintf('%s.%s', $uuid, $extension);
$fullPath = $this->publicDir . self::FULL_IMAGES_DIR . '/' . $filename;
$image = $this->imageManager->read($response->getBody()->getContents());
$image->save($fullPath, quality: self::FULL_IMAGE_QUALITY);
return self::FULL_IMAGES_DIR . '/' . $filename;
} catch (\Exception $e) {
throw new \RuntimeException('Erreur lors du téléchargement de l\'image : ' . $e->getMessage());
}
}
public function createThumbnail(string $originalImagePath): string
{
try {
$fullPath = $this->publicDir . $originalImagePath;
if (!file_exists($fullPath)) {
throw new \RuntimeException('Image originale non trouvée');
}
$filename = pathinfo($originalImagePath, PATHINFO_BASENAME);
$thumbnailPath = $this->publicDir . self::THUMBNAILS_DIR . '/' . $filename;
$thumbnail = $this->imageManager->read($fullPath);
$thumbnail->cover(self::THUMBNAIL_WIDTH, self::THUMBNAIL_HEIGHT);
$thumbnail->save($thumbnailPath, quality: self::THUMBNAIL_QUALITY);
return self::THUMBNAILS_DIR . '/' . $filename;
} catch (\Exception $e) {
throw new \RuntimeException('Erreur lors de la création de la miniature : ' . $e->getMessage());
}
}
private function ensureDirectoriesExist(): void
{
$directories = [
$this->publicDir . self::FULL_IMAGES_DIR,
$this->publicDir . self::THUMBNAILS_DIR,
];
foreach ($directories as $directory) {
if (!is_dir($directory)) {
if (!mkdir($directory, 0755, true) && !is_dir($directory)) {
throw new \RuntimeException(sprintf('Le répertoire "%s" n\'a pas pu être créé', $directory));
}
}
}
}
}