|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Helper\Uploadable; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
17
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
18
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
19
|
|
|
use Liip\ImagineBundle\Service\FilterService; |
|
20
|
|
|
use Silverback\ApiComponentsBundle\Annotation\UploadableField; |
|
21
|
|
|
use Silverback\ApiComponentsBundle\AttributeReader\UploadableAttributeReader; |
|
22
|
|
|
use Silverback\ApiComponentsBundle\Entity\Utility\ImagineFiltersInterface; |
|
23
|
|
|
use Silverback\ApiComponentsBundle\Flysystem\FilesystemProvider; |
|
24
|
|
|
use Silverback\ApiComponentsBundle\Imagine\CacheManager; |
|
25
|
|
|
use Silverback\ApiComponentsBundle\Imagine\FlysystemDataLoader; |
|
26
|
|
|
use Silverback\ApiComponentsBundle\Model\Uploadable\UploadedDataUriFile; |
|
27
|
|
|
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait; |
|
28
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\FileBag; |
|
30
|
|
|
use Symfony\Component\HttpFoundation\HeaderUtils; |
|
31
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
32
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
33
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
34
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @author Daniel West <[email protected]> |
|
38
|
|
|
*/ |
|
39
|
|
|
class UploadableFileManager |
|
40
|
|
|
{ |
|
41
|
|
|
use ClassMetadataTrait; |
|
42
|
|
|
|
|
43
|
|
|
private UploadableAttributeReader $annotationReader; |
|
44
|
|
|
private FilesystemProvider $filesystemProvider; |
|
45
|
|
|
private FlysystemDataLoader $flysystemDataLoader; |
|
46
|
|
|
private FileInfoCacheManager $fileInfoCacheManager; |
|
47
|
|
|
private ?CacheManager $imagineCacheManager; |
|
48
|
|
|
private ?FilterService $filterService; |
|
49
|
|
|
private ArrayCollection $deletedFields; |
|
50
|
|
|
|
|
51
|
|
|
public function __construct(ManagerRegistry $registry, UploadableAttributeReader $annotationReader, FilesystemProvider $filesystemProvider, FlysystemDataLoader $flysystemDataLoader, FileInfoCacheManager $fileInfoCacheManager, ?CacheManager $imagineCacheManager, ?FilterService $filterService = null) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->initRegistry($registry); |
|
54
|
|
|
$this->annotationReader = $annotationReader; |
|
55
|
|
|
$this->filesystemProvider = $filesystemProvider; |
|
56
|
|
|
$this->flysystemDataLoader = $flysystemDataLoader; |
|
57
|
|
|
$this->fileInfoCacheManager = $fileInfoCacheManager; |
|
58
|
|
|
$this->imagineCacheManager = $imagineCacheManager; |
|
59
|
|
|
$this->filterService = $filterService; |
|
60
|
|
|
$this->deletedFields = new ArrayCollection(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function addDeletedField($field): void |
|
64
|
|
|
{ |
|
65
|
|
|
$this->deletedFields->add($field); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function processClonedUploadable(object $oldObject, object $newObject): object |
|
69
|
|
|
{ |
|
70
|
|
|
if (!$this->annotationReader->isConfigured($oldObject)) { |
|
71
|
|
|
throw new \InvalidArgumentException('The old object is not configured as uploadable'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (\get_class($oldObject) !== \get_class($newObject)) { |
|
75
|
|
|
throw new \InvalidArgumentException('The objects must be the same class'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
79
|
|
|
$configuredProperties = $this->annotationReader->getConfiguredProperties($oldObject, false); |
|
80
|
|
|
foreach ($configuredProperties as $fieldConfiguration) { |
|
81
|
|
|
if ($propertyAccessor->getValue($oldObject, $fieldConfiguration->property)) { |
|
82
|
|
|
$newPath = $this->copyFilepath($oldObject, $fieldConfiguration); |
|
83
|
|
|
$propertyAccessor->setValue($newObject, $fieldConfiguration->property, $newPath); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $newObject; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function setUploadedFilesFromFileBag(object $object, FileBag $fileBag): void |
|
91
|
|
|
{ |
|
92
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
93
|
|
|
$configuredProperties = $this->annotationReader->getConfiguredProperties($object, false); |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @var UploadableField[] $configuredProperties |
|
97
|
|
|
*/ |
|
98
|
|
|
foreach ($configuredProperties as $fileProperty => $fieldConfiguration) { |
|
99
|
|
|
if ($file = $fileBag->get($fileProperty)) { |
|
100
|
|
|
$propertyAccessor->setValue($object, $fileProperty, $file); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function storeFilesMetadata(object $object): void |
|
106
|
|
|
{ |
|
107
|
|
|
$configuredProperties = $this->annotationReader->getConfiguredProperties($object, true); |
|
108
|
|
|
$classMetadata = $this->getClassMetadata($object); |
|
109
|
|
|
|
|
110
|
|
|
foreach ($configuredProperties as $fileProperty => $fieldConfiguration) { |
|
111
|
|
|
// Let the data loader which should be configured for imagine to know which adapter to use |
|
112
|
|
|
$this->flysystemDataLoader->setAdapter($fieldConfiguration->adapter); |
|
113
|
|
|
|
|
114
|
|
|
$filename = $classMetadata->getFieldValue($object, $fieldConfiguration->property); |
|
115
|
|
|
if ($filename && $object instanceof ImagineFiltersInterface && $this->filterService) { |
|
116
|
|
|
$filters = $object->getImagineFilters($fileProperty, null); |
|
117
|
|
|
foreach ($filters as $filter) { |
|
118
|
|
|
// This will trigger the cached file to be store |
|
119
|
|
|
// When cached files are store we save the file info |
|
120
|
|
|
$this->filterService->getUrlOfFilteredImage($filename, $filter); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function persistFiles(object $object): void |
|
127
|
|
|
{ |
|
128
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
129
|
|
|
$classMetadata = $this->getClassMetadata($object); |
|
130
|
|
|
|
|
131
|
|
|
$configuredProperties = $this->annotationReader->getConfiguredProperties($object, true); |
|
132
|
|
|
foreach ($configuredProperties as $fileProperty => $fieldConfiguration) { |
|
133
|
|
|
// this is null if null is submitted as the value... also null if not submitted |
|
134
|
|
|
/** @var File|UploadedDataUriFile|null $file */ |
|
135
|
|
|
$file = $propertyAccessor->getValue($object, $fileProperty); |
|
136
|
|
|
if (!$file) { |
|
137
|
|
|
// so we need to know if it was a deleted field from the denormalizer |
|
138
|
|
|
if ($this->deletedFields->contains($fieldConfiguration->property)) { |
|
139
|
|
|
$this->deleteFileForField($object, $classMetadata, $fieldConfiguration); |
|
140
|
|
|
$classMetadata->setFieldValue($object, $fieldConfiguration->property, null); |
|
141
|
|
|
} |
|
142
|
|
|
continue; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$this->deleteFileForField($object, $classMetadata, $fieldConfiguration); |
|
146
|
|
|
$filesystem = $this->filesystemProvider->getFilesystem($fieldConfiguration->adapter); |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
$path = $fieldConfiguration->prefix ?? ''; |
|
149
|
|
|
$path .= $file->getFilename(); |
|
150
|
|
|
$stream = fopen($file->getRealPath(), 'rb'); |
|
151
|
|
|
$filesystem->writeStream( |
|
152
|
|
|
$path, |
|
153
|
|
|
$stream, |
|
154
|
|
|
[ |
|
155
|
|
|
'mimetype' => $file->getMimeType(), |
|
156
|
|
|
] |
|
157
|
|
|
); |
|
158
|
|
|
$classMetadata->setFieldValue($object, $fieldConfiguration->property, $path); |
|
159
|
|
|
$propertyAccessor->setValue($object, $fileProperty, null); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function deleteFiles(object $object): void |
|
164
|
|
|
{ |
|
165
|
|
|
$classMetadata = $this->getClassMetadata($object); |
|
166
|
|
|
|
|
167
|
|
|
$configuredProperties = $this->annotationReader->getConfiguredProperties($object, true); |
|
168
|
|
|
foreach ($configuredProperties as $fileProperty => $fieldConfiguration) { |
|
169
|
|
|
$this->deleteFileForField($object, $classMetadata, $fieldConfiguration); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
private function deleteFileForField(object $object, ClassMetadata $classMetadata, UploadableField $fieldConfiguration): void |
|
174
|
|
|
{ |
|
175
|
|
|
$currentFilepath = $classMetadata->getFieldValue($object, $fieldConfiguration->property); |
|
176
|
|
|
if ($currentFilepath) { |
|
177
|
|
|
$this->removeFilepath($object, $fieldConfiguration); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function getFileResponse(object $object, string $property, bool $forceDownload = false): Response |
|
182
|
|
|
{ |
|
183
|
|
|
try { |
|
184
|
|
|
$reflectionProperty = new \ReflectionProperty($object, $property); |
|
185
|
|
|
} catch (\ReflectionException $exception) { |
|
186
|
|
|
throw new NotFoundHttpException($exception->getMessage()); |
|
187
|
|
|
} |
|
188
|
|
|
if (!$this->annotationReader->isFieldConfigured($reflectionProperty)) { |
|
189
|
|
|
throw new NotFoundHttpException(sprintf('field configuration not found for %s', $property)); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$propertyConfiguration = $this->annotationReader->getPropertyConfiguration($reflectionProperty); |
|
193
|
|
|
|
|
194
|
|
|
$filesystem = $this->filesystemProvider->getFilesystem($propertyConfiguration->adapter); |
|
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
$classMetadata = $this->getClassMetadata($object); |
|
197
|
|
|
|
|
198
|
|
|
$filePath = $classMetadata->getFieldValue($object, $propertyConfiguration->property); |
|
199
|
|
|
if (empty($filePath)) { |
|
200
|
|
|
return new Response('The file path for this resource is empty', Response::HTTP_NOT_FOUND); |
|
201
|
|
|
} |
|
202
|
|
|
$response = new StreamedResponse(); |
|
203
|
|
|
$response->setCallback( |
|
204
|
|
|
static function () use ($filesystem, $filePath) { |
|
205
|
|
|
$outputStream = fopen('php://output', 'w'); |
|
206
|
|
|
$fileStream = $filesystem->readStream($filePath); |
|
207
|
|
|
stream_copy_to_stream($fileStream, $outputStream); |
|
208
|
|
|
} |
|
209
|
|
|
); |
|
210
|
|
|
$response->headers->set('Content-Type', $filesystem->mimeType($filePath)); |
|
211
|
|
|
|
|
212
|
|
|
$disposition = HeaderUtils::makeDisposition($forceDownload ? HeaderUtils::DISPOSITION_ATTACHMENT : HeaderUtils::DISPOSITION_INLINE, $filePath); |
|
213
|
|
|
$response->headers->set('Content-Disposition', $disposition); |
|
214
|
|
|
|
|
215
|
|
|
return $response; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
private function removeFilepath(object $object, UploadableField $fieldConfiguration): void |
|
219
|
|
|
{ |
|
220
|
|
|
$classMetadata = $this->getClassMetadata($object); |
|
221
|
|
|
|
|
222
|
|
|
$filesystem = $this->filesystemProvider->getFilesystem($fieldConfiguration->adapter); |
|
|
|
|
|
|
223
|
|
|
$currentFilepath = $classMetadata->getFieldValue($object, $fieldConfiguration->property); |
|
224
|
|
|
$this->fileInfoCacheManager->deleteCaches([$currentFilepath], [null]); |
|
225
|
|
|
if ($this->imagineCacheManager) { |
|
226
|
|
|
$this->imagineCacheManager->remove([$currentFilepath], null); |
|
227
|
|
|
} |
|
228
|
|
|
if ($filesystem->fileExists($currentFilepath)) { |
|
229
|
|
|
$filesystem->delete($currentFilepath); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
private function copyFilepath(object $object, UploadableField $fieldConfiguration): ?string |
|
234
|
|
|
{ |
|
235
|
|
|
$classMetadata = $this->getClassMetadata($object); |
|
236
|
|
|
|
|
237
|
|
|
$filesystem = $this->filesystemProvider->getFilesystem($fieldConfiguration->adapter); |
|
|
|
|
|
|
238
|
|
|
$currentFilepath = $classMetadata->getFieldValue($object, $fieldConfiguration->property); |
|
239
|
|
|
if (!$filesystem->fileExists($currentFilepath)) { |
|
240
|
|
|
return null; |
|
241
|
|
|
} |
|
242
|
|
|
$pathInfo = pathinfo($currentFilepath); |
|
243
|
|
|
$basename = $pathInfo['filename']; |
|
244
|
|
|
$extension = $pathInfo['extension'] ?? null; |
|
245
|
|
|
if (!empty($extension)) { |
|
246
|
|
|
$extension = sprintf('.%s', $extension); |
|
247
|
|
|
} |
|
248
|
|
|
$num = 1; |
|
249
|
|
|
while ($filesystem->fileExists($newFilepath = sprintf('%s_%d%s', $basename, $num, $extension))) { |
|
250
|
|
|
++$num; |
|
251
|
|
|
} |
|
252
|
|
|
$filesystem->copy($currentFilepath, $newFilepath); |
|
253
|
|
|
|
|
254
|
|
|
return $newFilepath; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|