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_Core_Helper_File_Storage_Database extends Mage_Core_Helper_Abstract
35: {
36: 37: 38: 39:
40: protected $_databaseModel = null;
41:
42: 43: 44: 45:
46: protected $_resourceModel = null;
47:
48: 49: 50: 51: 52:
53: protected $_useDb = null;
54:
55: 56: 57: 58: 59:
60: protected $_mediaBaseDirectory;
61:
62: 63: 64: 65: 66: 67:
68: public function checkDbUsage()
69: {
70: if (null === $this->_useDb) {
71: $currentStorage = (int) Mage::app()->getConfig()
72: ->getNode(Mage_Core_Model_File_Storage::XML_PATH_STORAGE_MEDIA);
73: $this->_useDb = ($currentStorage == Mage_Core_Model_File_Storage::STORAGE_MEDIA_DATABASE);
74: }
75:
76: return $this->_useDb;
77: }
78:
79: 80: 81: 82: 83:
84: public function getStorageDatabaseModel()
85: {
86: if (is_null($this->_databaseModel)) {
87: $this->_databaseModel = Mage::getModel('core/file_storage_database');
88: }
89:
90: return $this->_databaseModel;
91: }
92:
93: 94: 95: 96: 97:
98: public function getStorageFileModel()
99: {
100: return Mage::getSingleton('core/file_storage_file');
101: }
102:
103: 104: 105: 106: 107:
108: public function getResourceStorageModel()
109: {
110: if (is_null($this->_resourceModel)) {
111: $this->_resourceModel = $this->getStorageDatabaseModel()->getResource();
112: }
113: return $this->_resourceModel;
114: }
115:
116: 117: 118: 119: 120:
121: public function saveFile($filename)
122: {
123: if ($this->checkDbUsage()) {
124: $this->getStorageDatabaseModel()->saveFile($this->_removeAbsPathFromFileName($filename));
125: }
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function renameFile($oldName, $newName)
135: {
136: if ($this->checkDbUsage()) {
137: $this->getStorageDatabaseModel()
138: ->renameFile($this->_removeAbsPathFromFileName($oldName), $this->_removeAbsPathFromFileName($newName));
139: }
140: }
141:
142: 143: 144: 145: 146: 147:
148: public function copyFile($oldName, $newName) {
149: if ($this->checkDbUsage()) {
150: $this->getStorageDatabaseModel()
151: ->copyFile($this->_removeAbsPathFromFileName($oldName), $this->_removeAbsPathFromFileName($newName));
152: }
153: }
154:
155: 156: 157: 158: 159: 160:
161: public function fileExists($filename)
162: {
163: if ($this->checkDbUsage()) {
164: return $this->getStorageDatabaseModel()->fileExists($this->_removeAbsPathFromFileName($filename));
165: } else {
166: return null;
167: }
168: }
169:
170: 171: 172: 173: 174: 175: 176:
177: public function getUniqueFilename($directory, $filename)
178: {
179: if ($this->checkDbUsage()) {
180: $directory = $this->_removeAbsPathFromFileName($directory);
181: if($this->fileExists($directory . $filename)) {
182: $index = 1;
183: $extension = strrchr($filename, '.');
184: $filenameWoExtension = substr($filename, 0, -1 * strlen($extension));
185: while ($this->fileExists($directory . $filenameWoExtension . '_' . $index . $extension)) {
186: $index ++;
187: }
188: $filename = $filenameWoExtension . '_' . $index . $extension;
189: }
190: }
191: return $filename;
192: }
193:
194: 195: 196: 197: 198: 199:
200: public function saveFileToFilesystem($filename) {
201: if ($this->checkDbUsage()) {
202:
203: $file = Mage::getModel('core/file_storage_database')
204: ->loadByFilename($this->_removeAbsPathFromFileName($filename));
205: if (!$file->getId()) {
206: return false;
207: }
208:
209: return $this->getStorageFileModel()->saveFile($file, true);
210: }
211: }
212:
213: 214: 215: 216: 217: 218:
219: public function getMediaRelativePath($fullPath)
220: {
221: $relativePath = ltrim(str_replace($this->getMediaBaseDir(), '', $fullPath), '\\/');
222: return str_replace(DS, '/', $relativePath);
223: }
224:
225: 226: 227: 228: 229:
230: public function deleteFolder($folderName)
231: {
232: if ($this->checkDbUsage()) {
233: $this->getResourceStorageModel()->deleteFolder($this->_removeAbsPathFromFileName($folderName));
234: }
235: }
236:
237: 238: 239: 240: 241:
242: public function deleteFile($filename)
243: {
244: if ($this->checkDbUsage()) {
245: $this->getStorageDatabaseModel()->deleteFile($this->_removeAbsPathFromFileName($filename));
246: }
247: }
248:
249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261:
262: public function saveUploadedFile($result = array())
263: {
264: if ($this->checkDbUsage()) {
265: $path = rtrim(str_replace(array('\\', '/'), DS, $result['path']), DS);
266: $file = '/' . ltrim($result['file'], '\\/');
267:
268: $uniqueResultFile = $this->getUniqueFilename($path, $file);
269:
270: if ($uniqueResultFile !== $file) {
271: $ioFile = new Varien_Io_File();
272: $ioFile->open(array('path' => $path));
273: $ioFile->mv($path . $file, $path . $uniqueResultFile);
274: }
275: $this->saveFile($path . $uniqueResultFile);
276:
277: return $uniqueResultFile;
278: } else {
279: return $result['file'];
280: }
281: }
282:
283: 284: 285: 286: 287: 288: 289:
290: protected function _removeAbsPathFromFileName($filename)
291: {
292: return $this->getMediaRelativePath($filename);
293: }
294:
295: 296: 297: 298: 299:
300: public function getMediaBaseDir()
301: {
302: if (null === $this->_mediaBaseDirectory) {
303: $this->_mediaBaseDirectory = rtrim(Mage::getBaseDir('media'), '\\/');
304: }
305: return $this->_mediaBaseDirectory;
306: }
307: }
308: