1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Downloadable_Adminhtml_Downloadable_FileController extends Mage_Adminhtml_Controller_Action
35: {
36:
37: 38: 39:
40: public function uploadAction()
41: {
42: $type = $this->getRequest()->getParam('type');
43: $tmpPath = '';
44: if ($type == 'samples') {
45: $tmpPath = Mage_Downloadable_Model_Sample::getBaseTmpPath();
46: } elseif ($type == 'links') {
47: $tmpPath = Mage_Downloadable_Model_Link::getBaseTmpPath();
48: } elseif ($type == 'link_samples') {
49: $tmpPath = Mage_Downloadable_Model_Link::getBaseSampleTmpPath();
50: }
51: $result = array();
52: try {
53: $uploader = new Mage_Core_Model_File_Uploader($type);
54: $uploader->setAllowRenameFiles(true);
55: $uploader->setFilesDispersion(true);
56: $result = $uploader->save($tmpPath);
57:
58: 59: 60:
61: $result['tmp_name'] = str_replace(DS, "/", $result['tmp_name']);
62: $result['path'] = str_replace(DS, "/", $result['path']);
63:
64: if (isset($result['file'])) {
65: $fullPath = rtrim($tmpPath, DS) . DS . ltrim($result['file'], DS);
66: Mage::helper('core/file_storage_database')->saveFile($fullPath);
67: }
68:
69: $result['cookie'] = array(
70: 'name' => session_name(),
71: 'value' => $this->_getSession()->getSessionId(),
72: 'lifetime' => $this->_getSession()->getCookieLifetime(),
73: 'path' => $this->_getSession()->getCookiePath(),
74: 'domain' => $this->_getSession()->getCookieDomain()
75: );
76: } catch (Exception $e) {
77: $result = array('error'=>$e->getMessage(), 'errorcode'=>$e->getCode());
78: }
79:
80: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
81: }
82:
83: 84: 85: 86: 87:
88: protected function _isAllowed()
89: {
90: return Mage::getSingleton('admin/session')->isAllowed('catalog/products');
91: }
92:
93: }
94: