/** * 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); }
/** * Delete data by item(s) * * @param \Magento\Downloadable\Model\Sample|array|int $items * @return $this */ public function deleteItems($items) { $connection = $this->getConnection(); if ($items instanceof \Magento\Downloadable\Model\Sample) { $where = ['sample_id = ?' => $items->getId()]; } else { $where = ['sample_id in (?)' => $items]; } $connection->delete($this->getMainTable(), $where); $connection->delete($this->getTable('downloadable_sample_title'), $where); return $this; }
/** * Delete data by item(s) * * @param \Magento\Downloadable\Model\Sample|array|int $items * @return $this */ public function deleteItems($items) { $writeAdapter = $this->_getWriteAdapter(); $where = ''; if ($items instanceof \Magento\Downloadable\Model\Sample) { $where = array('sample_id = ?' => $items->getId()); } else { $where = array('sample_id in (?)' => $items); } if ($where) { $writeAdapter->delete($this->getMainTable(), $where); $writeAdapter->delete($this->getTable('downloadable_sample_title'), $where); } return $this; }
public function testGetSampleData() { $expectingFileData = array('sample_file' => array('file' => 'file/sample.gif', 'name' => '<a href="final_url">sample.gif</a>', 'size' => '1.1', 'status' => 'old')); $this->productModel->expects($this->any())->method('getTypeId')->will($this->returnValue('downloadable')); $this->productModel->expects($this->any())->method('getTypeInstance')->will($this->returnValue($this->downloadableProductModel)); $this->productModel->expects($this->any())->method('getStoreId')->will($this->returnValue(0)); $this->downloadableProductModel->expects($this->any())->method('getSamples')->will($this->returnValue(array($this->downloadableSampleModel))); $this->coreRegistry->expects($this->any())->method('registry')->will($this->returnValue($this->productModel)); $this->downloadableSampleModel->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->downloadableSampleModel->expects($this->any())->method('getTitle')->will($this->returnValue('Sample Title')); $this->downloadableSampleModel->expects($this->any())->method('getSampleUrl')->will($this->returnValue(null)); $this->downloadableSampleModel->expects($this->any())->method('getSampleFile')->will($this->returnValue('file/sample.gif')); $this->downloadableSampleModel->expects($this->any())->method('getSampleType')->will($this->returnValue('file')); $this->downloadableSampleModel->expects($this->any())->method('getSortOrder')->will($this->returnValue(0)); $this->escaper->expects($this->any())->method('escapeHtml')->will($this->returnValue('Sample Title')); $this->fileHelper->expects($this->any())->method('getFilePath')->will($this->returnValue('/file/path/sample.gif')); $this->fileHelper->expects($this->any())->method('ensureFileInFilesystem')->will($this->returnValue(true)); $this->fileHelper->expects($this->any())->method('getFileSize')->will($this->returnValue('1.1')); $this->urlBuilder->expects($this->any())->method('getUrl')->will($this->returnValue('final_url')); $sampleData = $this->block->getSampleData(); foreach ($sampleData as $sample) { $fileSave = $sample->getFileSave(0); $this->assertEquals($expectingFileData['sample_file'], $fileSave); } }
/** * Retrieve samples array * * @return array */ public function getSampleData() { $samplesArr = []; if ($this->getProduct()->getTypeId() !== \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { return $samplesArr; } $samples = $this->getProduct()->getTypeInstance()->getSamples($this->getProduct()); $fileHelper = $this->_downloadableFile; foreach ($samples as $item) { $tmpSampleItem = ['sample_id' => $item->getId(), 'title' => $this->escapeHtml($item->getTitle()), 'sample_url' => $item->getSampleUrl(), 'sample_type' => $item->getSampleType(), 'sort_order' => $item->getSortOrder()]; $sampleFile = $item->getSampleFile(); if ($sampleFile) { $file = $fileHelper->getFilePath($this->_sampleModel->getBasePath(), $sampleFile); $fileExist = $fileHelper->ensureFileInFilesystem($file); if ($fileExist) { $name = '<a href="' . $this->getUrl('adminhtml/downloadable_product_edit/sample', ['id' => $item->getId(), '_secure' => true]) . '">' . $fileHelper->getFileFromPathFile($sampleFile) . '</a>'; $tmpSampleItem['file_save'] = [['file' => $sampleFile, 'name' => $name, 'size' => $fileHelper->getFileSize($file), 'status' => 'old']]; } } if ($this->getProduct() && $item->getStoreTitle()) { $tmpSampleItem['store_title'] = $item->getStoreTitle(); } $samplesArr[] = new \Magento\Framework\Object($tmpSampleItem); } return $samplesArr; }
/** * Retrieve samples array * * @return array */ public function getSampleData() { $samplesArr = array(); if ($this->getProduct()->getTypeId() !== \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { return $samplesArr; } $samples = $this->getProduct()->getTypeInstance()->getSamples($this->getProduct()); $fileHelper = $this->_downloadableFile; foreach ($samples as $item) { $tmpSampleItem = array('sample_id' => $item->getId(), 'title' => $this->escapeHtml($item->getTitle()), 'sample_url' => $item->getSampleUrl(), 'sample_type' => $item->getSampleType(), 'sort_order' => $item->getSortOrder()); $sampleFile = $item->getSampleFile(); if ($sampleFile) { $file = $fileHelper->getFilePath($this->_sampleModel->getBasePath(), $sampleFile); $fileExist = $fileHelper->ensureFileInFilesystem($file); if ($fileExist) { $tmpSampleItem['file_save'] = array(array('file' => $sampleFile, 'name' => $fileHelper->getFileFromPathFile($sampleFile), 'size' => $fileHelper->getFileSize($file), 'status' => 'old')); } } if ($this->getProduct() && $item->getStoreTitle()) { $tmpSampleItem['store_title'] = $item->getStoreTitle(); } $samplesArr[] = new \Magento\Framework\Object($tmpSampleItem); } return $samplesArr; }
public function testExecute() { $data = ['tmp_name' => 'tmp_name', 'path' => 'path', 'file' => 'file']; $uploader = $this->getMockBuilder('Magento\\MediaStorage\\Model\\File\\Uploader')->disableOriginalConstructor()->getMock(); $resultJson = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Json')->disableOriginalConstructor()->setMethods(['setData'])->getMock(); $this->request->expects($this->once())->method('getParam')->with('type')->willReturn('samples'); $this->sample->expects($this->once())->method('getBaseTmpPath')->willReturn('base_tmp_path'); $this->uploaderFactory->expects($this->once())->method('create')->willReturn($uploader); $this->fileHelper->expects($this->once())->method('uploadFromTmp')->willReturn($data); $this->storageDatabase->expects($this->once())->method('saveFile'); $this->session->expects($this->once())->method('getName')->willReturn('Name'); $this->session->expects($this->once())->method('getSessionId')->willReturn('SessionId'); $this->session->expects($this->once())->method('getCookieLifetime')->willReturn('CookieLifetime'); $this->session->expects($this->once())->method('getCookiePath')->willReturn('CookiePath'); $this->session->expects($this->once())->method('getCookieDomain')->willReturn('CookieDomain'); $this->resultFactory->expects($this->once())->method('create')->willReturn($resultJson); $resultJson->expects($this->once())->method('setData')->willReturnSelf(); $this->assertEquals($resultJson, $this->upload->execute()); }
/** * Add Sample File info into $sampleData * * @param array $sampleData * @param SampleInterface $sample * @return array */ protected function addSampleFile(array $sampleData, SampleInterface $sample) { $sampleFile = $sample->getSampleFile(); if ($sampleFile) { $file = $this->downloadableFile->getFilePath($this->sampleModel->getBasePath(), $sampleFile); if ($this->downloadableFile->ensureFileInFilesystem($file)) { $sampleData['file'][0] = ['file' => $sampleFile, 'name' => $this->downloadableFile->getFileFromPathFile($sampleFile), 'size' => $this->downloadableFile->getFileSize($file), 'status' => 'old', 'url' => $this->urlBuilder->addSessionParam()->getUrl('adminhtml/downloadable_product_edit/sample', ['id' => $sample->getId(), '_secure' => true])]; } } return $sampleData; }
/** * 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; }
/** * Execute download sample url action */ public function testExecuteUrl() { $this->request->expects($this->at(0))->method('getParam')->with('id', 0)->will($this->returnValue(1)); $this->response->expects($this->once())->method('setHttpResponseCode')->will($this->returnSelf()); $this->response->expects($this->once())->method('clearBody')->will($this->returnSelf()); $this->response->expects($this->any())->method('setHeader')->will($this->returnSelf()); $this->response->expects($this->once())->method('sendHeaders')->will($this->returnSelf()); $this->objectManager->expects($this->at(1))->method('get')->with('Magento\\Downloadable\\Helper\\Download')->will($this->returnValue($this->downloadHelper)); $this->downloadHelper->expects($this->once())->method('setResource')->will($this->returnSelf()); $this->downloadHelper->expects($this->once())->method('getFilename')->will($this->returnValue('sample.jpg')); $this->downloadHelper->expects($this->once())->method('getContentType')->will($this->returnSelf('url')); $this->downloadHelper->expects($this->once())->method('getFileSize')->will($this->returnValue(null)); $this->downloadHelper->expects($this->once())->method('getContentDisposition')->will($this->returnValue(null)); $this->downloadHelper->expects($this->once())->method('output')->will($this->returnSelf()); $this->sampleModel->expects($this->once())->method('load')->will($this->returnSelf()); $this->sampleModel->expects($this->once())->method('getId')->will($this->returnValue('1')); $this->sampleModel->expects($this->any())->method('getSampleType')->will($this->returnValue('url')); $this->objectManager->expects($this->once())->method('create')->will($this->returnValue($this->sampleModel)); $this->sample->execute(); }
/** * Subroutine for buildLink and buildSample * * @param Sample $resourceData * @param SampleInterface $dataObject * @return null */ protected function setBasicFields($resourceData, $dataObject) { $dataObject->setId($resourceData->getId()); $storeTitle = $resourceData->getStoreTitle(); $title = $resourceData->getTitle(); if (!empty($storeTitle)) { $dataObject->setTitle($storeTitle); } else { $dataObject->setTitle($title); } $dataObject->setSortOrder($resourceData->getSortOrder()); $dataObject->setSampleType($resourceData->getSampleType()); $dataObject->setSampleFile($resourceData->getSampleFile()); $dataObject->setSampleUrl($resourceData->getSampleUrl()); }
/** * Build file info data object * * @param string $entityType 'link' or 'sample' * @param \Magento\Downloadable\Model\Link|\Magento\Downloadable\Model\Sample $resourceData * @return \Magento\Downloadable\Service\V1\DownloadableLink\Data\DownloadableResourceInfo|null */ protected function entityInfoGenerator($entityType, $resourceData) { $type = $resourceData->getData($entityType . '_type'); if (empty($type)) { return null; } $this->resourceBuilder->populateWithArray([]); $this->resourceBuilder->setType($type); $this->resourceBuilder->setUrl($resourceData->getData($entityType . '_url')); $this->resourceBuilder->setFile($resourceData->getData($entityType . '_file')); return $this->resourceBuilder->create(); }