feat: ajout de la fonctionnalité de conversion de fichiers de bande dessinée, permettant la conversion de fichiers CBR en CBZ. Intégration d'un service de conversion, d'une API pour gérer les téléchargements, et mise en place de validations pour les fichiers uploadés. Tests unitaires ajoutés pour garantir le bon fonctionnement de cette nouvelle fonctionnalité.
This commit is contained in:
parent
b4bfa48d00
commit
7a05934116
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Conversion\Infrastructure\ApiPlatform\Controller;
|
||||
|
||||
use App\Domain\Conversion\Application\Command\ConvertFileCommand;
|
||||
use App\Domain\Conversion\Application\CommandHandler\ConvertFileCommandHandler;
|
||||
use App\Domain\Conversion\Domain\Exception\ConversionException;
|
||||
use App\Domain\Conversion\Infrastructure\ApiPlatform\Resource\ConvertFileResource;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Attribute\AsController;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
|
||||
#[AsController]
|
||||
final class ConvertFileController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ConvertFileCommandHandler $commandHandler
|
||||
) {}
|
||||
|
||||
public function __invoke(Request $request): Response
|
||||
{
|
||||
$uploadedFile = $request->files->get('file');
|
||||
if (!$uploadedFile) {
|
||||
return $this->json([
|
||||
['propertyPath' => 'file', 'message' => 'Please upload a file']
|
||||
], 422);
|
||||
}
|
||||
|
||||
// Validation manuelle pour éviter les règles automatiques de Symfony
|
||||
$errors = $this->validateFile($uploadedFile);
|
||||
if (!empty($errors)) {
|
||||
return $this->json($errors, 422);
|
||||
}
|
||||
|
||||
try {
|
||||
// Créer la commande
|
||||
$command = new ConvertFileCommand(
|
||||
filePath: $uploadedFile->getPathname(),
|
||||
originalFilename: $uploadedFile->getClientOriginalName(),
|
||||
fileSize: $uploadedFile->getSize()
|
||||
);
|
||||
|
||||
// Exécuter la conversion
|
||||
$response = $this->commandHandler->handle($command);
|
||||
|
||||
// Retourner le fichier converti
|
||||
$fileContent = file_get_contents($response->convertedFilePath);
|
||||
|
||||
return new Response(
|
||||
content: $fileContent,
|
||||
status: 200,
|
||||
headers: [
|
||||
'Content-Type' => 'application/x-cbz',
|
||||
'Content-Disposition' => sprintf('attachment; filename=%s', $response->outputFilename),
|
||||
]
|
||||
);
|
||||
|
||||
} catch (ConversionException $e) {
|
||||
return $this->json(['error' => $e->getMessage()], 400);
|
||||
}
|
||||
}
|
||||
|
||||
private function validateFile($uploadedFile): array
|
||||
{
|
||||
$errors = [];
|
||||
|
||||
// Vérifier si le fichier est valide
|
||||
if (!$uploadedFile->isValid()) {
|
||||
$errors[] = [
|
||||
'propertyPath' => 'file',
|
||||
'message' => 'The uploaded file is not valid: ' . $uploadedFile->getErrorMessage()
|
||||
];
|
||||
return $errors;
|
||||
}
|
||||
|
||||
// Vérifier la taille (150MB max)
|
||||
$maxSize = 150 * 1024 * 1024; // 150MB en bytes
|
||||
if ($uploadedFile->getSize() > $maxSize) {
|
||||
$errors[] = [
|
||||
'propertyPath' => 'file',
|
||||
'message' => 'The uploaded file is too large. Allowed size is 150MB.'
|
||||
];
|
||||
}
|
||||
|
||||
// Vérifier l'extension
|
||||
$allowedExtensions = ['cbr', 'cbz', 'zip', 'rar'];
|
||||
$extension = strtolower($uploadedFile->getClientOriginalExtension());
|
||||
|
||||
if (!in_array($extension, $allowedExtensions)) {
|
||||
$errors[] = [
|
||||
'propertyPath' => 'file',
|
||||
'message' => 'Please upload a valid CBR or CBZ file'
|
||||
];
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user