Upload.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Controller\Adminhtml\Downloadable\File;
  7. use Magento\Framework\App\Action\HttpPostActionInterface;
  8. use Magento\Framework\Controller\ResultFactory;
  9. /**
  10. * Class Upload
  11. *
  12. * @package Magento\Downloadable\Controller\Adminhtml\Downloadable\File
  13. */
  14. class Upload extends \Magento\Downloadable\Controller\Adminhtml\Downloadable\File implements HttpPostActionInterface
  15. {
  16. /**
  17. * @var \Magento\Downloadable\Model\Link
  18. */
  19. protected $_link;
  20. /**
  21. * @var \Magento\Downloadable\Model\Sample
  22. */
  23. protected $_sample;
  24. /**
  25. * Downloadable file helper.
  26. *
  27. * @var \Magento\Downloadable\Helper\File
  28. */
  29. protected $_fileHelper;
  30. /**
  31. * @var \Magento\MediaStorage\Model\File\UploaderFactory
  32. */
  33. private $uploaderFactory;
  34. /**
  35. * @var \Magento\MediaStorage\Helper\File\Storage\Database
  36. */
  37. private $storageDatabase;
  38. /**
  39. *
  40. * Copyright © Magento, Inc. All rights reserved.
  41. * See COPYING.txt for license details.
  42. * @param \Magento\Backend\App\Action\Context $context
  43. * @param \Magento\Downloadable\Model\Link $link
  44. * @param \Magento\Downloadable\Model\Sample $sample
  45. * @param \Magento\Downloadable\Helper\File $fileHelper
  46. * @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
  47. * @param \Magento\MediaStorage\Helper\File\Storage\Database $storageDatabase
  48. */
  49. public function __construct(
  50. \Magento\Backend\App\Action\Context $context,
  51. \Magento\Downloadable\Model\Link $link,
  52. \Magento\Downloadable\Model\Sample $sample,
  53. \Magento\Downloadable\Helper\File $fileHelper,
  54. \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
  55. \Magento\MediaStorage\Helper\File\Storage\Database $storageDatabase
  56. ) {
  57. parent::__construct($context);
  58. $this->_link = $link;
  59. $this->_sample = $sample;
  60. $this->_fileHelper = $fileHelper;
  61. $this->uploaderFactory = $uploaderFactory;
  62. $this->storageDatabase = $storageDatabase;
  63. }
  64. /**
  65. * Upload file controller action
  66. *
  67. * @return \Magento\Framework\Controller\ResultInterface
  68. */
  69. public function execute()
  70. {
  71. $type = $this->getRequest()->getParam('type');
  72. $tmpPath = '';
  73. if ($type == 'samples') {
  74. $tmpPath = $this->_sample->getBaseTmpPath();
  75. } elseif ($type == 'links') {
  76. $tmpPath = $this->_link->getBaseTmpPath();
  77. } elseif ($type == 'link_samples') {
  78. $tmpPath = $this->_link->getBaseSampleTmpPath();
  79. }
  80. try {
  81. $uploader = $this->uploaderFactory->create(['fileId' => $type]);
  82. $result = $this->_fileHelper->uploadFromTmp($tmpPath, $uploader);
  83. if (!$result) {
  84. throw new \Exception('File can not be moved from temporary folder to the destination folder.');
  85. }
  86. unset($result['tmp_name'], $result['path']);
  87. if (isset($result['file'])) {
  88. $relativePath = rtrim($tmpPath, '/') . '/' . ltrim($result['file'], '/');
  89. $this->storageDatabase->saveFile($relativePath);
  90. }
  91. } catch (\Exception $e) {
  92. $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
  93. }
  94. return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
  95. }
  96. }