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:
35: class Mage_Core_Model_File_Storage_Database extends Mage_Core_Model_File_Storage_Database_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_eventPrefix = 'core_file_storage_database';
43:
44: 45: 46: 47: 48:
49: protected $_directoryModel = null;
50:
51: 52: 53: 54: 55:
56: protected $_errors = array();
57:
58: 59: 60: 61: 62:
63: public function __construct($connectionName = null)
64: {
65: $this->_init('core/file_storage_database');
66:
67: parent::__construct($connectionName);
68: }
69:
70: 71: 72: 73: 74:
75: public function getDirectoryModel()
76: {
77: if (is_null($this->_directoryModel)) {
78: $this->_directoryModel = Mage::getModel(
79: 'core/file_storage_directory_database',
80: array('connection' => $this->getConnectionName()));
81: }
82:
83: return $this->_directoryModel;
84: }
85:
86: 87: 88: 89: 90:
91: public function init()
92: {
93: $this->getDirectoryModel()->prepareStorage();
94: $this->prepareStorage();
95:
96: return $this;
97: }
98:
99: 100: 101: 102: 103:
104: public function getStorageName()
105: {
106: return Mage::helper('core')->__('database "%s"', $this->getConnectionName());
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function loadByFilename($filePath)
116: {
117: $filename = basename($filePath);
118: $path = dirname($filePath);
119: $this->_getResource()->loadByFilename($this, $filename, $path);
120: return $this;
121: }
122:
123: 124: 125: 126: 127:
128: public function hasErrors()
129: {
130: return (!empty($this->_errors) || $this->getDirectoryModel()->hasErrors());
131: }
132:
133: 134: 135: 136: 137:
138: public function clear()
139: {
140: $this->getDirectoryModel()->clearDirectories();
141: $this->_getResource()->clearFiles();
142: return $this;
143: }
144:
145: 146: 147: 148: 149: 150: 151:
152: public function exportDirectories($offset = 0, $count = 100) {
153: return $this->getDirectoryModel()->exportDirectories($offset, $count);
154: }
155:
156: 157: 158: 159: 160: 161:
162: public function importDirectories($dirs) {
163: return $this->getDirectoryModel()->importDirectories($dirs);
164: }
165:
166: 167: 168: 169: 170: 171: 172:
173: public function exportFiles($offset = 0, $count = 100)
174: {
175: $offset = ((int) $offset >= 0) ? (int) $offset : 0;
176: $count = ((int) $count >= 1) ? (int) $count : 1;
177:
178: $result = $this->_getResource()->getFiles($offset, $count);
179: if (empty($result)) {
180: return false;
181: }
182:
183: return $result;
184: }
185:
186: 187: 188: 189: 190: 191:
192: public function importFiles($files)
193: {
194: if (!is_array($files)) {
195: return $this;
196: }
197:
198: $dateSingleton = Mage::getSingleton('core/date');
199: foreach ($files as $file) {
200: if (!isset($file['filename']) || !strlen($file['filename']) || !isset($file['content'])) {
201: continue;
202: }
203:
204: try {
205: $file['update_time'] = $dateSingleton->date();
206: $file['directory_id'] = (isset($file['directory']) && strlen($file['directory']))
207: ? Mage::getModel(
208: 'core/file_storage_directory_database',
209: array('connection' => $this->getConnectionName()))
210: ->loadByPath($file['directory'])->getId()
211: : null;
212:
213: $this->_getResource()->saveFile($file);
214: } catch (Exception $e) {
215: $this->_errors[] = $e->getMessage();
216: Mage::logException($e);
217: }
218: }
219:
220: return $this;
221: }
222:
223: 224: 225: 226: 227: 228:
229: public function saveFile($filename)
230: {
231: $fileInfo = $this->collectFileInfo($filename);
232: $filePath = $fileInfo['directory'];
233:
234: $directory = Mage::getModel('core/file_storage_directory_database')->loadByPath($filePath);
235:
236: if (!$directory->getId()) {
237: $directory = $this->getDirectoryModel()->createRecursive($filePath);
238: }
239:
240: $fileInfo['directory_id'] = $directory->getId();
241: $this->_getResource()->saveFile($fileInfo);
242:
243: return $this;
244: }
245:
246: 247: 248: 249: 250: 251:
252: public function fileExists($filePath)
253: {
254: return $this->_getResource()->fileExists(basename($filePath), dirname($filePath));
255: }
256:
257: 258: 259: 260: 261: 262: 263:
264: public function copyFile($oldFilePath, $newFilePath)
265: {
266: $this->_getResource()->copyFile(
267: basename($oldFilePath),
268: dirname($oldFilePath),
269: basename($newFilePath),
270: dirname($newFilePath)
271: );
272:
273: return $this;
274: }
275:
276: 277: 278: 279: 280: 281: 282:
283: public function renameFile($oldFilePath, $newFilePath)
284: {
285: $this->_getResource()->renameFile(
286: basename($oldFilePath),
287: dirname($oldFilePath),
288: basename($newFilePath),
289: dirname($newFilePath)
290: );
291:
292: $newPath = dirname($newFilePath);
293: $directory = Mage::getModel('core/file_storage_directory_database')->loadByPath($newPath);
294:
295: if (!$directory->getId()) {
296: $directory = $this->getDirectoryModel()->createRecursive($newPath);
297: }
298:
299: $this->loadByFilename($newFilePath);
300: if ($this->getId()) {
301: $this->setDirectoryId($directory->getId())->save();
302: }
303:
304: return $this;
305: }
306:
307: 308: 309: 310: 311: 312:
313: public function getDirectoryFiles($directory)
314: {
315: $directory = Mage::helper('core/file_storage_database')->getMediaRelativePath($directory);
316: return $this->_getResource()->getDirectoryFiles($directory);
317: }
318:
319: 320: 321: 322: 323: 324:
325: public function deleteFile($path)
326: {
327: $filename = basename($path);
328: $directory = dirname($path);
329: $this->_getResource()->deleteFile($filename, $directory);
330:
331: return $this;
332: }
333: }
334: