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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Theme wysiwyg storage model
*/
namespace Magento\Theme\Model\Wysiwyg;
use Magento\Framework\App\Filesystem\DirectoryList;
/**
* Class Storage
*
* @package Magento\Theme\Model\Wysiwyg
*/
class Storage
{
/**
* Type font
*/
const TYPE_FONT = 'font';
/**
* Type image
*/
const TYPE_IMAGE = 'image';
/**
* \Directory for image thumbnail
*/
const THUMBNAIL_DIRECTORY = '.thumbnail';
/**
* Image thumbnail width
*/
const THUMBNAIL_WIDTH = 100;
/**
* Image thumbnail height
*/
const THUMBNAIL_HEIGHT = 100;
/**
* \Directory name regular expression
*/
const DIRECTORY_NAME_REGEXP = '/^[a-z0-9\-\_]+$/si';
/**
* Storage helper
*
* @var \Magento\Theme\Helper\Storage
*/
protected $_helper;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $_objectManager;
/**
* @var \Magento\Framework\Image\AdapterFactory
*/
protected $_imageFactory;
/**
* @var \Magento\Framework\Filesystem\Directory\Write
*/
protected $mediaWriteDirectory;
/**
* @var \Magento\Framework\Url\EncoderInterface
*/
protected $urlEncoder;
/**
* @var \Magento\Framework\Url\DecoderInterface
*/
protected $urlDecoder;
/**
* Initialize dependencies
*
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Theme\Helper\Storage $helper
* @param \Magento\Framework\ObjectManagerInterface $objectManager
* @param \Magento\Framework\Image\AdapterFactory $imageFactory
* @param \Magento\Framework\Url\EncoderInterface $urlEncoder
* @param \Magento\Framework\Url\DecoderInterface $urlDecoder
*/
public function __construct(
\Magento\Framework\Filesystem $filesystem,
\Magento\Theme\Helper\Storage $helper,
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Framework\Image\AdapterFactory $imageFactory,
\Magento\Framework\Url\EncoderInterface $urlEncoder,
\Magento\Framework\Url\DecoderInterface $urlDecoder
) {
$this->mediaWriteDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->_helper = $helper;
$this->_objectManager = $objectManager;
$this->_imageFactory = $imageFactory;
$this->urlEncoder = $urlEncoder;
$this->urlDecoder = $urlDecoder;
}
/**
* Upload file
*
* @param string $targetPath
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function uploadFile($targetPath)
{
/** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
$uploader = $this->_objectManager->create(
\Magento\MediaStorage\Model\File\Uploader::class,
['fileId' => 'file']
);
$uploader->setAllowedExtensions($this->_helper->getAllowedExtensionsByType());
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$result = $uploader->save($targetPath);
unset($result['path']);
if (!$result) {
throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t upload the file right now.'));
}
$this->_createThumbnail($targetPath . '/' . $uploader->getUploadedFileName());
return $result;
}
/**
* Create thumbnail for image and save it to thumbnails directory
*
* @param string $source
* @return bool|string Resized filepath or false if errors were occurred
*/
public function _createThumbnail($source)
{
if (self::TYPE_IMAGE != $this->_helper->getStorageType() || !$this->mediaWriteDirectory->isFile(
$source
) || !$this->mediaWriteDirectory->isReadable(
$source
)
) {
return false;
}
$thumbnailDir = $this->_helper->getThumbnailDirectory($source);
$thumbnailPath = $thumbnailDir . '/' . pathinfo($source, PATHINFO_BASENAME);
try {
$this->mediaWriteDirectory->isExist($thumbnailDir);
$image = $this->_imageFactory->create();
$image->open($this->mediaWriteDirectory->getAbsolutePath($source));
$image->keepAspectRatio(true);
$image->resize(self::THUMBNAIL_WIDTH, self::THUMBNAIL_HEIGHT);
$image->save($this->mediaWriteDirectory->getAbsolutePath($thumbnailPath));
} catch (\Magento\Framework\Exception\FileSystemException $e) {
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
return false;
}
if ($this->mediaWriteDirectory->isFile($thumbnailPath)) {
return $thumbnailPath;
}
return false;
}
/**
* Create folder
*
* @param string $name
* @param string $path
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function createFolder($name, $path)
{
if (!preg_match(self::DIRECTORY_NAME_REGEXP, $name)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Use only standard alphanumeric, dashes and underscores.')
);
}
if (!$this->mediaWriteDirectory->isWritable($path)) {
$path = $this->_helper->getStorageRoot();
}
$newPath = $path . '/' . $name;
if ($this->mediaWriteDirectory->isExist($newPath)) {
throw new \Magento\Framework\Exception\LocalizedException(__('We found a directory with the same name.'));
}
$this->mediaWriteDirectory->create($newPath);
$result = [
'name' => $name,
'short_name' => $this->_helper->getShortFilename($name),
'path' => str_replace($this->_helper->getStorageRoot(), '', $newPath),
'id' => $this->_helper->convertPathToId($newPath)
];
return $result;
}
/**
* Delete file
*
* @param string $file
* @return \Magento\Theme\Model\Wysiwyg\Storage
*/
public function deleteFile($file)
{
$file = $this->urlDecoder->decode($file);
$path = $this->mediaWriteDirectory->getRelativePath($this->_helper->getCurrentPath());
$filePath = $this->mediaWriteDirectory->getRelativePath($path . '/' . $file);
$thumbnailPath = $this->_helper->getThumbnailDirectory($filePath) . '/' . $file;
if (0 === strpos($filePath, $path) && 0 === strpos($filePath, $this->_helper->getStorageRoot())) {
$this->mediaWriteDirectory->delete($filePath);
$this->mediaWriteDirectory->delete($thumbnailPath);
}
return $this;
}
/**
* Get directory collection
*
* @param string $currentPath
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getDirsCollection($currentPath)
{
if (!$this->mediaWriteDirectory->isExist($currentPath)) {
throw new \Magento\Framework\Exception\LocalizedException(__('We cannot find a directory with this name.'));
}
$paths = $this->mediaWriteDirectory->search('.*', $currentPath);
$directories = [];
foreach ($paths as $path) {
if ($this->mediaWriteDirectory->isDirectory($path)) {
$directories[] = $path;
}
}
return $directories;
}
/**
* Get files collection
*
* @return array
*/
public function getFilesCollection()
{
$paths = $this->mediaWriteDirectory->search('.*', $this->_helper->getCurrentPath());
$files = [];
$requestParams = $this->_helper->getRequestParams();
$storageType = $this->_helper->getStorageType();
foreach ($paths as $path) {
if (!$this->mediaWriteDirectory->isFile($path)) {
continue;
}
$fileName = pathinfo($path, PATHINFO_BASENAME);
$file = ['text' => $fileName, 'id' => $this->urlEncoder->encode($fileName)];
if (self::TYPE_IMAGE == $storageType) {
$requestParams['file'] = $fileName;
$file['thumbnailParams'] = $requestParams;
$size = @getimagesize($path);
if (is_array($size)) {
$file['width'] = $size[0];
$file['height'] = $size[1];
}
}
$files[] = $file;
}
return $files;
}
/**
* Get directories tree array
*
* @return array
*/
public function getTreeArray()
{
$directories = $this->getDirsCollection($this->_helper->getCurrentPath());
$resultArray = [];
foreach ($directories as $path) {
$resultArray[] = [
'text' => $this->_helper->getShortFilename(pathinfo($path, PATHINFO_BASENAME), 20),
'id' => $this->_helper->convertPathToId($path),
'cls' => 'folder'
];
}
return $resultArray;
}
/**
* Delete directory
*
* @param string $path
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function deleteDirectory($path)
{
$rootCmp = rtrim($this->_helper->getStorageRoot(), '/');
$pathCmp = rtrim($path, '/');
if ($rootCmp == $pathCmp) {
throw new \Magento\Framework\Exception\LocalizedException(
__('We can\'t delete root directory %1 right now.', $path)
);
}
return $this->mediaWriteDirectory->delete($path);
}
}