88
99use Magento \Framework \Filesystem \DriverInterface ;
1010use Magento \Framework \Exception \FileSystemException ;
11+ use Magento \Framework \Phrase ;
1112
1213class Write extends Read implements WriteInterface
1314{
@@ -28,18 +29,22 @@ public function __construct($path, DriverInterface $driver, $mode)
2829 * Assert file existence for proper mode
2930 *
3031 * @return void
31- * @throws \Magento\Framework\Exception\ FileSystemException
32+ * @throws FileSystemException
3233 */
3334 protected function assertValid ()
3435 {
3536 $ fileExists = $ this ->driver ->isExists ($ this ->path );
3637 $ mode = $ this ->mode ?? '' ;
37- if (!$ fileExists && preg_match ('/r/ ' , $ mode )) {
38+ if (preg_match ('/(?:^-|\s-)/ ' , basename ($ this ->path ))) {
39+ throw new FileSystemException (
40+ new Phrase ('The filename "%1" contains invalid characters ' , [basename ($ this ->path )])
41+ );
42+ } elseif (!$ fileExists && preg_match ('/r/ ' , $ mode )) {
3843 throw new FileSystemException (
39- new \ Magento \ Framework \ Phrase ('The "%1" file doesn \'t exist. ' , [$ this ->path ])
44+ new Phrase ('The "%1" file doesn \'t exist. ' , [$ this ->path ])
4045 );
4146 } elseif ($ fileExists && preg_match ('/x/ ' , $ mode )) {
42- throw new FileSystemException (new \ Magento \ Framework \ Phrase ('The file "%1" already exists ' , [$ this ->path ]));
47+ throw new FileSystemException (new Phrase ('The file "%1" already exists ' , [$ this ->path ]));
4348 }
4449 }
4550
@@ -56,7 +61,7 @@ public function write($data)
5661 return $ this ->driver ->fileWrite ($ this ->resource , $ data );
5762 } catch (FileSystemException $ e ) {
5863 throw new FileSystemException (
59- new \ Magento \ Framework \ Phrase ('Cannot write to the "%1" file. %2 ' , [$ this ->path , $ e ->getMessage ()])
64+ new Phrase ('Cannot write to the "%1" file. %2 ' , [$ this ->path , $ e ->getMessage ()])
6065 );
6166 }
6267 }
@@ -76,7 +81,7 @@ public function writeCsv(array $data, $delimiter = ',', $enclosure = '"')
7681 return $ this ->driver ->filePutCsv ($ this ->resource , $ data , $ delimiter , $ enclosure );
7782 } catch (FileSystemException $ e ) {
7883 throw new FileSystemException (
79- new \ Magento \ Framework \ Phrase ('Cannot write to the "%1" file. %2 ' , [$ this ->path , $ e ->getMessage ()])
84+ new Phrase ('Cannot write to the "%1" file. %2 ' , [$ this ->path , $ e ->getMessage ()])
8085 );
8186 }
8287 }
@@ -93,7 +98,7 @@ public function flush()
9398 return $ this ->driver ->fileFlush ($ this ->resource );
9499 } catch (FileSystemException $ e ) {
95100 throw new FileSystemException (
96- new \ Magento \ Framework \ Phrase ('Cannot flush the "%1" file. %2 ' , [$ this ->path , $ e ->getMessage ()])
101+ new Phrase ('Cannot flush the "%1" file. %2 ' , [$ this ->path , $ e ->getMessage ()])
97102 );
98103 }
99104 }
0 commit comments