/** * Upload file controller action */ public function uploadAction() { $type = $this->getRequest()->getParam('type'); $tmpPath = ''; if ($type == 'samples') { $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath(); } elseif ($type == 'links') { $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath(); } elseif ($type == 'link_samples') { $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath(); } $result = array(); try { $uploader = new Mage_Core_Model_File_Uploader($type); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $result = $uploader->save($tmpPath); /** * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS */ $result['tmp_name'] = str_replace(DS, "/", $result['tmp_name']); $result['path'] = str_replace(DS, "/", $result['path']); if (isset($result['file'])) { $fullPath = rtrim($tmpPath, DS) . DS . ltrim($result['file'], DS); Mage::helper('core/file_storage_database')->saveFile($fullPath); } $result['cookie'] = array('name' => session_name(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain()); } catch (Exception $e) { $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode()); } $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result)); }
/** * Upload file controller action */ public function uploadAction() { $type = $this->getRequest()->getParam('type'); $tmpPath = ''; if ($type == 'samples') { $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath(); } elseif ($type == 'links') { $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath(); } elseif ($type == 'link_samples') { $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath(); } $result = array(); try { $uploader = new Mage_Core_Model_File_Uploader($type); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $result = $uploader->save($tmpPath); if (isset($result['file'])) { $fullPath = rtrim($tmpPath, DS) . DS . ltrim($result['file'], DS); Mage::helper('Mage_Core_Helper_File_Storage_Database')->saveFile($fullPath); } $result['cookie'] = array('name' => session_name(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain()); } catch (Exception $e) { $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode()); } $this->getResponse()->setBody(Mage::helper('Mage_Core_Helper_Data')->jsonEncode($result)); }
/** * Decode file from base64 and upload it to donwloadable 'tmp' folder * * @param array $fileInfo * @param string $type * @return string */ protected function _uploadFile($fileInfo, $type) { $tmpPath = ''; if ($type == 'sample') { $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath(); } elseif ($type == 'link') { $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath(); } elseif ($type == 'link_samples') { $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath(); } $result = array(); try { $uploader = Mage::getModel('downloadable/link_api_uploader', $fileInfo); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $result = $uploader->save($tmpPath); if (isset($result['file'])) { $fullPath = rtrim($tmpPath, DS) . DS . ltrim($result['file'], DS); Mage::helper('core/file_storage_database')->saveFile($fullPath); } } catch (Exception $e) { if ($e->getMessage() != '') { $this->_fault('upload_failed', $e->getMessage()); } else { $this->_fault($e->getCode()); } } $result['status'] = 'new'; $result['name'] = substr($result['file'], strrpos($result['file'], '/') + 1); return Mage::helper('core')->jsonEncode(array($result)); }
/** * Upload file controller action */ public function uploadAction() { $type = $this->getRequest()->getParam('type'); $tmpPath = ''; if ($type == 'samples') { $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath(); } elseif ($type == 'links') { $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath(); } elseif ($type == 'link_samples') { $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath(); } $result = array(); try { $uploader = new Varien_File_Uploader($type); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $result = $uploader->save($tmpPath); $result['cookie'] = array('name' => session_name(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain()); } catch (Exception $e) { $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode()); } $this->getResponse()->setBody(Zend_Json::encode($result)); }
/** * Save Product downloadable information (links and samples) * * @param Mage_Catalog_Model_Product $product * @return Mage_Downloadable_Model_Product_Type */ public function save($product = null) { parent::save($product); $product = $this->getProduct($product); /* @var Mage_Catalog_Model_Product $product */ if ($data = $product->getDownloadableData()) { if (isset($data['sample'])) { $_deleteItems = array(); foreach ($data['sample'] as $sampleItem) { if ($sampleItem['is_delete'] == '1') { if ($sampleItem['sample_id']) { $_deleteItems[] = $sampleItem['sample_id']; } } else { unset($sampleItem['is_delete']); if (!$sampleItem['sample_id']) { unset($sampleItem['sample_id']); } $sampleModel = Mage::getModel('downloadable/sample'); $files = array(); if (isset($sampleItem['file'])) { $files = Mage::helper('core')->jsonDecode($sampleItem['file']); unset($sampleItem['file']); } $sampleModel->setData($sampleItem)->setSampleType($sampleItem['type'])->setProductId($product->getId())->setStoreId($product->getStoreId()); if ($sampleModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) { $sampleFileName = Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Sample::getBaseTmpPath(), Mage_Downloadable_Model_Sample::getBasePath(), $files); $sampleModel->setSampleFile($sampleFileName); } $sampleModel->save(); } } if ($_deleteItems) { Mage::getResourceModel('downloadable/sample')->deleteItems($_deleteItems); } } if (isset($data['link'])) { $_deleteItems = array(); foreach ($data['link'] as $linkItem) { if ($linkItem['is_delete'] == '1') { if ($linkItem['link_id']) { $_deleteItems[] = $linkItem['link_id']; } } else { unset($linkItem['is_delete']); if (!$linkItem['link_id']) { unset($linkItem['link_id']); } $files = array(); if (isset($linkItem['file'])) { $files = Mage::helper('core')->jsonDecode($linkItem['file']); unset($linkItem['file']); } $sample = array(); if (isset($linkItem['sample'])) { $sample = $linkItem['sample']; unset($linkItem['sample']); } $linkModel = Mage::getModel('downloadable/link')->setData($linkItem)->setLinkType($linkItem['type'])->setProductId($product->getId())->setStoreId($product->getStoreId())->setWebsiteId($product->getStore()->getWebsiteId())->setProductWebsiteIds($product->getWebsiteIds()); if (null === $linkModel->getPrice()) { $linkModel->setPrice(0); } if ($linkModel->getIsUnlimited()) { $linkModel->setNumberOfDownloads(0); } $sampleFile = array(); if ($sample && isset($sample['type'])) { if ($sample['type'] == 'url' && $sample['url'] != '') { $linkModel->setSampleUrl($sample['url']); } $linkModel->setSampleType($sample['type']); $sampleFile = Mage::helper('core')->jsonDecode($sample['file']); } if ($linkModel->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) { $linkFileName = Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Link::getBaseTmpPath(), Mage_Downloadable_Model_Link::getBasePath(), $files); $linkModel->setLinkFile($linkFileName); } if ($linkModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) { $linkSampleFileName = Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Link::getBaseSampleTmpPath(), Mage_Downloadable_Model_Link::getBaseSamplePath(), $sampleFile); $linkModel->setSampleFile($linkSampleFileName); } $linkModel->save(); } } if ($_deleteItems) { Mage::getResourceModel('downloadable/link')->deleteItems($_deleteItems); } if ($this->getProduct($product)->getLinksPurchasedSeparately()) { $this->getProduct($product)->setIsCustomOptionChanged(); } } } return $this; }
/** * Decode file from base64 and upload it to donwloadable 'tmp' folder * * @param array $fileInfo * @param string $type * @return string */ protected function _uploadFile($fileInfo, $type) { $tmpPath = ''; if ($type == 'sample') { $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath(); } elseif ($type == 'link') { $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath(); } elseif ($type == 'link_samples') { $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath(); } $result = array(); $url = $fileInfo['url']; $remoteFileName = $fileInfo['name']; $ioAdapter = new Varien_Io_File(); $ioAdapter->checkAndCreateFolder($tmpPath); $ioAdapter->open(array('path' => $tmpPath)); $fileName = $tmpPath . DS . Varien_File_Uploader::getCorrectFileName($remoteFileName); if ($ioAdapter->cp($url, $fileName)) { Mage::helper('core/file_storage_database')->saveFile($fileName); } $result['file'] = $remoteFileName; $result['status'] = 'new'; $result['name'] = $remoteFileName; return Mage::helper('core')->jsonEncode(array($result)); }
/** * Enter description here... * * @return Mage_Downloadable_Model_Product_Type */ public function save() { parent::save(); $product = $this->getProduct(); /* @var Mage_Catalog_Model_Product $product */ if ($data = $product->getDownloadableData()) { if (isset($data['sample'])) { $_deleteItems = array(); foreach ($data['sample'] as $sampleItem) { if ($sampleItem['is_delete'] == '1') { if ($sampleItem['sample_id']) { $_deleteItems[] = $sampleItem['sample_id']; } } else { unset($sampleItem['is_delete']); if (!$sampleItem['sample_id']) { unset($sampleItem['sample_id']); } $sampleModel = Mage::getModel('downloadable/sample'); $files = array(); if (isset($sampleItem['file'])) { $files = Zend_Json::decode($sampleItem['file']); unset($sampleItem['file']); } $sampleModel->setData($sampleItem)->setSampleType($sampleItem['type'])->setProductId($product->getId())->setStoreId($product->getStoreId()); $fileStatusNew = false; if (isset($files[0]) && $sampleModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) { $sampleModel->setSampleFile($files[0]['file']); if ($files[0]['status'] == 'new') { $fileStatusNew = true; } } $sampleModel->save(); if ($sampleModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE && $fileStatusNew) { try { Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Sample::getBaseTmpPath(), Mage_Downloadable_Model_Sample::getBasePath(), $files[0]['file']); } catch (Exception $e) { Mage::throwException(Mage::helper('downloadable')->__('An error occurred while saving the file(s).')); } } } } if ($_deleteItems) { Mage::getResourceModel('downloadable/sample')->deleteItems($_deleteItems); } } if (isset($data['link'])) { $_deleteItems = array(); foreach ($data['link'] as $linkItem) { if ($linkItem['is_delete'] == '1') { if ($linkItem['link_id']) { $_deleteItems[] = $linkItem['link_id']; } } else { unset($linkItem['is_delete']); if (!$linkItem['link_id']) { unset($linkItem['link_id']); } $files = array(); if (isset($linkItem['file'])) { $files = Zend_Json::decode($linkItem['file']); unset($linkItem['file']); } $sample = array(); if (isset($linkItem['sample'])) { $sample = $linkItem['sample']; unset($linkItem['sample']); } $linkModel = Mage::getModel('downloadable/link')->setData($linkItem)->setLinkType($linkItem['type'])->setProductId($product->getId())->setStoreId($product->getStoreId())->setWebsiteId($product->getStore()->getWebsiteId()); if (null === $linkModel->getPrice()) { $linkModel->setPrice(0); } if ($linkModel->getIsUnlimited()) { $linkModel->setNumberOfDownloads(0); } $fileStatusNew = false; if (isset($files[0]) && $linkModel->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) { $linkModel->setLinkFile($files[0]['file']); if ($files[0]['status'] == 'new') { $fileStatusNew = true; } } $sampleFileNew = false; if ($sample && isset($sample['type'])) { if ($sample['type'] == 'url' && $sample['url'] != '') { $linkModel->setSampleUrl($sample['url'])->setSampleType($sample['type']); } $sampleFile = Zend_Json::decode($sample['file']); if ($sample['type'] == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE && isset($sampleFile[0])) { $linkModel->setSampleFile($sampleFile[0]['file'])->setSampleType($sample['type']); if ($sampleFile[0]['status'] == 'new') { $sampleFileNew = true; } } } $linkModel->save(); if ($linkModel->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE && $fileStatusNew) { try { Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Link::getBaseTmpPath(), Mage_Downloadable_Model_Link::getBasePath(), $files[0]['file']); } catch (Exception $e) { Mage::throwException(Mage::helper('downloadable')->__('An error occurred while saving the file(s).')); } } if ($linkModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE && $sampleFileNew) { try { Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Link::getBaseSampleTmpPath(), Mage_Downloadable_Model_Link::getBaseSamplePath(), $sampleFile[0]['file']); } catch (Exception $e) { Mage::throwException(Mage::helper('downloadable')->__('An error occurred while saving the file(s).')); } } } } if ($_deleteItems) { Mage::getResourceModel('downloadable/link')->deleteItems($_deleteItems); } } } return $this; }
/** * Get sample file name * * @param array $sampleModel * @param array $sample * @return string $sampleFileName */ public function getSampleFileName($sampleModel, $sample) { $sampleFileName = ''; if ($sampleModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) { $sampleFileName = Mage::helper('downloadable/file')->moveFileFromTmp(Mage_Downloadable_Model_Sample::getBaseTmpPath(), Mage_Downloadable_Model_Sample::getBasePath(), $sample); } return $sampleFileName; }
/** * Get temporary path for downloadable product * * @param array $key * @return array $tmpPathResult */ public function getTmpPathForDownloadable($key) { $type = $tmpPath = ''; $tmpPathResult = array(); if (substr($key, 0, 5) == 'sampl') { $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath(); $type = 'samples'; } if (substr($key, 0, 5) == 'links') { $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath(); $type = 'links'; } if (substr($key, 0, 5) == 'l_sam') { $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath(); $type = 'link_samples'; } $tmpPathResult['type'] = $type; $tmpPathResult['tmp_path'] = $tmpPath; return $tmpPathResult; }