feat: refonte du gestionnaire de chapitres pour intégrer la génération de fichiers CBZ, le téléchargement d'images en lot et la gestion des requêtes de scraping, avec mise à jour des interfaces et des modèles associés
This commit is contained in:
parent
cdee6f77fc
commit
d7088b14c2
@@ -2,13 +2,19 @@
|
||||
|
||||
namespace App\Domain\Scraping\Infrastructure\Service;
|
||||
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use App\Domain\Scraping\Domain\Contract\Service\ImageDownloaderInterface;
|
||||
use App\Domain\Scraping\Domain\Event\PageScrapingProgressed;
|
||||
use App\Domain\Scraping\Domain\Model\ScrapingProgress;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\DownloadResult;
|
||||
use App\Domain\Scraping\Domain\Model\ValueObject\TempDirectory;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
readonly class ImageDownloader implements ImageDownloaderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private HttpClientInterface $httpClient
|
||||
private HttpClientInterface $httpClient,
|
||||
private MessageBusInterface $eventBus
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -22,4 +28,44 @@ readonly class ImageDownloader implements ImageDownloaderInterface
|
||||
|
||||
file_put_contents($destination, $response->getContent());
|
||||
}
|
||||
|
||||
public function downloadBatch(array $urls, TempDirectory $tempDir, string $jobId): array
|
||||
{
|
||||
$results = [];
|
||||
$totalUrls = count($urls);
|
||||
|
||||
foreach ($urls as $index => $url) {
|
||||
try {
|
||||
$extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION) ?: 'jpg';
|
||||
$destination = sprintf(
|
||||
'%s/%03d.%s',
|
||||
$tempDir->getPath(),
|
||||
$index + 1,
|
||||
$extension
|
||||
);
|
||||
|
||||
$this->download($url, $destination);
|
||||
$results[] = new DownloadResult($destination, $url);
|
||||
|
||||
$this->dispatchProgressEvent($jobId, $index + 1, $totalUrls);
|
||||
} catch (\Exception $e) {
|
||||
// Log l'erreur mais continue avec les autres images
|
||||
error_log("Failed to download image {$url}: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($results)) {
|
||||
throw new \RuntimeException('Failed to download any images');
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
private function dispatchProgressEvent(string $jobId, int $currentPage, int $totalPages): void
|
||||
{
|
||||
$this->eventBus->dispatch(new PageScrapingProgressed(
|
||||
$jobId,
|
||||
new ScrapingProgress($currentPage, $totalPages)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user