Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Download Class Reference

Public Member Functions

 __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)
 
 downloadFile ($info)
 

Protected Member Functions

 _isCanProcessed ($relativePath)
 
 _processDatabaseFile ($filePath, $relativePath)
 

Protected Attributes

 $_rootDir
 
 $_fileStorageDatabase
 
 $_storageDatabaseFactory
 
 $_fileFactory
 
 $rootDirBasePath
 

Detailed Description

Class Download. Represents download logic for files

Definition at line 14 of file Download.php.

Constructor & Destructor Documentation

◆ __construct()

__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 
)

Constructor method

Parameters
\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
string$rootDirBasePath

Definition at line 50 of file Download.php.

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  }
$filesystem

Member Function Documentation

◆ _isCanProcessed()

_isCanProcessed (   $relativePath)
protected

Method checks, if file can be returned depends on the given filepath

Parameters
string$relativePath
Returns
bool

Definition at line 97 of file Download.php.

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  }
$relativePath
Definition: get.php:35
_processDatabaseFile($filePath, $relativePath)
Definition: Download.php:113

◆ _processDatabaseFile()

_processDatabaseFile (   $filePath,
  $relativePath 
)
protected

Check file in database storage if needed and place it on file system

Parameters
string$filePath
string$relativePath
Returns
bool

Definition at line 113 of file Download.php.

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  }
$relativePath
Definition: get.php:35

◆ downloadFile()

downloadFile (   $info)

Custom options downloader

Parameters
array$info
Returns
void
Exceptions

Definition at line 71 of file Download.php.

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  }
__()
Definition: __.php:13
_isCanProcessed($relativePath)
Definition: Download.php:97
$relativePath
Definition: get.php:35
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52

Field Documentation

◆ $_fileFactory

$_fileFactory
protected

Definition at line 34 of file Download.php.

◆ $_fileStorageDatabase

$_fileStorageDatabase
protected

Definition at line 24 of file Download.php.

◆ $_rootDir

$_rootDir
protected

Definition at line 19 of file Download.php.

◆ $_storageDatabaseFactory

$_storageDatabaseFactory
protected

Definition at line 29 of file Download.php.

◆ $rootDirBasePath

$rootDirBasePath
protected

Definition at line 39 of file Download.php.


The documentation for this class was generated from the following file: