-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFileUploader.php
More file actions
165 lines (138 loc) · 5.14 KB
/
FileUploader.php
File metadata and controls
165 lines (138 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace Cloudinary\Cloudinary\Plugin;
use Cloudinary\Cloudinary\Core\CloudinaryImageManager;
use Cloudinary\Cloudinary\Core\ConfigurationInterface;
use Cloudinary\Cloudinary\Core\Image;
use Cloudinary\Cloudinary\Model\MediaLibraryMapFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\File\Uploader;
use Magento\Framework\Filesystem;
class FileUploader
{
const ALLOWED_EXTENSIONS = ['png', 'gif', 'jpg', 'jpeg'];
/**
* @var CloudinaryImageManager
*/
private $cloudinaryImageManager;
/**
* @var DirectoryList
*/
private $directoryList;
/**
* @var ConfigurationInterface
*/
private $configuration;
/**
* @var MediaLibraryMapFactory
*/
private $mediaLibraryMapFactory;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @method __construct
* @param CloudinaryImageManager $cloudinaryImageManager
* @param DirectoryList $directoryList
* @param ConfigurationInterface $configuration
* @param MediaLibraryMapFactory $mediaLibraryMapFactory
* @param Filesystem $filesystem
*/
public function __construct(
CloudinaryImageManager $cloudinaryImageManager,
DirectoryList $directoryList,
ConfigurationInterface $configuration,
MediaLibraryMapFactory $mediaLibraryMapFactory,
Filesystem $filesystem
) {
$this->cloudinaryImageManager = $cloudinaryImageManager;
$this->directoryList = $directoryList;
$this->configuration = $configuration;
$this->mediaLibraryMapFactory = $mediaLibraryMapFactory;
$this->filesystem = $filesystem;
}
/**
* @param Uploader $uploader
* @param array $result
* @return array
*/
public function afterSave(Uploader $uploader, $result)
{
if (!$this->configuration->isEnabled() || !$this->configuration->hasEnvironmentVariable()) {
return $result;
}
$filepath = $this->absoluteFilePath($result);
if ($this->isAllowedImageExtension($filepath) && $this->isMediaFilePath($filepath)) {
try {
$relativePath = $this->mediaRelativePath($filepath);
// Normalize path: remove /tmp/ to match final location
$normalizedPath = preg_replace('#/tmp/#', '/', $relativePath);
$image = Image::fromPath($filepath, $normalizedPath);
$uploadResults = $this->cloudinaryImageManager->uploadAndSynchronise($image);
// fallback to existing files
if (isset($uploadResults['file_exists']) && $uploadResults['file_exists']) {
$normalizedPath = $uploadResults['file_exists'];
}
// Only save mapping if upload succeeded and settings enabled
$cldUniqid = $this->configuration->generateCLDuniqid();
// Save public_id without extension (e.g., media/catalog/category/_4_2_3)
$publicId = isset($uploadResults['public_id']) ? $uploadResults['public_id'] : preg_replace('/\.[^.]+$/', '', $normalizedPath);
$this->mediaLibraryMapFactory->create()
->setCldUniqid($cldUniqid)
->setCldPublicId($publicId)
->setFreeTransformation(null)
->save();
} catch (\Cloudinary\Cloudinary\Core\Exception\FileExists $e) {
// Image already exists in Cloudinary - skip mapping creation
} catch (\Exception $e) {
// Upload failed - skip mapping creation
}
}
return $result;
}
/**
* @param string $filepath
* @return string
*/
protected function isAllowedImageExtension($filepath)
{
return in_array(pathinfo($filepath, PATHINFO_EXTENSION), self::ALLOWED_EXTENSIONS);
}
/**
* @param string $filepath
* @return bool
*/
protected function isMediaFilePath($filepath)
{
return strpos($filepath, $this->directoryList->getPath('media')) === 0;
}
/**
* @param string $filepath
* @return string
*/
protected function isMediaTmpFilePath($filepath)
{
// Check for standard tmp path
$standardTmpCheck = strpos($filepath, sprintf('%s/tmp', $this->directoryList->getPath('media'))) === 0;
// Also check for catalog/tmp (category images go to catalog/tmp/category)
$catalogTmpCheck = strpos($filepath, $this->directoryList->getPath('media') . '/catalog/tmp') === 0;
return $standardTmpCheck || $catalogTmpCheck;
}
/**
* @param array $result
* @return string
*/
protected function absoluteFilePath(array $result)
{
return sprintf('%s%s%s', $result['path'], DIRECTORY_SEPARATOR, $result['file']);
}
/**
* @param string $filepath
* @return string
*/
protected function mediaRelativePath($filepath)
{
$pubPath = $this->directoryList->getPath(DirectoryList::PUB) . DIRECTORY_SEPARATOR;
return (strpos($filepath, $pubPath) === 0) ? str_replace($pubPath, '', $filepath) : $filepath;
}
}