Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ContentUploader.php
Go to the documentation of this file.
1 <?php
7 
17 
19 {
23  const DEFAULT_MIME_TYPE = 'application/octet-stream';
24 
30  protected $filePrefix = 'magento_api';
31 
35  protected $mediaDirectory;
36 
41 
45  protected $linkConfig;
46 
50  protected $sampleConfig;
51 
60  public function __construct(
61  Database $coreFileStorageDb,
62  Storage $coreFileStorage,
63  NotProtectedExtension $validator,
67  ) {
68  $this->_validator = $validator;
69  $this->_coreFileStorage = $coreFileStorage;
70  $this->_coreFileStorageDb = $coreFileStorageDb;
71  $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
72  $this->systemTmpDirectory = $filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
73  $this->linkConfig = $linkConfig;
74  $this->sampleConfig = $sampleConfig;
75  }
76 
83  protected function decodeContent(ContentInterface $fileContent)
84  {
85  $tmpFileName = $this->getTmpFileName();
86  $fileSize = $this->systemTmpDirectory->writeFile($tmpFileName, base64_decode($fileContent->getFileData()));
87 
88  return [
89  'name' => $fileContent->getName(),
90  'type' => self::DEFAULT_MIME_TYPE,
91  'tmp_name' => $this->systemTmpDirectory->getAbsolutePath($tmpFileName),
92  'error' => 0,
93  'size' => $fileSize,
94  ];
95  }
96 
102  protected function getTmpFileName()
103  {
104  return uniqid($this->filePrefix, true);
105  }
106 
110  public function upload(ContentInterface $fileContent, $contentType)
111  {
112  $this->_file = $this->decodeContent($fileContent);
113  if (!file_exists($this->_file['tmp_name'])) {
114  throw new \InvalidArgumentException('There was an error during file content upload.');
115  }
116  $this->_fileExists = true;
117  $this->_uploadType = self::SINGLE_STYLE;
118  $this->setAllowRenameFiles(true);
119  $this->setFilesDispersion(true);
120  $result = $this->save($this->getDestinationDirectory($contentType));
121  unset($result['path']);
122  $result['status'] = 'new';
123  $result['name'] = substr($result['file'], strrpos($result['file'], '/') + 1);
124  return $result;
125  }
126 
134  protected function getDestinationDirectory($contentType)
135  {
136  switch ($contentType) {
137  case 'link_file':
138  $directory = $this->mediaDirectory->getAbsolutePath($this->linkConfig->getBaseTmpPath());
139  break;
140  case 'link_sample_file':
141  $directory = $this->mediaDirectory->getAbsolutePath($this->linkConfig->getBaseSampleTmpPath());
142  break;
143  case 'sample':
144  $directory = $this->mediaDirectory->getAbsolutePath($this->sampleConfig->getBaseTmpPath());
145  break;
146  default:
147  throw new \InvalidArgumentException('Invalid downloadable file content type.');
148  }
149  return $directory;
150  }
151 }
decodeContent(ContentInterface $fileContent)
upload(ContentInterface $fileContent, $contentType)
$filesystem
__construct(Database $coreFileStorageDb, Storage $coreFileStorage, NotProtectedExtension $validator, Filesystem $filesystem, LinkConfig $linkConfig, SampleConfig $sampleConfig)
save($destinationFolder, $newFileName=null)
Definition: Uploader.php:203