1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component 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\ApiComponentBundle\Uploadable; |
15
|
|
|
|
16
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
17
|
|
|
use Ramsey\Uuid\Uuid; |
18
|
|
|
use Silverback\ApiComponentBundle\Annotation\UploadableField; |
19
|
|
|
use Silverback\ApiComponentBundle\AnnotationReader\UploadableAnnotationReader; |
20
|
|
|
use Silverback\ApiComponentBundle\Flysystem\FilesystemProvider; |
21
|
|
|
use Silverback\ApiComponentBundle\Model\Uploadable\UploadedBase64EncodedFile; |
22
|
|
|
use Silverback\ApiComponentBundle\Utility\ClassMetadataTrait; |
23
|
|
|
use Symfony\Component\HttpFoundation\FileBag; |
24
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Daniel West <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class UploadableHelper |
30
|
|
|
{ |
31
|
|
|
use ClassMetadataTrait; |
32
|
|
|
|
33
|
|
|
private UploadableAnnotationReader $annotationReader; |
34
|
|
|
private FilesystemProvider $filesystemProvider; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
ManagerRegistry $registry, |
38
|
|
|
UploadableAnnotationReader $annotationReader, |
39
|
|
|
FilesystemProvider $filesystemProvider |
40
|
|
|
) { |
41
|
|
|
$this->initRegistry($registry); |
42
|
|
|
$this->annotationReader = $annotationReader; |
43
|
|
|
$this->filesystemProvider = $filesystemProvider; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setUploadedFilesFromFileBag(object $object, FileBag $fileBag): void |
47
|
|
|
{ |
48
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
49
|
|
|
$configuredProperties = $this->annotationReader->getConfiguredProperties($object, false, true); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var UploadableField[] $configuredProperties |
53
|
|
|
*/ |
54
|
|
|
foreach ($configuredProperties as $fileProperty => $fieldConfiguration) { |
55
|
|
|
if ($file = $fileBag->get($fileProperty, null)) { |
56
|
|
|
$propertyAccessor->setValue($object, $fileProperty, $file); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function persistFiles(object $object): void |
62
|
|
|
{ |
63
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
64
|
|
|
$classMetadata = $this->getClassMetadata($object); |
65
|
|
|
|
66
|
|
|
// $configuration = $this->annotationReader->getConfiguration($object); |
67
|
|
|
$configuredProperties = $this->annotationReader->getConfiguredProperties($object, true, true); |
68
|
|
|
/** |
69
|
|
|
* @var UploadableField[] $configuredProperties |
70
|
|
|
*/ |
71
|
|
|
foreach ($configuredProperties as $fileProperty => $fieldConfiguration) { |
72
|
|
|
$currentFilepath = $classMetadata->getFieldValue($object, $fieldConfiguration->property); |
73
|
|
|
if ($currentFilepath) { |
74
|
|
|
$this->removeFilepath($object, $fieldConfiguration); |
75
|
|
|
} |
76
|
|
|
/** @var UploadedBase64EncodedFile|null $file */ |
77
|
|
|
$file = $propertyAccessor->getValue($object, $fileProperty); |
78
|
|
|
if (!$file) { |
79
|
|
|
$classMetadata->setFieldValue($object, $fieldConfiguration->property, null); |
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$filesystem = $this->filesystemProvider->getFilesystem($fieldConfiguration->adapter); |
84
|
|
|
|
85
|
|
|
$path = $fieldConfiguration->prefix ?? ''; |
86
|
|
|
$path .= Uuid::uuid4() . $file->getFilename(); |
87
|
|
|
$stream = fopen($file->getRealPath(), 'r'); |
88
|
|
|
$filesystem->writeStream($path, $stream, [ |
89
|
|
|
'mimetype' => $file->getMimeType(), |
90
|
|
|
]); |
91
|
|
|
$classMetadata->setFieldValue($object, $fieldConfiguration->property, $path); |
92
|
|
|
$propertyAccessor->setValue($object, $fileProperty, null); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function removeFilepath(object $object, UploadableField $fieldConfiguration): void |
97
|
|
|
{ |
98
|
|
|
$classMetadata = $this->getClassMetadata($object); |
99
|
|
|
|
100
|
|
|
$filesystem = $this->filesystemProvider->getFilesystem($fieldConfiguration->adapter); |
101
|
|
|
$currentFilepath = $classMetadata->getFieldValue($object, $fieldConfiguration->property); |
102
|
|
|
$filesystem->delete($currentFilepath); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|