|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2019 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2019 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\Controller; |
|
18
|
|
|
|
|
19
|
|
|
use Doctrine\Common\Cache\Cache; |
|
20
|
|
|
use SWP\Bundle\ContentBundle\File\FileExtensionCheckerInterface; |
|
21
|
|
|
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface; |
|
22
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleMedia; |
|
23
|
|
|
use SWP\Bundle\ContentBundle\Provider\FileProviderInterface; |
|
24
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
26
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
|
27
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
28
|
|
|
use Hoa\Mime\Mime; |
|
29
|
|
|
|
|
30
|
|
|
abstract class AbstractMediaController extends AbstractController |
|
31
|
|
|
{ |
|
32
|
|
|
protected $mediaManager; |
|
33
|
|
|
|
|
34
|
|
|
protected $cacheProvider; |
|
35
|
|
|
|
|
36
|
|
|
protected $fileProvider; |
|
37
|
|
|
|
|
38
|
|
|
protected $fileExtensionChecker; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct( |
|
41
|
|
|
MediaManagerInterface $mediaManager, |
|
42
|
|
|
Cache $cacheProvider, |
|
43
|
|
|
FileProviderInterface $fileProvider, |
|
44
|
|
|
FileExtensionCheckerInterface $fileExtensionChecker |
|
45
|
|
|
) { |
|
46
|
|
|
$this->mediaManager = $mediaManager; |
|
47
|
|
|
$this->cacheProvider = $cacheProvider; |
|
48
|
|
|
$this->fileProvider = $fileProvider; |
|
49
|
|
|
$this->fileExtensionChecker = $fileExtensionChecker; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getMedia(string $mediaId, string $extension): Response |
|
53
|
|
|
{ |
|
54
|
|
|
$cacheKey = md5(serialize(['media_file', $mediaId])); |
|
55
|
|
|
if (!$this->cacheProvider->contains($cacheKey)) { |
|
56
|
|
|
$media = $this->fileProvider->getFile(ArticleMedia::handleMediaId($mediaId), $extension); |
|
57
|
|
|
$this->cacheProvider->save($cacheKey, $media, 63072000); |
|
58
|
|
|
} else { |
|
59
|
|
|
$media = $this->cacheProvider->fetch($cacheKey); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if (null === $media) { |
|
63
|
|
|
throw new NotFoundHttpException('Media was not found.'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$response = new Response(); |
|
67
|
|
|
$mimeType = Mime::getMimeFromExtension($extension); |
|
68
|
|
|
|
|
69
|
|
|
if (!$this->fileExtensionChecker->isAttachment($mimeType)) { |
|
70
|
|
|
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE, str_replace('/', '_', $mediaId.'.'.$media->getFileExtension())); |
|
71
|
|
|
} else { |
|
72
|
|
|
$disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, str_replace('/', '_', $mediaId.'.'.$media->getFileExtension())); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$response->headers->set('Content-Disposition', $disposition); |
|
76
|
|
|
$response->headers->set('Content-Type', Mime::getMimeFromExtension($media->getFileExtension())); |
|
77
|
|
|
|
|
78
|
|
|
$response->setPublic(); |
|
79
|
|
|
$response->setMaxAge(63072000); |
|
80
|
|
|
$response->setSharedMaxAge(63072000); |
|
81
|
|
|
$response->setLastModified($media->getUpdatedAt() ?: $media->getCreatedAt()); |
|
82
|
|
|
$response->setContent($this->mediaManager->getFile($media)); |
|
83
|
|
|
|
|
84
|
|
|
return $response; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|