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,20 +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 (preg_match ('/(?:^-|\s-)/ ' , $ this ->path )) {
38- throw new FileSystemException (new \Magento \Framework \Phrase ('The file "%1" can \' be allowed ' , [$ this ->path ]));
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+ );
3942 } elseif (!$ fileExists && preg_match ('/r/ ' , $ mode )) {
4043 throw new FileSystemException (
41- new \ Magento \ Framework \ Phrase ('The "%1" file doesn \'t exist. ' , [$ this ->path ])
44+ new Phrase ('The "%1" file doesn \'t exist. ' , [$ this ->path ])
4245 );
4346 } elseif ($ fileExists && preg_match ('/x/ ' , $ mode )) {
44- 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 ]));
4548 }
4649 }
4750
@@ -58,7 +61,7 @@ public function write($data)
5861 return $ this ->driver ->fileWrite ($ this ->resource , $ data );
5962 } catch (FileSystemException $ e ) {
6063 throw new FileSystemException (
61- 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 ()])
6265 );
6366 }
6467 }
@@ -78,7 +81,7 @@ public function writeCsv(array $data, $delimiter = ',', $enclosure = '"')
7881 return $ this ->driver ->filePutCsv ($ this ->resource , $ data , $ delimiter , $ enclosure );
7982 } catch (FileSystemException $ e ) {
8083 throw new FileSystemException (
81- 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 ()])
8285 );
8386 }
8487 }
@@ -95,7 +98,7 @@ public function flush()
9598 return $ this ->driver ->fileFlush ($ this ->resource );
9699 } catch (FileSystemException $ e ) {
97100 throw new FileSystemException (
98- 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 ()])
99102 );
100103 }
101104 }
0 commit comments