-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMedia.php
More file actions
264 lines (213 loc) · 7.11 KB
/
Copy pathMedia.php
File metadata and controls
264 lines (213 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace JoliCode\MediaBundle\Model;
use JoliCode\MediaBundle\Binary\Binary;
use JoliCode\MediaBundle\Exception\MediaNotFoundException;
use JoliCode\MediaBundle\Library\Library;
use JoliCode\MediaBundle\Resolver\Resolver;
use JoliCode\MediaBundle\Storage\OriginalStorage;
use JoliCode\MediaBundle\Variation\Variation;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class Media implements StorableInterface
{
/**
* @var callable(): Resolver|null
*/
public static $resolverInitializer;
private static Resolver $resolver;
/**
* @var array<string, MediaVariation>
*/
private array $variations = [];
private ?bool $stored = null;
public function __construct(
private readonly string $path,
private readonly OriginalStorage $storage,
private ?Binary $binary = null,
) {
}
public function __serialize(): array
{
return [
'path' => $this->path,
'library' => $this->storage->getLibrary()->getName(),
];
}
public function __unserialize(array $data): void
{
$path = $data['path'] ?? $data[0] ?? null;
$libraryName = $data['library'] ?? $data[1] ?? null;
if (!\is_string($path) || !\is_string($libraryName)) {
throw new \UnexpectedValueException('Invalid serialized media payload.');
}
try {
$resolvedMedia = $this->getResolver()->resolveMedia($path, $libraryName);
} catch (MediaNotFoundException) {
$resolvedMedia = $this->getResolver()->createUnresolvedMedia($path, $libraryName);
}
$this->path = $resolvedMedia->path;
$this->storage = $resolvedMedia->storage;
$this->binary = null;
$this->stored = null;
$this->variations = [];
}
public function addVariation(MediaVariation $variation): void
{
$this->variations[$variation->getVariation()->getName()] = $variation;
}
public function createVariation(string|Variation $variation): MediaVariation
{
if (\is_string($variation)) {
$variation = $this->storage->getLibrary()->getVariation($variation);
}
return $variation->getForMedia($this);
}
public function delete(): void
{
if ($this->isStored()) {
$this->storage->delete($this->path);
}
$this->stored = false;
$this->binary = null;
$this->variations = [];
}
public function getBinary(): Binary
{
if (!$this->binary instanceof Binary) {
if (!$this->isStored()) {
throw new \RuntimeException('This media is not stored and its binary is not set');
}
$this->binary = $this->storage->get($this->path);
}
return $this->binary;
}
public function getFilename(): string
{
return basename($this->path);
}
public function getFileSize(): int
{
return $this->storage->getFileSize($this->path);
}
public function getFileType(): string
{
$mimeTypeParts = explode('/', $this->getMimeType());
if (2 !== \count($mimeTypeParts)) {
return 'file';
}
if (\in_array($mimeTypeParts[0], ['audio', 'image', 'video'], true)) {
return $mimeTypeParts[0];
}
return match ($this->getMimeType()) {
'application/msword' => 'word',
'application/pdf' => 'pdf',
'application/vnd.ms-excel' => 'excel',
'application/vnd.ms-powerpoint' => 'powerpoint',
'application/vnd.oasis.opendocument.presentation' => 'powerpoint',
'application/vnd.oasis.opendocument.spreadsheet' => 'excel',
'application/vnd.oasis.opendocument.text' => 'word',
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'powerpoint',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'excel',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'word',
'application/xml' => 'xml',
'application/zip' => 'zip',
'text/csv' => 'csv',
'text/html' => 'html',
'text/plain' => 'text',
'text/xml' => 'xml',
default => 'file',
};
}
public function getFolderPath(): string
{
return \dirname($this->path);
}
public function getFormat(): string
{
if ($this->binary instanceof Binary) {
return $this->binary->getFormat();
}
return $this->storage->getFormat($this->path);
}
public function getMimeType(): string
{
if ($this->binary instanceof Binary) {
return $this->binary->getMimeType();
}
return $this->storage->getMimeType($this->path);
}
public function getLastModified(): \DateTime
{
$timestamp = $this->storage->getLastModified($this->path);
return new \DateTime('@' . $timestamp);
}
public function getLibrary(): Library
{
return $this->storage->getLibrary();
}
public function getPath(): string
{
return $this->path;
}
/**
* @return false|array{height: int, width: int}
*/
public function getPixelDimensions(): array|false
{
return $this->storage->getPixelDimensions($this->path);
}
public function getStorage(): OriginalStorage
{
return $this->storage;
}
public function getUrl(
int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH,
): string {
return $this->storage->getUrl($this->path, $referenceType);
}
public function getVariation(string $variationName): MediaVariation
{
if (!isset($this->variations[$variationName])) {
throw new \InvalidArgumentException(\sprintf('Variation "%s" not found', $variationName));
}
return $this->variations[$variationName];
}
/**
* @return array<string, MediaVariation>
*/
public function getVariations(): array
{
return $this->variations;
}
public function hasVariation(string $variationName): bool
{
return isset($this->variations[$variationName]);
}
public function isStored(): bool
{
if (null === $this->stored) {
$this->stored = $this->storage->has($this->path);
}
return $this->stored;
}
public function store(?Binary $binary = null): void
{
if ($binary instanceof Binary) {
$this->binary = $binary;
}
if (!$this->binary instanceof Binary) {
throw new \RuntimeException('No binary set to store');
}
$this->storage->createMediaFromBinary($this->path, $this->binary);
$this->stored = true;
}
private function getResolver(): Resolver
{
if (!isset(self::$resolver)) {
if (!isset(self::$resolverInitializer)) {
throw new \LogicException('Resolver Initializer is not set.');
}
self::$resolver = (self::$resolverInitializer)();
}
return self::$resolver;
}
}