_validator = $validator; $this->_coreFileStorage = $coreFileStorage; $this->_coreFileStorageDb = $coreFileStorageDb; $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); $this->systemTmpDirectory = $filesystem->getDirectoryWrite(DirectoryList::SYS_TMP); $this->linkConfig = $linkConfig; $this->sampleConfig = $sampleConfig; } /** * Decode base64 encoded content and save it in system tmp folder * * @param ContentInterface $fileContent * @return array */ protected function decodeContent(ContentInterface $fileContent) { $tmpFileName = $this->getTmpFileName(); $fileSize = $this->systemTmpDirectory->writeFile($tmpFileName, base64_decode($fileContent->getFileData())); return [ 'name' => $fileContent->getName(), 'type' => self::DEFAULT_MIME_TYPE, 'tmp_name' => $this->systemTmpDirectory->getAbsolutePath($tmpFileName), 'error' => 0, 'size' => $fileSize, ]; } /** * Generate temporary file name * * @return string */ protected function getTmpFileName() { return uniqid($this->filePrefix, true); } /** * {@inheritdoc} */ public function upload(ContentInterface $fileContent, $contentType) { $this->_file = $this->decodeContent($fileContent); if (!file_exists($this->_file['tmp_name'])) { throw new \InvalidArgumentException('There was an error during file content upload.'); } $this->_fileExists = true; $this->_uploadType = self::SINGLE_STYLE; $this->setAllowRenameFiles(true); $this->setFilesDispersion(true); $result = $this->save($this->getDestinationDirectory($contentType)); unset($result['path']); $result['status'] = 'new'; $result['name'] = substr($result['file'], strrpos($result['file'], '/') + 1); return $result; } /** * Retrieve destination directory for given content type * * @param string $contentType * @return string * @throws \InvalidArgumentException */ protected function getDestinationDirectory($contentType) { switch ($contentType) { case 'link_file': $directory = $this->mediaDirectory->getAbsolutePath($this->linkConfig->getBaseTmpPath()); break; case 'link_sample_file': $directory = $this->mediaDirectory->getAbsolutePath($this->linkConfig->getBaseSampleTmpPath()); break; case 'sample': $directory = $this->mediaDirectory->getAbsolutePath($this->sampleConfig->getBaseTmpPath()); break; default: throw new \InvalidArgumentException('Invalid downloadable file content type.'); } return $directory; } }