/** * Prepare Data Source * * @param array $dataSource * @return void */ public function prepareDataSource(array &$dataSource) { if (isset($dataSource['data']['items'])) { foreach ($dataSource['data']['items'] as &$item) { $item[$this->getData('name')]['edit'] = ['href' => $this->urlBuilder->getUrl('umc/module/edit', ['id' => $item['safe_id']]), 'label' => __('Edit'), 'hidden' => false]; $rootDir = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR); foreach ($this->downloader->getDownloaderTypes() as $type) { $downloader = $this->downloader->getDownloader($type); $file = $downloader->getRelativePath($item['filename_id']); $file = $rootDir->getRelativePath(Settings::VAR_DIR_NAME . '/' . $file); if ($rootDir->isFile($file) && $rootDir->isReadable($file)) { $url = $this->urlBuilder->getUrl('umc/module/download', ['id' => $item['safe_id'], 'type' => $type]); $label = $downloader->getLabel(); } else { $url = '#'; $label = $downloader->getErrorLabel(); } $item[$this->getData('name')][$type] = ['href' => $url, 'label' => $label, 'hidden' => false]; } } } }
/** * run the action * * @return \Magento\Backend\Model\View\Result\Redirect|\Magento\Framework\Controller\Result\Raw */ public function execute() { $type = $this->getRequest()->getParam('type'); $id = $this->getRequest()->getParam('id'); $packageName = $this->decoder->decode($id); $downloader = $this->downloader->getDownloader($type); $rootDir = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR); $file = $downloader->getRelativePath($packageName); $relativeFile = Settings::VAR_DIR_NAME . '/' . $file; $absoluteFile = $rootDir->getAbsolutePath($relativeFile); if ($rootDir->isFile($relativeFile) && $rootDir->isReadable($relativeFile)) { $fileName = basename($absoluteFile); $this->fileFactory->create($fileName, null, DirectoryList::VAR_DIR, 'application/octet-stream', $rootDir->stat($relativeFile)['size']); $resultRaw = $this->resultRawFactory->create(); $resultRaw->setContents($rootDir->readFile($relativeFile)); return $resultRaw; } else { $result = $this->resultRedirectFactory->create(); $result->setPath('umc/module/index'); $this->messageManager->addError(__('The requested file does not exist or is not readable')); return $result; } }
/** * get downloader * * @param $type * @return Downloader\DownloaderInterface */ protected function getDownloader($type) { return $this->downloader->getDownloader($type); }