Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Download Class Reference
Inheritance diagram for Download:
AbstractHelper

Public Member Functions

 __construct (\Magento\Framework\App\Helper\Context $context, \Magento\Downloadable\Helper\File $downloadableFile, \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Session\SessionManagerInterface $session, \Magento\Framework\Filesystem\File\ReadFactory $fileReadFactory)
 
 getFileSize ()
 
 getContentType ()
 
 getFilename ()
 
 setResource ($resourceFile, $linkType=self::LINK_TYPE_FILE)
 
 output ()
 
 getContentDisposition ($store=null)
 
- Public Member Functions inherited from AbstractHelper
 __construct (Context $context)
 
 isModuleOutputEnabled ($moduleName=null)
 

Data Fields

const LINK_TYPE_URL = 'url'
 
const LINK_TYPE_FILE = 'file'
 
const XML_PATH_CONTENT_DISPOSITION = 'catalog/downloadable/content_disposition'
 

Protected Member Functions

 _getHandle ()
 
- Protected Member Functions inherited from AbstractHelper
 _getRequest ()
 
 _getModuleName ()
 
 _getUrl ($route, $params=[])
 

Protected Attributes

 $_linkType = self::LINK_TYPE_FILE
 
 $_resourceFile = null
 
 $_handle = null
 
 $_urlHeaders = []
 
 $_contentType = 'application/octet-stream'
 
 $_fileName = 'download'
 
 $_coreFileStorageDb
 
 $_downloadableFile
 
 $_filesystem
 
 $fileReadFactory
 
 $_workingDirectory
 
 $_session
 
- Protected Attributes inherited from AbstractHelper
 $_moduleName
 
 $_request
 
 $_moduleManager
 
 $_logger
 
 $_urlBuilder
 
 $_httpHeader
 
 $_eventManager
 
 $_remoteAddress
 
 $urlEncoder
 
 $urlDecoder
 
 $scopeConfig
 
 $_cacheConfig
 

Detailed Description

Downloadable Products Download Helper @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 17 of file Download.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\Magento\Framework\App\Helper\Context$context
File$downloadableFile
\Magento\MediaStorage\Helper\File\Storage\Database$coreFileStorageDb
Filesystem$filesystem
\Magento\Framework\Session\SessionManagerInterface$session
Filesystem\File\ReadFactory$fileReadFactory

Definition at line 119 of file Download.php.

126  {
127  parent::__construct($context);
128  $this->_downloadableFile = $downloadableFile;
129  $this->_coreFileStorageDb = $coreFileStorageDb;
130  $this->_filesystem = $filesystem;
131  $this->_session = $session;
132  $this->fileReadFactory = $fileReadFactory;
133  }
$filesystem

Member Function Documentation

◆ _getHandle()

_getHandle ( )
protected

Retrieve Resource file handle (socket, file pointer etc)

Returns
\Magento\Framework\Filesystem\File\ReadInterface
Exceptions
CoreException|

Definition at line 141 of file Download.php.

142  {
143  if (!$this->_resourceFile) {
144  throw new CoreException(__('Please set resource file and link type.'));
145  }
146 
147  if ($this->_handle === null) {
148  if ($this->_linkType == self::LINK_TYPE_URL) {
150  $protocol = strtolower(parse_url($path, PHP_URL_SCHEME));
151  if ($protocol) {
152  // Strip down protocol from path
153  $path = preg_replace('#.+://#', '', $path);
154  }
155  $this->_handle = $this->fileReadFactory->create($path, $protocol);
156  } elseif ($this->_linkType == self::LINK_TYPE_FILE) {
157  $this->_workingDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
158  $fileExists = $this->_downloadableFile->ensureFileInFilesystem($this->_resourceFile);
159  if ($fileExists) {
160  $this->_handle = $this->_workingDirectory->openFile($this->_resourceFile);
161  } else {
162  throw new CoreException(__('Invalid download link type.'));
163  }
164  } else {
165  throw new CoreException(__('Invalid download link type.'));
166  }
167  }
168  return $this->_handle;
169  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13

◆ getContentDisposition()

getContentDisposition (   $store = null)

Use Content-Disposition: attachment

Parameters
mixed$store
Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 283 of file Download.php.

284  {
285  return $this->scopeConfig->getValue(
286  self::XML_PATH_CONTENT_DISPOSITION,
287  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
288  $store
289  );
290  }

◆ getContentType()

getContentType ( )

Return MIME type of a file.

Returns
string

Definition at line 186 of file Download.php.

187  {
188  $this->_getHandle();
189  if ($this->_linkType == self::LINK_TYPE_FILE) {
190  if (function_exists(
191  'mime_content_type'
192  ) && ($contentType = mime_content_type(
193  $this->_workingDirectory->getAbsolutePath($this->_resourceFile)
194  ))
195  ) {
196  return $contentType;
197  } else {
198  return $this->_downloadableFile->getFileType($this->_resourceFile);
199  }
200  } elseif ($this->_linkType == self::LINK_TYPE_URL) {
201  return $this->_handle->stat($this->_resourceFile)['type'];
202  }
203  return $this->_contentType;
204  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ getFilename()

getFilename ( )

Return name of the file

Returns
string

Definition at line 211 of file Download.php.

212  {
213  $this->_getHandle();
214  if ($this->_linkType == self::LINK_TYPE_FILE) {
215  return pathinfo($this->_resourceFile, PATHINFO_BASENAME);
216  } elseif ($this->_linkType == self::LINK_TYPE_URL) {
217  $stat = $this->_handle->stat($this->_resourceFile);
218  if (isset($stat['disposition'])) {
219  $contentDisposition = explode('; ', $stat['disposition']);
220  if (!empty($contentDisposition[1]) && preg_match(
221  '/filename=([^ ]+)/',
222  $contentDisposition[1],
223  $matches
224  )
225  ) {
226  return $matches[1];
227  }
228  }
229  $fileName = @pathinfo($this->_resourceFile, PATHINFO_BASENAME);
230  if ($fileName) {
231  return $fileName;
232  }
233  }
234  return $this->_fileName;
235  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$fileName
Definition: translate.phtml:15

◆ getFileSize()

getFileSize ( )

Retrieve file size in bytes

Returns
int

Definition at line 176 of file Download.php.

177  {
178  return $this->_getHandle()->stat($this->_resourceFile)['size'];
179  }

◆ output()

output ( )

Output file contents

Returns
void

Definition at line 267 of file Download.php.

268  {
269  $handle = $this->_getHandle();
270  $this->_session->writeClose();
271  while (true == ($buffer = $handle->read(1024))) {
272  echo $buffer; //@codingStandardsIgnoreLine
273  }
274  }
$handle

◆ setResource()

setResource (   $resourceFile,
  $linkType = self::LINK_TYPE_FILE 
)

Set resource file for download

Parameters
string$resourceFile
string$linkType
Returns
$this
Exceptions

Definition at line 245 of file Download.php.

246  {
247  if (self::LINK_TYPE_FILE == $linkType) {
248  //check LFI protection
249  if (preg_match('#\.\.[\\\/]#', $resourceFile)) {
250  throw new \InvalidArgumentException(
251  'Requested file may not include parent directory traversal ("../", "..\\" notation)'
252  );
253  }
254  }
255 
256  $this->_resourceFile = $resourceFile;
257  $this->_linkType = $linkType;
258 
259  return $this;
260  }

Field Documentation

◆ $_contentType

$_contentType = 'application/octet-stream'
protected

Definition at line 67 of file Download.php.

◆ $_coreFileStorageDb

$_coreFileStorageDb
protected

Definition at line 81 of file Download.php.

◆ $_downloadableFile

$_downloadableFile
protected

Definition at line 88 of file Download.php.

◆ $_fileName

$_fileName = 'download'
protected

Definition at line 74 of file Download.php.

◆ $_filesystem

$_filesystem
protected

Definition at line 93 of file Download.php.

◆ $_handle

$_handle = null
protected

Definition at line 53 of file Download.php.

◆ $_linkType

$_linkType = self::LINK_TYPE_FILE
protected

Definition at line 39 of file Download.php.

◆ $_resourceFile

$_resourceFile = null
protected

Definition at line 46 of file Download.php.

◆ $_session

$_session
protected

Definition at line 109 of file Download.php.

◆ $_urlHeaders

$_urlHeaders = []
protected

Definition at line 60 of file Download.php.

◆ $_workingDirectory

$_workingDirectory
protected

Definition at line 104 of file Download.php.

◆ $fileReadFactory

$fileReadFactory
protected

Definition at line 98 of file Download.php.

◆ LINK_TYPE_FILE

const LINK_TYPE_FILE = 'file'

Link type file

Definition at line 27 of file Download.php.

◆ LINK_TYPE_URL

const LINK_TYPE_URL = 'url'

Link type url

Definition at line 22 of file Download.php.

◆ XML_PATH_CONTENT_DISPOSITION

const XML_PATH_CONTENT_DISPOSITION = 'catalog/downloadable/content_disposition'

Config path to content disposition

Definition at line 32 of file Download.php.


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