@@ -200,10 +200,21 @@ public function moveFileFromTmp($imageName)
200
200
{
201
201
$ baseTmpPath = $ this ->getBaseTmpPath ();
202
202
$ basePath = $ this ->getBasePath ();
203
-
203
+
204
204
$ baseImagePath = $ this ->getFilePath ($ basePath , $ imageName );
205
205
$ baseTmpImagePath = $ this ->getFilePath ($ baseTmpPath , $ imageName );
206
-
206
+
207
+ $ destinationFileAbsolutePath = $ this ->mediaDirectory ->getAbsolutePath ($ baseImagePath );
208
+ $ fileInfo = pathinfo ($ destinationFileAbsolutePath );
209
+ if (file_exists ($ destinationFileAbsolutePath )) {
210
+ $ index = 1 ;
211
+ $ imageName = $ fileInfo ['filename ' ] . '. ' . $ fileInfo ['extension ' ];
212
+ while (file_exists ($ fileInfo ['dirname ' ] . '/ ' . $ imageName )) {
213
+ $ imageName = $ fileInfo ['filename ' ] . '_ ' . $ index . '. ' . $ fileInfo ['extension ' ];
214
+ $ index ++;
215
+ }
216
+ $ baseImagePath = $ this ->getFilePath ($ basePath , $ imageName );
217
+ }
207
218
try {
208
219
$ this ->coreFileStorageDatabase ->copyFile (
209
220
$ baseTmpImagePath ,
@@ -222,7 +233,7 @@ public function moveFileFromTmp($imageName)
222
233
return $ imageName ;
223
234
}
224
235
225
- /**
236
+ /**
226
237
* Checking file for save and save it to tmp dir
227
238
*
228
239
* @param string $fileId
@@ -233,16 +244,16 @@ public function moveFileFromTmp($imageName)
233
244
*/
234
245
public function saveFileToTmpDir ($ fileId )
235
246
{
236
- $ baseTmpPath = $ this ->getBaseTmpPath ();
247
+ $ baseTmpPath = $ this ->getBaseTmpPath ();
237
248
238
249
/** @var \Magento\MediaStorage\Model\File\Uploader $uploader */
239
250
$ uploader = $ this ->uploaderFactory ->create (['fileId ' => $ fileId ]);
240
251
$ uploader ->setAllowedExtensions ($ this ->getAllowedExtensions ());
241
252
$ uploader ->setAllowRenameFiles (true );
242
253
if (!$ uploader ->checkMimeType ($ this ->allowedMimeTypes )) {
243
254
throw new \Magento \Framework \Exception \LocalizedException (__ ('File validation failed. ' ));
244
- }
245
- $ result = $ uploader ->save ($ this ->mediaDirectory ->getAbsolutePath ($ baseTmpPath ));
255
+ }
256
+ $ result = $ uploader ->save ($ this ->mediaDirectory ->getAbsolutePath ($ baseTmpPath ));
246
257
unset($ result ['path ' ]);
247
258
248
259
if (!$ result ) {
0 commit comments