|
@@ -97,14 +97,27 @@ implements FileUploaderInterface
|
97
|
97
|
* @param string $path Chemin relatif du fichier
|
98
|
98
|
* @return string
|
99
|
99
|
*/
|
100
|
|
- public function buildUploadPublicPath(string $path)
|
|
100
|
+ public function buildUploadPublicPath(string $path = null, AbstractEntity $entity = null, string $propertyName = null)
|
101
|
101
|
{
|
102
|
|
- $pathPrefix = $this->getUploadPublicRootPath();
|
103
|
|
-
|
104
|
102
|
// Elimine le slash au début
|
105
|
|
- $path = ltrim($path, '/');
|
|
103
|
+ $path = ltrim($path, DIRECTORY_SEPARATOR);
|
106
|
104
|
|
107
|
|
- return sprintf('%s/%s', $pathPrefix, $path);
|
|
105
|
+ if ($entity && $propertyName) {
|
|
106
|
+ // ici, sert uniquement à tester si la méthode de l'entité existe
|
|
107
|
+ $this->getEntityValue($entity, $propertyName);
|
|
108
|
+
|
|
109
|
+ // plugin vich uploader
|
|
110
|
+ $mapping = $this->factory->fromObject($entity);
|
|
111
|
+ foreach ($mapping as $map) {
|
|
112
|
+
|
|
113
|
+ if ($map->getFileNamePropertyName() == $propertyName) {
|
|
114
|
+ $pathPrefix = $map->getUploadDestination();
|
|
115
|
+ return $pathPrefix . DIRECTORY_SEPARATOR . $path;
|
|
116
|
+ }
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+ $pathPrefix = $this->getUploadPublicRootPath();
|
|
120
|
+ return $pathPrefix . DIRECTORY_SEPARATOR . $path;
|
108
|
121
|
}
|
109
|
122
|
|
110
|
123
|
/**
|
|
@@ -123,6 +136,14 @@ implements FileUploaderInterface
|
123
|
136
|
return sprintf('%s/%s', $pathPrefix, $path);
|
124
|
137
|
}
|
125
|
138
|
|
|
139
|
+ public function getUploadPublicPath(AbstractEntity $entity, string $propertyName)
|
|
140
|
+ {
|
|
141
|
+ $oldFileName = $this->getEntityValue($entity, $propertyName);
|
|
142
|
+
|
|
143
|
+ // fonctionnement normal
|
|
144
|
+ return $this->buildUploadPublicPath($oldFileName, $entity, $propertyName);
|
|
145
|
+ }
|
|
146
|
+
|
126
|
147
|
/**
|
127
|
148
|
* Retourne l'URL complète vers un fichier à partir de son chemin
|
128
|
149
|
* relatif dans le répertoire public des uploads.
|
|
@@ -139,32 +160,36 @@ implements FileUploaderInterface
|
139
|
160
|
return sprintf('%s/%s', $urlPrefix, $path);
|
140
|
161
|
}
|
141
|
162
|
|
142
|
|
- public function cloneFile(AbstractEntity $entity, string $propertyName): string
|
|
163
|
+ private function getEntityValue(AbstractEntity $entity, string $propertyName)
|
|
164
|
+ {
|
|
165
|
+ // test le lien entre le propriété et l'objet
|
|
166
|
+ $getter = 'get' . ucfirst($propertyName);
|
|
167
|
+ if (!method_exists($entity, $getter)) {
|
|
168
|
+ throw new \Exception();
|
|
169
|
+ }
|
|
170
|
+ return $entity->{$getter}();
|
|
171
|
+ }
|
|
172
|
+
|
|
173
|
+ public function generateNewName(string $oldFileName): string
|
143
|
174
|
{
|
144
|
|
- $mapping = $this->factory->fromObject($entity);
|
|
175
|
+ return uniqid() . '.' . pathinfo($oldFileName, PATHINFO_EXTENSION);
|
|
176
|
+ }
|
145
|
177
|
|
146
|
|
- $newFileName = '';
|
|
178
|
+ public function cloneFile(AbstractEntity $entity, string $propertyName): string
|
|
179
|
+ {
|
147
|
180
|
$filesystem = new Filesystem();
|
148
|
|
- foreach ($mapping as $map) {
|
149
|
|
-
|
150
|
|
- if ($map->getFileNamePropertyName() == $propertyName) {
|
151
|
|
- $getter = 'get' . ucfirst($propertyName);
|
152
|
|
- $oldFileName = $entity->$getter();
|
153
|
|
- $oldFilePath = $map->getUploadDestination() . DIRECTORY_SEPARATOR . $oldFileName;
|
154
|
|
-
|
155
|
|
- if (is_file($oldFilePath)) {
|
156
|
|
- $newFileName = uniqid() . '.' . pathinfo($oldFileName, PATHINFO_EXTENSION);
|
157
|
|
- $newFilePath = $map->getUploadDestination() . DIRECTORY_SEPARATOR . $newFileName;
|
158
|
|
-
|
159
|
|
- try {
|
160
|
|
- $filesystem->copy($oldFilePath, $newFilePath);
|
161
|
|
- } catch (\Exception $exp) {
|
162
|
|
- dd($exp->getMessage());
|
163
|
|
- }
|
164
|
|
- }
|
165
|
|
- break;
|
166
|
|
- }
|
|
181
|
+ $oldFileName = $this->getEntityValue($entity, $propertyName);
|
|
182
|
+ $newFileName = $this->generateNewName($oldFileName);
|
|
183
|
+
|
|
184
|
+ try {
|
|
185
|
+ $filesystem->copy(
|
|
186
|
+ $this->buildUploadPublicPath($oldFileName, $entity, $propertyName),
|
|
187
|
+ $this->buildUploadPublicPath($newFileName, $entity, $propertyName)
|
|
188
|
+ );
|
|
189
|
+ } catch (\Exception $exp) {
|
|
190
|
+ dd($exp->getMessage());
|
167
|
191
|
}
|
|
192
|
+
|
168
|
193
|
return $newFileName;
|
169
|
194
|
}
|
170
|
195
|
}
|