Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Download.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Sales\Model;
7 
10 
14 class Download
15 {
19  protected $_rootDir;
20 
25 
30 
34  protected $_fileFactory;
35 
39  protected $rootDirBasePath;
40 
50  public function __construct(
51  \Magento\Framework\Filesystem $filesystem,
52  \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDatabase,
53  \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $storageDatabaseFactory,
54  \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
56  ) {
57  $this->rootDirBasePath = $rootDirBasePath;
58  $this->_rootDir = $filesystem->getDirectoryWrite($this->rootDirBasePath);
59  $this->_fileStorageDatabase = $fileStorageDatabase;
60  $this->_storageDatabaseFactory = $storageDatabaseFactory;
61  $this->_fileFactory = $fileFactory;
62  }
63 
71  public function downloadFile($info)
72  {
73  $relativePath = $info['order_path'];
74  if (!$this->_isCanProcessed($relativePath)) {
75  //try get file from quote
76  $relativePath = $info['quote_path'];
77  if (!$this->_isCanProcessed($relativePath)) {
78  throw new LocalizedException(
79  __('Path "%1" is not part of allowed directory "%2"', $relativePath, $this->rootDirBasePath)
80  );
81  }
82  }
83  $this->_fileFactory->create(
84  $info['title'],
85  ['value' => $this->_rootDir->getRelativePath($relativePath), 'type' => 'filename'],
87  $info['type']
88  );
89  }
90 
97  protected function _isCanProcessed($relativePath)
98  {
99  $filePath = $this->_rootDir->getAbsolutePath($relativePath);
100  $pathWithFixedSeparator = str_replace('\\', '/', $this->_rootDir->getDriver()->getRealPath($filePath));
101  return (strpos($pathWithFixedSeparator, $relativePath) !== false
102  && $this->_rootDir->isFile($relativePath) && $this->_rootDir->isReadable($relativePath))
103  || $this->_processDatabaseFile($filePath, $relativePath);
104  }
105 
113  protected function _processDatabaseFile($filePath, $relativePath)
114  {
115  if (!$this->_fileStorageDatabase->checkDbUsage()) {
116  return false;
117  }
118  $file = $this->_storageDatabaseFactory->create()->loadByFilename($relativePath);
119  if (!$file->getId()) {
120  return false;
121  }
122  $stream = $this->_rootDir->openFile($relativePath, 'w+');
123  $stream->lock();
124  $stream->write($filePath, $file->getContent());
125  $stream->unlock();
126  $stream->close();
127  return true;
128  }
129 }
__()
Definition: __.php:13
_isCanProcessed($relativePath)
Definition: Download.php:97
$relativePath
Definition: get.php:35
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
__construct(\Magento\Framework\Filesystem $filesystem, \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDatabase, \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $storageDatabaseFactory, \Magento\Framework\App\Response\Http\FileFactory $fileFactory, $rootDirBasePath=DirectoryList::MEDIA)
Definition: Download.php:50
_processDatabaseFile($filePath, $relativePath)
Definition: Download.php:113
$filesystem