|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Provider; |
|
15
|
|
|
|
|
16
|
|
|
use Gaufrette\Filesystem; |
|
17
|
|
|
use Sonata\Form\Validator\ErrorElement; |
|
18
|
|
|
use Sonata\MediaBundle\CDN\CDNInterface; |
|
19
|
|
|
use Sonata\MediaBundle\Generator\GeneratorInterface; |
|
20
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
|
21
|
|
|
use Sonata\MediaBundle\Resizer\ResizerInterface; |
|
22
|
|
|
use Sonata\MediaBundle\Thumbnail\ThumbnailInterface; |
|
23
|
|
|
|
|
24
|
|
|
abstract class BaseProvider implements MediaProviderInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $formats = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string[] |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $templates = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var ResizerInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $resizer; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var Filesystem |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $filesystem; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var GeneratorInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $pathGenerator; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var CDNInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $cdn; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var ThumbnailInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $thumbnail; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $name; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var MediaInterface[] |
|
68
|
|
|
*/ |
|
69
|
|
|
private $clones = []; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $name |
|
73
|
|
|
*/ |
|
74
|
|
|
public function __construct($name, Filesystem $filesystem, CDNInterface $cdn, GeneratorInterface $pathGenerator, ThumbnailInterface $thumbnail) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->name = $name; |
|
77
|
|
|
$this->filesystem = $filesystem; |
|
78
|
|
|
$this->cdn = $cdn; |
|
79
|
|
|
$this->pathGenerator = $pathGenerator; |
|
80
|
|
|
$this->thumbnail = $thumbnail; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
final public function transform(MediaInterface $media): void |
|
84
|
|
|
{ |
|
85
|
|
|
if (null === $media->getBinaryContent()) { |
|
86
|
|
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->doTransform($media); |
|
90
|
|
|
$this->flushCdn($media); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function flushCdn(MediaInterface $media): void |
|
94
|
|
|
{ |
|
95
|
|
|
if ($media->getId() && $this->requireThumbnails() && !$media->getCdnIsFlushable()) { |
|
96
|
|
|
$flushPaths = []; |
|
97
|
|
|
foreach ($this->getFormats() as $format => $settings) { |
|
98
|
|
|
if (MediaProviderInterface::FORMAT_ADMIN === $format || |
|
99
|
|
|
substr($format, 0, \strlen((string) $media->getContext())) === $media->getContext()) { |
|
100
|
|
|
$flushPaths[] = $this->getFilesystem()->get($this->generatePrivateUrl($media, $format), true)->getKey(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
if (!empty($flushPaths)) { |
|
104
|
|
|
$cdnFlushIdentifier = $this->getCdn()->flushPaths($flushPaths); |
|
105
|
|
|
$media->setCdnFlushIdentifier($cdnFlushIdentifier); |
|
106
|
|
|
$media->setCdnIsFlushable(true); |
|
107
|
|
|
$media->setCdnStatus(CDNInterface::STATUS_TO_FLUSH); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function addFormat($name, $format): void |
|
113
|
|
|
{ |
|
114
|
|
|
$this->formats[$name] = $format; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function getFormat($name) |
|
118
|
|
|
{ |
|
119
|
|
|
return isset($this->formats[$name]) ? $this->formats[$name] : false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function requireThumbnails() |
|
123
|
|
|
{ |
|
124
|
|
|
return null !== $this->getResizer(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function generateThumbnails(MediaInterface $media): void |
|
128
|
|
|
{ |
|
129
|
|
|
$this->thumbnail->generate($this, $media); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function removeThumbnails(MediaInterface $media, $formats = null): void |
|
133
|
|
|
{ |
|
134
|
|
|
$this->thumbnail->delete($this, $media, $formats); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function getFormatName(MediaInterface $media, $format) |
|
138
|
|
|
{ |
|
139
|
|
|
if (MediaProviderInterface::FORMAT_ADMIN === $format) { |
|
140
|
|
|
return MediaProviderInterface::FORMAT_ADMIN; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if (MediaProviderInterface::FORMAT_REFERENCE === $format) { |
|
144
|
|
|
return MediaProviderInterface::FORMAT_REFERENCE; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$baseName = $media->getContext().'_'; |
|
148
|
|
|
if (substr($format, 0, \strlen($baseName)) === $baseName) { |
|
149
|
|
|
return $format; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $baseName.$format; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function getProviderMetadata() |
|
156
|
|
|
{ |
|
157
|
|
|
return new Metadata($this->getName(), $this->getName().'.description', null, 'SonataMediaBundle', ['class' => 'fa fa-file']); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function preRemove(MediaInterface $media): void |
|
161
|
|
|
{ |
|
162
|
|
|
$hash = spl_object_hash($media); |
|
163
|
|
|
$this->clones[$hash] = clone $media; |
|
164
|
|
|
|
|
165
|
|
|
if ($this->requireThumbnails()) { |
|
166
|
|
|
$this->thumbnail->delete($this, $media); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function postRemove(MediaInterface $media): void |
|
171
|
|
|
{ |
|
172
|
|
|
$hash = spl_object_hash($media); |
|
173
|
|
|
|
|
174
|
|
|
if (isset($this->clones[$hash])) { |
|
175
|
|
|
$media = $this->clones[$hash]; |
|
176
|
|
|
unset($this->clones[$hash]); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$path = $this->getReferenceImage($media); |
|
180
|
|
|
|
|
181
|
|
|
if ($this->getFilesystem()->has($path)) { |
|
182
|
|
|
$this->getFilesystem()->delete($path); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function generatePath(MediaInterface $media) |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->pathGenerator->generatePath($media); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function getFormats() |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->formats; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function setName($name): void |
|
197
|
|
|
{ |
|
198
|
|
|
$this->name = $name; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function getName() |
|
202
|
|
|
{ |
|
203
|
|
|
return $this->name; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function setTemplates(array $templates): void |
|
207
|
|
|
{ |
|
208
|
|
|
$this->templates = $templates; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function getTemplates() |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->templates; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function getTemplate($name) |
|
217
|
|
|
{ |
|
218
|
|
|
return isset($this->templates[$name]) ? $this->templates[$name] : null; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
public function getResizer() |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->resizer; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function getFilesystem() |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->filesystem; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function getCdn() |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->cdn; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function getCdnPath($relativePath, $isFlushable) |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->getCdn()->getPath($relativePath, $isFlushable); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
public function setResizer(ResizerInterface $resizer): void |
|
242
|
|
|
{ |
|
243
|
|
|
$this->resizer = $resizer; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
public function prePersist(MediaInterface $media): void |
|
247
|
|
|
{ |
|
248
|
|
|
$media->setCreatedAt(new \DateTime()); |
|
249
|
|
|
$media->setUpdatedAt(new \DateTime()); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
public function preUpdate(MediaInterface $media): void |
|
253
|
|
|
{ |
|
254
|
|
|
$media->setUpdatedAt(new \DateTime()); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function validate(ErrorElement $errorElement, MediaInterface $media): void |
|
258
|
|
|
{ |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
abstract protected function doTransform(MediaInterface $media); |
|
262
|
|
|
} |
|
263
|
|
|
|