/** * Upload file controller action * * @return \Magento\Framework\Controller\ResultInterface */ public function executeInternal() { $type = $this->getRequest()->getParam('type'); $tmpPath = ''; if ($type == 'samples') { $tmpPath = $this->_sample->getBaseTmpPath(); } elseif ($type == 'links') { $tmpPath = $this->_link->getBaseTmpPath(); } elseif ($type == 'link_samples') { $tmpPath = $this->_link->getBaseSampleTmpPath(); } try { $uploader = $this->uploaderFactory->create(['fileId' => $type]); $result = $this->_fileHelper->uploadFromTmp($tmpPath, $uploader); if (!$result) { throw new \Exception('File can not be moved from temporary folder to the destination folder.'); } /** * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS */ $result['tmp_name'] = str_replace('\\', '/', $result['tmp_name']); $result['path'] = str_replace('\\', '/', $result['path']); if (isset($result['file'])) { $relativePath = rtrim($tmpPath, '/') . '/' . ltrim($result['file'], '/'); $this->storageDatabase->saveFile($relativePath); } $result['cookie'] = ['name' => $this->_getSession()->getName(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain()]; } catch (\Exception $e) { $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()]; } return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($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; }