|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Damax\Media\Bridge\Symfony\Bundle\Controller\Standard; |
|
6
|
|
|
|
|
7
|
|
|
use Damax\Media\Application\Exception\ImageProcessingFailure; |
|
8
|
|
|
use Damax\Media\Application\Exception\MediaNotFound; |
|
9
|
|
|
use Damax\Media\Application\Query\DownloadMedia; |
|
10
|
|
|
use Damax\Media\Application\Query\GetImage; |
|
11
|
|
|
use League\Tactician\CommandBus as QueryBus; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
14
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
16
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @Route("/media") |
|
20
|
|
|
*/ |
|
21
|
|
|
final class DownloadController |
|
22
|
|
|
{ |
|
23
|
|
|
private $queryBus; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(QueryBus $queryBus) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->queryBus = $queryBus; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @Route("/{id}/download", methods={"GET"}, name="media_download") |
|
32
|
|
|
* |
|
33
|
|
|
* @throws NotFoundHttpException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function download(string $id): Response |
|
36
|
|
|
{ |
|
37
|
|
|
try { |
|
38
|
|
|
return $this->queryBus->handle(new DownloadMedia($id)); |
|
39
|
|
|
} catch (MediaNotFound $e) { |
|
40
|
|
|
throw new NotFoundHttpException(); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @Route("/{id}/image", methods={"GET"}, name="media_image") |
|
46
|
|
|
* |
|
47
|
|
|
* @throws NotFoundHttpException |
|
48
|
|
|
* @throws BadRequestHttpException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function image(Request $request, string $id): Response |
|
51
|
|
|
{ |
|
52
|
|
|
try { |
|
53
|
|
|
return $this->queryBus->handle(new GetImage($id, $request->query->all())); |
|
54
|
|
|
} catch (MediaNotFound $e) { |
|
55
|
|
|
throw new NotFoundHttpException(); |
|
56
|
|
|
} catch (ImageProcessingFailure $e) { |
|
57
|
|
|
throw new BadRequestHttpException(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|