6
6
namespace Magento \Framework \File ;
7
7
8
8
use Magento \Framework \App \Filesystem \DirectoryList ;
9
+ use Magento \Framework \App \ObjectManager ;
9
10
use Magento \Framework \Exception \FileSystemException ;
11
+ use Magento \Framework \Filesystem \DriverInterface ;
12
+ use Magento \Framework \Filesystem \DriverPool ;
10
13
use Magento \Framework \Validation \ValidationException ;
11
14
12
15
/**
@@ -143,15 +146,13 @@ class Uploader
143
146
144
147
/**
145
148
* Maximum Image Width resolution in pixels. For image resizing on client side
146
- * @deprecated
147
- * @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxWidth()
149
+ * @deprecated @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxWidth()
148
150
*/
149
151
const MAX_IMAGE_WIDTH = 1920 ;
150
152
151
153
/**
152
154
* Maximum Image Height resolution in pixels. For image resizing on client side
153
- * @deprecated
154
- * @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxHeight()
155
+ * @deprecated @see \Magento\Framework\Image\Adapter\UploadConfigInterface::getMaxHeight()
155
156
*/
156
157
const MAX_IMAGE_HEIGHT = 1200 ;
157
158
@@ -168,21 +169,32 @@ class Uploader
168
169
*/
169
170
private $ directoryList ;
170
171
172
+ /**
173
+ * @var DriverPool|null
174
+ */
175
+ private $ driverPool ;
176
+
177
+ /**
178
+ * @var DriverInterface|null
179
+ */
180
+ private $ fileDriver ;
181
+
171
182
/**
172
183
* Init upload
173
184
*
174
185
* @param string|array $fileId
175
186
* @param \Magento\Framework\File\Mime|null $fileMime
176
187
* @param DirectoryList|null $directoryList
188
+ * @param DriverPool|null $driverPool
177
189
* @throws \DomainException
178
190
*/
179
191
public function __construct (
180
192
$ fileId ,
181
193
Mime $ fileMime = null ,
182
- DirectoryList $ directoryList = null
194
+ DirectoryList $ directoryList = null ,
195
+ DriverPool $ driverPool = null
183
196
) {
184
- $ this ->directoryList = $ directoryList ?: \Magento \Framework \App \ObjectManager::getInstance ()
185
- ->get (DirectoryList::class);
197
+ $ this ->directoryList = $ directoryList ?: ObjectManager::getInstance ()->get (DirectoryList::class);
186
198
187
199
$ this ->_setUploadFileId ($ fileId );
188
200
if (!file_exists ($ this ->_file ['tmp_name ' ])) {
@@ -191,7 +203,8 @@ public function __construct(
191
203
} else {
192
204
$ this ->_fileExists = true ;
193
205
}
194
- $ this ->fileMime = $ fileMime ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (Mime::class);
206
+ $ this ->fileMime = $ fileMime ?: ObjectManager::getInstance ()->get (Mime::class);
207
+ $ this ->driverPool = $ driverPool ;
195
208
}
196
209
197
210
/**
@@ -229,7 +242,7 @@ public function save($destinationFolder, $newFileName = null)
229
242
$ this ->setAllowCreateFolders (true );
230
243
$ this ->_dispretionPath = static ::getDispersionPath ($ fileName );
231
244
$ destinationFile .= $ this ->_dispretionPath ;
232
- $ this ->_createDestinationFolder ($ destinationFile );
245
+ $ this ->createDestinationFolder ($ destinationFile );
233
246
}
234
247
235
248
if ($ this ->_allowRenameFiles ) {
@@ -274,13 +287,11 @@ public function save($destinationFolder, $newFileName = null)
274
287
* @return void
275
288
* @throws FileSystemException
276
289
*/
277
- private function validateDestination ($ destinationFolder )
290
+ private function validateDestination (string $ destinationFolder ): void
278
291
{
279
292
if ($ this ->_allowCreateFolders ) {
280
- $ this ->_createDestinationFolder ($ destinationFolder );
281
- }
282
-
283
- if (!is_writable ($ destinationFolder )) {
293
+ $ this ->createDestinationFolder ($ destinationFolder );
294
+ } elseif (!$ this ->getFileDriver ()->isWritable ($ destinationFolder )) {
284
295
throw new FileSystemException (__ ('Destination folder is not writable or does not exists. ' ));
285
296
}
286
297
}
@@ -654,7 +665,7 @@ private function validateFileId(array $fileId): void
654
665
* @return \Magento\Framework\File\Uploader
655
666
* @throws FileSystemException
656
667
*/
657
- private function _createDestinationFolder ( $ destinationFolder )
668
+ private function createDestinationFolder ( string $ destinationFolder )
658
669
{
659
670
if (!$ destinationFolder ) {
660
671
return $ this ;
@@ -664,11 +675,13 @@ private function _createDestinationFolder($destinationFolder)
664
675
$ destinationFolder = substr ($ destinationFolder , 0 , -1 );
665
676
}
666
677
667
- if (!(@is_dir ($ destinationFolder )
668
- || @mkdir ($ destinationFolder , 0777 , true )
669
- )) {
670
- throw new FileSystemException (__ ('Unable to create directory %1. ' , $ destinationFolder ));
678
+ if (!$ this ->getFileDriver ()->isDirectory ($ destinationFolder )) {
679
+ $ result = $ this ->getFileDriver ()->createDirectory ($ destinationFolder );
680
+ if (!$ result ) {
681
+ throw new FileSystemException (__ ('Unable to create directory %1. ' , $ destinationFolder ));
682
+ }
671
683
}
684
+
672
685
return $ this ;
673
686
}
674
687
@@ -730,4 +743,20 @@ public static function getDispersionPath($fileName)
730
743
}
731
744
return $ dispersionPath ;
732
745
}
746
+
747
+ /**
748
+ * Get driver for file
749
+ *
750
+ * @deprecated
751
+ * @return DriverInterface
752
+ */
753
+ private function getFileDriver (): DriverInterface
754
+ {
755
+ if (!$ this ->fileDriver ) {
756
+ $ this ->driverPool = $ this ->driverPool ?: ObjectManager::getInstance ()->get (DriverPool::class);
757
+ $ this ->fileDriver = $ this ->driverPool ->getDriver (DriverPool::FILE );
758
+ }
759
+
760
+ return $ this ->fileDriver ;
761
+ }
733
762
}
0 commit comments