Download.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Controller;
  7. use Magento\Downloadable\Helper\Download as DownloadHelper;
  8. use Magento\Framework\App\Response\Http as HttpResponse;
  9. /**
  10. * Download controller
  11. *
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. */
  14. abstract class Download extends \Magento\Framework\App\Action\Action
  15. {
  16. /**
  17. * @var array
  18. */
  19. private $disallowedContentTypes = [
  20. 'text/html',
  21. ];
  22. /**
  23. * Prepare response to output resource contents
  24. *
  25. * @param string $path Path to resource
  26. * @param string $resourceType Type of resource (see Magento\Downloadable\Helper\Download::LINK_TYPE_* constants)
  27. * @return void
  28. */
  29. protected function _processDownload($path, $resourceType)
  30. {
  31. /* @var $helper DownloadHelper */
  32. $helper = $this->_objectManager->get(\Magento\Downloadable\Helper\Download::class);
  33. $helper->setResource($path, $resourceType);
  34. $fileName = $helper->getFilename();
  35. $contentType = $helper->getContentType();
  36. /** @var HttpResponse $response */
  37. $response = $this->getResponse();
  38. $response->setHttpResponseCode(
  39. 200
  40. )->setHeader(
  41. 'Pragma',
  42. 'public',
  43. true
  44. )->setHeader(
  45. 'Cache-Control',
  46. 'must-revalidate, post-check=0, pre-check=0',
  47. true
  48. )->setHeader(
  49. 'Content-type',
  50. $contentType,
  51. true
  52. );
  53. if ($fileSize = $helper->getFileSize()) {
  54. $response->setHeader('Content-Length', $fileSize);
  55. }
  56. $contentDisposition = $helper->getContentDisposition();
  57. if (!$contentDisposition || in_array($contentType, $this->disallowedContentTypes)) {
  58. // For security reasons we force browsers to download the file instead of opening it.
  59. $contentDisposition = \Magento\Framework\HTTP\Mime::DISPOSITION_ATTACHMENT;
  60. }
  61. $response->setHeader('Content-Disposition', $contentDisposition . '; filename=' . $fileName);
  62. //Rendering
  63. $response->clearBody();
  64. $response->sendHeaders();
  65. $helper->output();
  66. }
  67. /**
  68. * Get link model
  69. *
  70. * @return \Magento\Downloadable\Model\Link
  71. */
  72. protected function _getLink()
  73. {
  74. return $this->_objectManager->get(\Magento\Downloadable\Model\Link::class);
  75. }
  76. }