66namespace Magento \Framework \File ;
77
88use Magento \Framework \App \Filesystem \DirectoryList ;
9+ use Magento \Framework \App \ObjectManager ;
910use Magento \Framework \Exception \FileSystemException ;
11+ use Magento \Framework \Filesystem \DriverInterface ;
12+ use Magento \Framework \Filesystem \DriverPool ;
1013use Magento \Framework \Validation \ValidationException ;
1114
1215/**
@@ -143,15 +146,13 @@ class Uploader
143146
144147 /**
145148 * 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()
148150 */
149151 const MAX_IMAGE_WIDTH = 1920 ;
150152
151153 /**
152154 * 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()
155156 */
156157 const MAX_IMAGE_HEIGHT = 1200 ;
157158
@@ -168,21 +169,32 @@ class Uploader
168169 */
169170 private $ directoryList ;
170171
172+ /**
173+ * @var DriverPool|null
174+ */
175+ private $ driverPool ;
176+
177+ /**
178+ * @var DriverInterface|null
179+ */
180+ private $ fileDriver ;
181+
171182 /**
172183 * Init upload
173184 *
174185 * @param string|array $fileId
175186 * @param \Magento\Framework\File\Mime|null $fileMime
176187 * @param DirectoryList|null $directoryList
188+ * @param DriverPool|null $driverPool
177189 * @throws \DomainException
178190 */
179191 public function __construct (
180192 $ fileId ,
181193 Mime $ fileMime = null ,
182- DirectoryList $ directoryList = null
194+ DirectoryList $ directoryList = null ,
195+ DriverPool $ driverPool = null
183196 ) {
184- $ this ->directoryList = $ directoryList ?: \Magento \Framework \App \ObjectManager::getInstance ()
185- ->get (DirectoryList::class);
197+ $ this ->directoryList = $ directoryList ?: ObjectManager::getInstance ()->get (DirectoryList::class);
186198
187199 $ this ->_setUploadFileId ($ fileId );
188200 if (!file_exists ($ this ->_file ['tmp_name ' ])) {
@@ -191,7 +203,8 @@ public function __construct(
191203 } else {
192204 $ this ->_fileExists = true ;
193205 }
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 ;
195208 }
196209
197210 /**
@@ -229,7 +242,7 @@ public function save($destinationFolder, $newFileName = null)
229242 $ this ->setAllowCreateFolders (true );
230243 $ this ->_dispretionPath = static ::getDispersionPath ($ fileName );
231244 $ destinationFile .= $ this ->_dispretionPath ;
232- $ this ->_createDestinationFolder ($ destinationFile );
245+ $ this ->createDestinationFolder ($ destinationFile );
233246 }
234247
235248 if ($ this ->_allowRenameFiles ) {
@@ -274,13 +287,11 @@ public function save($destinationFolder, $newFileName = null)
274287 * @return void
275288 * @throws FileSystemException
276289 */
277- private function validateDestination ($ destinationFolder )
290+ private function validateDestination (string $ destinationFolder ): void
278291 {
279292 if ($ this ->_allowCreateFolders ) {
280- $ this ->_createDestinationFolder ($ destinationFolder );
281- }
282-
283- if (!is_writable ($ destinationFolder )) {
293+ $ this ->createDestinationFolder ($ destinationFolder );
294+ } elseif (!$ this ->getFileDriver ()->isWritable ($ destinationFolder )) {
284295 throw new FileSystemException (__ ('Destination folder is not writable or does not exists. ' ));
285296 }
286297 }
@@ -654,7 +665,7 @@ private function validateFileId(array $fileId): void
654665 * @return \Magento\Framework\File\Uploader
655666 * @throws FileSystemException
656667 */
657- private function _createDestinationFolder ( $ destinationFolder )
668+ private function createDestinationFolder ( string $ destinationFolder )
658669 {
659670 if (!$ destinationFolder ) {
660671 return $ this ;
@@ -664,11 +675,13 @@ private function _createDestinationFolder($destinationFolder)
664675 $ destinationFolder = substr ($ destinationFolder , 0 , -1 );
665676 }
666677
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+ }
671683 }
684+
672685 return $ this ;
673686 }
674687
@@ -730,4 +743,20 @@ public static function getDispersionPath($fileName)
730743 }
731744 return $ dispersionPath ;
732745 }
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+ }
733762}
0 commit comments