Added:
- turbo + code adaptation - cover & thumbnails download
This commit is contained in:
@@ -8,83 +8,94 @@ use App\Entity\Manga;
|
||||
use App\Message\DownloadChapter;
|
||||
use App\Repository\ChapterRepository;
|
||||
use App\Repository\MangaRepository;
|
||||
use App\Service\ActivityService;
|
||||
use App\Service\MangadexProvider;
|
||||
use App\Service\MangaScraperService;
|
||||
use App\Service\MangaUpdatesMetadataProvider;
|
||||
use App\Service\SushiScanProviderService;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Intervention\Image\Drivers\Gd\Driver;
|
||||
use Intervention\Image\ImageManager;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\String\Slugger\SluggerInterface;
|
||||
|
||||
class TestController extends AbstractController
|
||||
{
|
||||
private ImageManager $imageManager;
|
||||
public function __construct(
|
||||
private MangadexProvider $mangadexProvider,
|
||||
private MangaRepository $mangaRepository,
|
||||
private MessageBusInterface $bus,
|
||||
private Connection $connection,
|
||||
private SerializerInterface $serializer,
|
||||
private readonly ChapterRepository $chapterRepository
|
||||
private string $projectDir,
|
||||
private SluggerInterface $slugger,
|
||||
private MangaRepository $mangaRepository
|
||||
)
|
||||
{
|
||||
$this->imageManager = new ImageManager(new Driver());
|
||||
}
|
||||
|
||||
#[Route('/test', name: 'test')]
|
||||
public function test(): Response
|
||||
{
|
||||
$sqlPending = 'SELECT * FROM messenger_messages WHERE queue_name = :queue';
|
||||
$pending = $this->connection->fetchAllAssociative($sqlPending, ['queue' => 'default']);
|
||||
$mangas = $this->mangaRepository->findAll();
|
||||
|
||||
// // Requête pour récupérer les messages en cours de traitement
|
||||
// $sqlProcessing = 'SELECT * FROM messenger_messages WHERE queue_name = :queue AND available_at IS NOT NULL';
|
||||
// $processing = $this->connection->fetchAllAssociative($sqlProcessing, ['queue' => 'default']);
|
||||
|
||||
// dd($pending);
|
||||
$decoded = $this->decodeMessages($pending);
|
||||
|
||||
$status = [];
|
||||
foreach($decoded as $message) {
|
||||
$message = $message['body'];
|
||||
if($message instanceof Envelope) {
|
||||
$chapter = $this->chapterRepository->find($message->getMessage()->getChapterId());
|
||||
$manga = $chapter->getManga();
|
||||
$status[] = [
|
||||
'manga' => $manga->getTitle(),
|
||||
'volume' => $chapter->getVolume(),
|
||||
'chapter' => $chapter->getNumber(),
|
||||
'title' => $chapter->getTitle(),
|
||||
];
|
||||
$changed = 0;
|
||||
foreach ($mangas as $manga){
|
||||
//si getImageUrl() retourne un lien sous la forme d'une URL (https ou http)
|
||||
if($manga->getImageUrl()){
|
||||
$imageUrls = $this->processAndSaveImage($manga->getImageUrl());
|
||||
$manga->setThumbnailUrl($imageUrls['thumbnail']);
|
||||
$this->mangaRepository->save($manga, true);
|
||||
$changed++;
|
||||
}
|
||||
}
|
||||
|
||||
// $this->bus->dispatch(new DownloadChapter(1));
|
||||
|
||||
dd($status);
|
||||
return new JsonResponse(['changed' => $changed]);
|
||||
}
|
||||
|
||||
private function decodeMessages(array $messages): array
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
private function processAndSaveImage(string $imageUrl): array
|
||||
{
|
||||
$decodedMessages = [];
|
||||
$image = file_get_contents($this->projectDir . '/public' .$imageUrl);
|
||||
$tempImage = tmpfile();
|
||||
fwrite($tempImage, $image);
|
||||
$tempImagePath = stream_get_meta_data($tempImage)['uri'];
|
||||
|
||||
foreach ($messages as $message) {
|
||||
$decodedMessages[] = [
|
||||
'id' => $message['id'],
|
||||
'body' => $this->decodeMessageBody($message['body']),
|
||||
'headers' => json_decode($message['headers'], true),
|
||||
// Générer un nom de fichier unique
|
||||
$originalFilename = pathinfo($imageUrl, PATHINFO_FILENAME);
|
||||
$safeFilename = $this->slugger->slug($originalFilename);
|
||||
$newFilename = $safeFilename . '-' . uniqid() . '.' . 'jpg';
|
||||
|
||||
try {
|
||||
// Créer et sauvegarder la miniature
|
||||
$thumbnail = $this->imageManager->read($tempImagePath);
|
||||
$thumbnail->cover(300, 440);
|
||||
$thumbnail->save($this->projectDir . '/public/images/thumbnails/' . $newFilename, quality: 85);
|
||||
|
||||
// Sauvegarder l'image en taille réelle
|
||||
// $fullImage = $this->imageManager->read($tempImagePath);
|
||||
// $fullImage->save($this->projectDir . '/public/images/full/' . $newFilename, quality: 90);
|
||||
|
||||
// Fermer et supprimer le fichier temporaire
|
||||
fclose($tempImage);
|
||||
|
||||
return [
|
||||
'full' => '/images/full/' . $newFilename,
|
||||
'thumbnail' => '/images/thumbnails/' . $newFilename
|
||||
];
|
||||
|
||||
} catch (FileException $e) {
|
||||
// Fermer le fichier temporaire en cas d'erreur
|
||||
fclose($tempImage);
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $decodedMessages;
|
||||
}
|
||||
|
||||
private function decodeMessageBody(string $body)
|
||||
{
|
||||
return unserialize(stripcslashes($body));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user