/vendor/magento/module-downloadable/Model/File/ContentUploader.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 150 lines · 84 code · 13 blank · 53 comment · 2 complexity · 614c5e0a6c5d46fb053bb5b059df2e0c MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model\File;
  7. use Magento\MediaStorage\Helper\File\Storage;
  8. use Magento\MediaStorage\Helper\File\Storage\Database;
  9. use Magento\MediaStorage\Model\File\Uploader;
  10. use Magento\MediaStorage\Model\File\Validator\NotProtectedExtension;
  11. use Magento\Downloadable\Api\Data\File\ContentInterface;
  12. use Magento\Downloadable\Model\Link as LinkConfig;
  13. use Magento\Downloadable\Model\Sample as SampleConfig;
  14. use Magento\Framework\App\Filesystem\DirectoryList;
  15. use Magento\Framework\Filesystem;
  16. class ContentUploader extends Uploader implements \Magento\Downloadable\Api\Data\File\ContentUploaderInterface
  17. {
  18. /**
  19. * Default MIME type
  20. */
  21. const DEFAULT_MIME_TYPE = 'application/octet-stream';
  22. /**
  23. * Filename prefix for temporary files
  24. *
  25. * @var string
  26. */
  27. protected $filePrefix = 'magento_api';
  28. /**
  29. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  30. */
  31. protected $mediaDirectory;
  32. /**
  33. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  34. */
  35. protected $systemTmpDirectory;
  36. /**
  37. * @var LinkConfig
  38. */
  39. protected $linkConfig;
  40. /**
  41. * @var SampleConfig
  42. */
  43. protected $sampleConfig;
  44. /**
  45. * @param Database $coreFileStorageDb
  46. * @param Storage $coreFileStorage
  47. * @param NotProtectedExtension $validator
  48. * @param Filesystem $filesystem
  49. * @param LinkConfig $linkConfig
  50. * @param SampleConfig $sampleConfig
  51. */
  52. public function __construct(
  53. Database $coreFileStorageDb,
  54. Storage $coreFileStorage,
  55. NotProtectedExtension $validator,
  56. Filesystem $filesystem,
  57. LinkConfig $linkConfig,
  58. SampleConfig $sampleConfig
  59. ) {
  60. $this->_validator = $validator;
  61. $this->_coreFileStorage = $coreFileStorage;
  62. $this->_coreFileStorageDb = $coreFileStorageDb;
  63. $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  64. $this->systemTmpDirectory = $filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
  65. $this->linkConfig = $linkConfig;
  66. $this->sampleConfig = $sampleConfig;
  67. }
  68. /**
  69. * Decode base64 encoded content and save it in system tmp folder
  70. *
  71. * @param ContentInterface $fileContent
  72. * @return array
  73. */
  74. protected function decodeContent(ContentInterface $fileContent)
  75. {
  76. $tmpFileName = $this->getTmpFileName();
  77. $fileSize = $this->systemTmpDirectory->writeFile($tmpFileName, base64_decode($fileContent->getFileData()));
  78. return [
  79. 'name' => $fileContent->getName(),
  80. 'type' => self::DEFAULT_MIME_TYPE,
  81. 'tmp_name' => $this->systemTmpDirectory->getAbsolutePath($tmpFileName),
  82. 'error' => 0,
  83. 'size' => $fileSize,
  84. ];
  85. }
  86. /**
  87. * Generate temporary file name
  88. *
  89. * @return string
  90. */
  91. protected function getTmpFileName()
  92. {
  93. return uniqid($this->filePrefix, true);
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function upload(ContentInterface $fileContent, $contentType)
  99. {
  100. $this->_file = $this->decodeContent($fileContent);
  101. if (!file_exists($this->_file['tmp_name'])) {
  102. throw new \InvalidArgumentException('There was an error during file content upload.');
  103. }
  104. $this->_fileExists = true;
  105. $this->_uploadType = self::SINGLE_STYLE;
  106. $this->setAllowRenameFiles(true);
  107. $this->setFilesDispersion(true);
  108. $result = $this->save($this->getDestinationDirectory($contentType));
  109. $result['status'] = 'new';
  110. $result['name'] = substr($result['file'], strrpos($result['file'], '/') + 1);
  111. return $result;
  112. }
  113. /**
  114. * Retrieve destination directory for given content type
  115. *
  116. * @param string $contentType
  117. * @return string
  118. * @throws \InvalidArgumentException
  119. */
  120. protected function getDestinationDirectory($contentType)
  121. {
  122. switch ($contentType) {
  123. case 'link_file':
  124. $directory = $this->mediaDirectory->getAbsolutePath($this->linkConfig->getBaseTmpPath());
  125. break;
  126. case 'link_sample_file':
  127. $directory = $this->mediaDirectory->getAbsolutePath($this->linkConfig->getBaseSampleTmpPath());
  128. break;
  129. case 'sample':
  130. $directory = $this->mediaDirectory->getAbsolutePath($this->sampleConfig->getBaseTmpPath());
  131. break;
  132. default:
  133. throw new \InvalidArgumentException('Invalid downloadable file content type.');
  134. }
  135. return $directory;
  136. }
  137. }