55use Cloudinary \Cloudinary \Core \CloudinaryImageManager ;
66use Cloudinary \Cloudinary \Core \ConfigurationInterface ;
77use Cloudinary \Cloudinary \Core \Image ;
8+ use Cloudinary \Cloudinary \Model \MediaLibraryMapFactory ;
89use Magento \Framework \App \Filesystem \DirectoryList ;
910use Magento \Framework \File \Uploader ;
11+ use Magento \Framework \Filesystem ;
1012
1113class FileUploader
1214{
@@ -27,20 +29,36 @@ class FileUploader
2729 */
2830 private $ configuration ;
2931
32+ /**
33+ * @var MediaLibraryMapFactory
34+ */
35+ private $ mediaLibraryMapFactory ;
36+
37+ /**
38+ * @var Filesystem
39+ */
40+ private $ filesystem ;
41+
3042 /**
3143 * @method __construct
3244 * @param CloudinaryImageManager $cloudinaryImageManager
3345 * @param DirectoryList $directoryList
3446 * @param ConfigurationInterface $configuration
47+ * @param MediaLibraryMapFactory $mediaLibraryMapFactory
48+ * @param Filesystem $filesystem
3549 */
3650 public function __construct (
3751 CloudinaryImageManager $ cloudinaryImageManager ,
3852 DirectoryList $ directoryList ,
39- ConfigurationInterface $ configuration
53+ ConfigurationInterface $ configuration ,
54+ MediaLibraryMapFactory $ mediaLibraryMapFactory ,
55+ Filesystem $ filesystem
4056 ) {
4157 $ this ->cloudinaryImageManager = $ cloudinaryImageManager ;
4258 $ this ->directoryList = $ directoryList ;
4359 $ this ->configuration = $ configuration ;
60+ $ this ->mediaLibraryMapFactory = $ mediaLibraryMapFactory ;
61+ $ this ->filesystem = $ filesystem ;
4462 }
4563
4664 /**
@@ -56,10 +74,38 @@ public function afterSave(Uploader $uploader, $result)
5674
5775 $ filepath = $ this ->absoluteFilePath ($ result );
5876
59- if ($ this ->isAllowedImageExtension ($ filepath ) && $ this ->isMediaFilePath ($ filepath ) && !$ this ->isMediaTmpFilePath ($ filepath )) {
60- $ this ->cloudinaryImageManager ->uploadAndSynchronise (
61- Image::fromPath ($ filepath , $ this ->mediaRelativePath ($ filepath ))
62- );
77+
78+
79+ if ($ this ->isAllowedImageExtension ($ filepath ) && $ this ->isMediaFilePath ($ filepath )) {
80+ try {
81+ $ relativePath = $ this ->mediaRelativePath ($ filepath );
82+
83+ // Normalize path: remove /tmp/ to match final location
84+ $ normalizedPath = preg_replace ('#/tmp/# ' , '/ ' , $ relativePath );
85+
86+ $ image = Image::fromPath ($ filepath , $ normalizedPath );
87+ $ uploadResults = $ this ->cloudinaryImageManager ->uploadAndSynchronise ($ image );
88+
89+ // fallback to existing files
90+ if (isset ($ uploadResults ['file_exists ' ]) && $ uploadResults ['file_exists ' ]) {
91+ $ normalizedPath = $ uploadResults ['file_exists ' ];
92+ }
93+
94+ // Only save mapping if upload succeeded and settings enabled
95+ $ cldUniqid = $ this ->configuration ->generateCLDuniqid ();
96+ // Save public_id without extension (e.g., media/catalog/category/_4_2_3)
97+ $ publicId = isset ($ uploadResults ['public_id ' ]) ? $ uploadResults ['public_id ' ] : preg_replace ('/\.[^.]+$/ ' , '' , $ normalizedPath );
98+ $ this ->mediaLibraryMapFactory ->create ()
99+ ->setCldUniqid ($ cldUniqid )
100+ ->setCldPublicId ($ publicId )
101+ ->setFreeTransformation (null )
102+ ->save ();
103+
104+ } catch (\Cloudinary \Cloudinary \Core \Exception \FileExists $ e ) {
105+ // Image already exists in Cloudinary - skip mapping creation
106+ } catch (\Exception $ e ) {
107+ // Upload failed - skip mapping creation
108+ }
63109 }
64110
65111 return $ result ;
@@ -89,7 +135,13 @@ protected function isMediaFilePath($filepath)
89135 */
90136 protected function isMediaTmpFilePath ($ filepath )
91137 {
92- return strpos ($ filepath , sprintf ('%s/tmp ' , $ this ->directoryList ->getPath ('media ' ))) === 0 ;
138+ // Check for standard tmp path
139+ $ standardTmpCheck = strpos ($ filepath , sprintf ('%s/tmp ' , $ this ->directoryList ->getPath ('media ' ))) === 0 ;
140+
141+ // Also check for catalog/tmp (category images go to catalog/tmp/category)
142+ $ catalogTmpCheck = strpos ($ filepath , $ this ->directoryList ->getPath ('media ' ) . '/catalog/tmp ' ) === 0 ;
143+
144+ return $ standardTmpCheck || $ catalogTmpCheck ;
93145 }
94146
95147 /**
0 commit comments