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_ImportExport_Model_Import_Uploader extends Mage_Core_Model_File_Uploader
35: {
36: protected $_tmpDir = '';
37: protected $_destDir = '';
38: protected $_allowedMimeTypes = array(
39: 'jpg' => 'image/jpeg',
40: 'jpeg' => 'image/jpeg',
41: 'gif' => 'image/gif',
42: 'png' => 'image/png'
43: );
44: const DEFAULT_FILE_TYPE = 'application/octet-stream';
45:
46: function __construct($filePath = null)
47: {
48: if (!is_null($filePath)) {
49: $this->_setUploadFile($filePath);
50: }
51: }
52:
53: 54: 55:
56: public function init()
57: {
58: $this->setAllowRenameFiles(true);
59: $this->setAllowCreateFolders(true);
60: $this->setFilesDispersion(true);
61: $this->setAllowedExtensions(array_keys($this->_allowedMimeTypes));
62: $this->addValidateCallback('catalog_product_image',
63: Mage::helper('catalog/image'), 'validateUploadFile');
64: $this->_uploadType = self::SINGLE_STYLE;
65: }
66:
67: 68: 69: 70: 71: 72:
73: public function move($fileName)
74: {
75: $filePath = realpath($this->getTmpDir() . DS . $fileName);
76: $this->_setUploadFile($filePath);
77: $result = $this->save($this->getDestDir());
78: $result['name'] = self::getCorrectFileName($result['name']);
79: return $result;
80: }
81:
82: 83: 84: 85: 86:
87: protected function _setUploadFile($filePath)
88: {
89: if (!is_readable($filePath)) {
90: Mage::throwException("File '{$filePath}' was not found or has read restriction.");
91: }
92: $this->_file = $this->_readFileInfo($filePath);
93:
94: $this->_validateFile();
95: }
96:
97: 98: 99: 100: 101: 102:
103: protected function _readFileInfo($filePath)
104: {
105: $fileInfo = pathinfo($filePath);
106:
107: return array(
108: 'name' => $fileInfo['basename'],
109: 'type' => $this->_getMimeTypeByExt($fileInfo['extension']),
110: 'tmp_name' => $filePath,
111: 'error' => 0,
112: 'size' => filesize($filePath)
113: );
114: }
115:
116: 117: 118:
119: protected function _validateFile()
120: {
121: $filePath = $this->_file['tmp_name'];
122: if (is_readable($filePath)) {
123: $this->_fileExists = true;
124: } else {
125: $this->_fileExists = false;
126: }
127:
128: $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
129: if (!$this->checkAllowedExtension($fileExtension)) {
130: throw new Exception('Disallowed file type.');
131: }
132:
133: foreach ($this->_validateCallbacks as $params) {
134: if (is_object($params['object']) && method_exists($params['object'], $params['method'])) {
135: $params['object']->$params['method']($filePath);
136: }
137: }
138: }
139:
140: 141: 142: 143: 144: 145:
146: protected function _getMimeTypeByExt($ext)
147: {
148: if (array_key_exists($ext, $this->_allowedMimeTypes)) {
149: return $this->_allowedMimeTypes[$ext];
150: }
151: return '';
152: }
153:
154: 155: 156: 157: 158:
159: public function getTmpDir()
160: {
161: return $this->_tmpDir;
162: }
163:
164: 165: 166: 167: 168: 169:
170: public function setTmpDir($path)
171: {
172: if (is_string($path) && is_readable($path)) {
173: $this->_tmpDir = $path;
174: return true;
175: }
176: return false;
177: }
178:
179: 180: 181: 182: 183:
184: public function getDestDir()
185: {
186: return $this->_destDir;
187: }
188:
189: 190: 191: 192: 193: 194:
195: public function setDestDir($path)
196: {
197: if (is_string($path) && is_writable($path)) {
198: $this->_destDir = $path;
199: return true;
200: }
201: return false;
202: }
203:
204: 205: 206: 207: 208: 209: 210:
211: protected function _moveFile($tmpPath, $destPath)
212: {
213: $sourceFile = realpath($tmpPath);
214: if ($sourceFile !== false) {
215: return copy($sourceFile, $destPath);
216: } else {
217: return false;
218: }
219: }
220:
221: }
222: