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_Adminhtml_Catalog_Product_GalleryController extends Mage_Adminhtml_Controller_Action
35: {
36: public function uploadAction()
37: {
38: try {
39: $uploader = new Mage_Core_Model_File_Uploader('image');
40: $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
41: $uploader->addValidateCallback('catalog_product_image',
42: Mage::helper('catalog/image'), 'validateUploadFile');
43: $uploader->setAllowRenameFiles(true);
44: $uploader->setFilesDispersion(true);
45: $result = $uploader->save(
46: Mage::getSingleton('catalog/product_media_config')->getBaseTmpMediaPath()
47: );
48:
49: Mage::dispatchEvent('catalog_product_gallery_upload_image_after', array(
50: 'result' => $result,
51: 'action' => $this
52: ));
53:
54: 55: 56:
57: $result['tmp_name'] = str_replace(DS, "/", $result['tmp_name']);
58: $result['path'] = str_replace(DS, "/", $result['path']);
59:
60: $result['url'] = Mage::getSingleton('catalog/product_media_config')->getTmpMediaUrl($result['file']);
61: $result['file'] = $result['file'] . '.tmp';
62: $result['cookie'] = array(
63: 'name' => session_name(),
64: 'value' => $this->_getSession()->getSessionId(),
65: 'lifetime' => $this->_getSession()->getCookieLifetime(),
66: 'path' => $this->_getSession()->getCookiePath(),
67: 'domain' => $this->_getSession()->getCookieDomain()
68: );
69:
70: } catch (Exception $e) {
71: $result = array(
72: 'error' => $e->getMessage(),
73: 'errorcode' => $e->getCode());
74: }
75:
76: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
77: }
78:
79: protected function _isAllowed()
80: {
81: return Mage::getSingleton('admin/session')->isAllowed('catalog/products');
82: }
83: }
84: