Download.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Exception\LocalizedException;
  9. /**
  10. * Class Download. Represents download logic for files
  11. */
  12. class Download
  13. {
  14. /**
  15. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  16. */
  17. protected $_rootDir;
  18. /**
  19. * @var \Magento\MediaStorage\Helper\File\Storage\Database
  20. */
  21. protected $_fileStorageDatabase;
  22. /**
  23. * @var \Magento\MediaStorage\Model\File\Storage\DatabaseFactory
  24. */
  25. protected $_storageDatabaseFactory;
  26. /**
  27. * @var \Magento\Framework\App\Response\Http\FileFactory
  28. */
  29. protected $_fileFactory;
  30. /**
  31. * @var string
  32. */
  33. protected $rootDirBasePath;
  34. /**
  35. * Constructor method
  36. *
  37. * @param \Magento\Framework\Filesystem $filesystem
  38. * @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDatabase
  39. * @param \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $storageDatabaseFactory
  40. * @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
  41. * @param string $rootDirBasePath
  42. */
  43. public function __construct(
  44. \Magento\Framework\Filesystem $filesystem,
  45. \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDatabase,
  46. \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $storageDatabaseFactory,
  47. \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
  48. $rootDirBasePath = DirectoryList::MEDIA
  49. ) {
  50. $this->rootDirBasePath = $rootDirBasePath;
  51. $this->_rootDir = $filesystem->getDirectoryWrite($this->rootDirBasePath);
  52. $this->_fileStorageDatabase = $fileStorageDatabase;
  53. $this->_storageDatabaseFactory = $storageDatabaseFactory;
  54. $this->_fileFactory = $fileFactory;
  55. }
  56. /**
  57. * Custom options downloader
  58. *
  59. * @param array $info
  60. * @return void
  61. * @throws \Exception
  62. */
  63. public function downloadFile($info)
  64. {
  65. $relativePath = $info['order_path'];
  66. if (!$this->_isCanProcessed($relativePath)) {
  67. //try get file from quote
  68. $relativePath = $info['quote_path'];
  69. if (!$this->_isCanProcessed($relativePath)) {
  70. throw new LocalizedException(
  71. __('Path "%1" is not part of allowed directory "%2"', $relativePath, $this->rootDirBasePath)
  72. );
  73. }
  74. }
  75. $this->_fileFactory->create(
  76. $info['title'],
  77. ['value' => $this->_rootDir->getRelativePath($relativePath), 'type' => 'filename'],
  78. $this->rootDirBasePath,
  79. $info['type']
  80. );
  81. }
  82. /**
  83. * Method checks, if file can be returned depends on the given filepath
  84. *
  85. * @param string $relativePath
  86. * @return bool
  87. */
  88. protected function _isCanProcessed($relativePath)
  89. {
  90. $filePath = $this->_rootDir->getAbsolutePath($relativePath);
  91. $pathWithFixedSeparator = str_replace('\\', '/', $this->_rootDir->getDriver()->getRealPath($filePath));
  92. return (strpos($pathWithFixedSeparator, $relativePath) !== false
  93. && $this->_rootDir->isFile($relativePath) && $this->_rootDir->isReadable($relativePath))
  94. || $this->_processDatabaseFile($filePath, $relativePath);
  95. }
  96. /**
  97. * Check file in database storage if needed and place it on file system
  98. *
  99. * @param string $filePath
  100. * @param string $relativePath
  101. * @return bool
  102. */
  103. protected function _processDatabaseFile($filePath, $relativePath)
  104. {
  105. if (!$this->_fileStorageDatabase->checkDbUsage()) {
  106. return false;
  107. }
  108. $file = $this->_storageDatabaseFactory->create()->loadByFilename($relativePath);
  109. if (!$file->getId()) {
  110. return false;
  111. }
  112. $stream = $this->_rootDir->openFile($relativePath, 'w+');
  113. $stream->lock();
  114. $stream->write($filePath, $file->getContent());
  115. $stream->unlock();
  116. $stream->close();
  117. return true;
  118. }
  119. }