1111use Magento \Framework \App \Filesystem \DirectoryList ;
1212use Magento \Framework \Exception \AlreadyExistsException ;
1313use Magento \Framework \Exception \FileSystemException ;
14+ use Magento \Framework \Exception \InputException ;
1415use Magento \Framework \Filesystem ;
1516use Magento \Framework \Filesystem \Driver \Https ;
1617use Magento \Framework \Filesystem \DriverInterface ;
@@ -30,15 +31,23 @@ class Save
3031 */
3132 private $ driver ;
3233
34+ /**
35+ * @var int|null
36+ */
37+ private $ maxFileLength ;
38+
3339 /**
3440 * Storage constructor.
3541 * @param Filesystem $filesystem
3642 * @param Https $driver
43+ * @param int|null $maxFileLength
3744 */
3845 public function __construct (
3946 Filesystem $ filesystem ,
40- Https $ driver
47+ Https $ driver ,
48+ int $ maxFileLength = null
4149 ) {
50+ $ this ->maxFileLength = $ maxFileLength ?: 255 ;
4251 $ this ->filesystem = $ filesystem ;
4352 $ this ->driver = $ driver ;
4453 }
@@ -51,10 +60,16 @@ public function __construct(
5160 * @return string
5261 * @throws AlreadyExistsException
5362 * @throws FileSystemException
63+ * @throws InputException
5464 */
5565 public function execute (string $ imageUrl , string $ destinationPath = '' ) : string
5666 {
5767 $ mediaDirectory = $ this ->filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
68+ if (strlen ($ destinationPath ) > $ this ->maxFileLength ) {
69+ throw new InputException (
70+ __ ('Destination path is too long; must be %1 characters or less ' , $ this ->maxFileLength )
71+ );
72+ }
5873
5974 if ($ mediaDirectory ->isExist ($ destinationPath )) {
6075 throw new AlreadyExistsException (__ ('Image with the same file name already exits. ' ));
0 commit comments