feat: ajout de la gestion des commandes pour la suppression des fichiers CBZ et des chapitres, avec création des gestionnaires et des ressources API correspondantes
This commit is contained in:
parent
7fe4ac0d3b
commit
37e1b202c2
77
src/Domain/Manga/Infrastructure/Service/FileService.php
Normal file
77
src/Domain/Manga/Infrastructure/Service/FileService.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Manga\Infrastructure\Service;
|
||||
|
||||
use App\Domain\Manga\Domain\Contract\Service\FileServiceInterface;
|
||||
use App\Domain\Manga\Domain\Exception\CbzFileNotFoundException;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
|
||||
readonly class FileService implements FileServiceInterface
|
||||
{
|
||||
public function __construct(
|
||||
private string $cbzStoragePath = '/app/public/cbz'
|
||||
) {}
|
||||
|
||||
public function downloadCbz(string $filePath, string $filename): Response
|
||||
{
|
||||
if (!$this->cbzExists($filePath)) {
|
||||
throw new CbzFileNotFoundException($filePath);
|
||||
}
|
||||
|
||||
$response = new BinaryFileResponse($filePath);
|
||||
$response->setContentDisposition(
|
||||
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
|
||||
$filename
|
||||
);
|
||||
$response->headers->set('Content-Type', 'application/x-cbz');
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function createVolumeCbz(array $cbzPaths, string $volumeName): Response
|
||||
{
|
||||
$tempCbzPath = sys_get_temp_dir() . '/' . $volumeName . '.cbz';
|
||||
|
||||
$cbz = new \ZipArchive();
|
||||
if ($cbz->open($tempCbzPath, \ZipArchive::CREATE) !== true) {
|
||||
throw new \RuntimeException('Cannot create CBZ file');
|
||||
}
|
||||
|
||||
foreach ($cbzPaths as $cbzPath) {
|
||||
if ($this->cbzExists($cbzPath)) {
|
||||
$filename = basename($cbzPath);
|
||||
$cbz->addFile($cbzPath, $filename);
|
||||
}
|
||||
}
|
||||
|
||||
$cbz->close();
|
||||
|
||||
$response = new BinaryFileResponse($tempCbzPath);
|
||||
$response->setContentDisposition(
|
||||
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
|
||||
$volumeName . '.cbz'
|
||||
);
|
||||
$response->headers->set('Content-Type', 'application/x-cbz');
|
||||
|
||||
// Clean up temp file after sending
|
||||
$response->deleteFileAfterSend();
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function deleteCbzFile(string $filePath): bool
|
||||
{
|
||||
if (!$this->cbzExists($filePath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return unlink($filePath);
|
||||
}
|
||||
|
||||
public function cbzExists(string $filePath): bool
|
||||
{
|
||||
return file_exists($filePath) && is_readable($filePath);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user