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_Resource_File_Storage_Database extends Mage_Core_Model_Resource_File_Storage_Abstract
36: {
37: 38: 39:
40: protected function _construct()
41: {
42: $this->_init('core/file_storage', 'file_id');
43: }
44:
45: 46: 47: 48: 49:
50: public function createDatabaseScheme()
51: {
52: $adapter = $this->_getWriteAdapter();
53: $table = $this->getMainTable();
54: if ($adapter->isTableExists($table)) {
55: return $this;
56: }
57:
58: $dirStorageTable = $this->getTable('core/directory_storage');
59:
60: $ddlTable = $adapter->newTable($table)
61: ->addColumn('file_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
62: 'identity' => true,
63: 'unsigned' => true,
64: 'nullable' => false,
65: 'primary' => true
66: ), 'File Id')
67: ->addColumn('content', Varien_Db_Ddl_Table::TYPE_VARBINARY, Varien_Db_Ddl_Table::MAX_VARBINARY_SIZE, array(
68: 'nullable' => false
69: ), 'File Content')
70: ->addColumn('upload_time', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
71: 'nullable' => false,
72: 'default' => Varien_Db_Ddl_Table::TIMESTAMP_INIT
73: ), 'Upload Timestamp')
74: ->addColumn('filename', Varien_Db_Ddl_Table::TYPE_TEXT, 100, array(
75: 'nullable' => false
76: ), 'Filename')
77: ->addColumn('directory_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
78: 'unsigned' => true,
79: 'default' => null
80: ), 'Identifier of Directory where File is Located')
81: ->addColumn('directory', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
82: 'default' => null
83: ), 'Directory Path')
84: ->addIndex($adapter->getIndexName($table, array('filename', 'directory_id'),
85: Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE),
86: array('filename', 'directory_id'), array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE))
87: ->addIndex($adapter->getIndexName($table, array('directory_id')), array('directory_id'))
88: ->addForeignKey($adapter->getForeignKeyName($table, 'directory_id', $dirStorageTable, 'directory_id'),
89: 'directory_id', $dirStorageTable, 'directory_id',
90: Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
91: ->setComment('File Storage');
92:
93: $adapter->createTable($ddlTable);
94: return $this;
95: }
96:
97: 98: 99: 100: 101: 102:
103: protected function _decodeFileContent($row)
104: {
105: $row['content'] = $this->_getReadAdapter()->decodeVarbinary($row['content']);
106: return $row;
107: }
108:
109: 110: 111: 112: 113: 114:
115: protected function _decodeAllFilesContent($rows)
116: {
117: foreach ($rows as $key => $row) {
118: $rows[$key] = $this->_decodeFileContent($row);
119: }
120: return $rows;
121: }
122:
123: 124: 125: 126: 127: 128: 129: 130:
131: public function loadByFilename(Mage_Core_Model_File_Storage_Database $object, $filename, $path)
132: {
133: $adapter = $this->_getReadAdapter();
134:
135: $select = $adapter->select()
136: ->from(array('e' => $this->getMainTable()))
137: ->where('filename = ?', $filename)
138: ->where($adapter->prepareSqlCondition('directory', array('seq' => $path)));
139:
140: $row = $adapter->fetchRow($select);
141: if ($row) {
142: $row = $this->_decodeFileContent($row);
143: $object->setData($row);
144: $this->_afterLoad($object);
145: }
146:
147: return $this;
148: }
149:
150: 151: 152: 153: 154:
155: public function clearFiles()
156: {
157: $adapter = $this->_getWriteAdapter();
158: $adapter->delete($this->getMainTable());
159:
160: return $this;
161: }
162:
163: 164: 165: 166: 167: 168: 169:
170: public function getFiles($offset = 0, $count = 100)
171: {
172: $adapter = $this->_getReadAdapter();
173:
174: $select = $adapter->select()
175: ->from(
176: array('e' => $this->getMainTable()),
177: array('filename', 'content', 'directory')
178: )
179: ->order('file_id')
180: ->limit($count, $offset);
181:
182: $rows = $adapter->fetchAll($select);
183: return $this->_decodeAllFilesContent($rows);
184: }
185:
186: 187: 188: 189: 190: 191:
192: public function saveFile($file)
193: {
194: $adapter = $this->_getWriteAdapter();
195:
196: $contentParam = new Varien_Db_Statement_Parameter($file['content']);
197: $contentParam->setIsBlob(true);
198: $data = array(
199: 'content' => $contentParam,
200: 'upload_time' => $file['update_time'],
201: 'filename' => $file['filename'],
202: 'directory_id' => $file['directory_id'],
203: 'directory' => $file['directory']
204: );
205:
206: $adapter->insertOnDuplicate($this->getMainTable(), $data, array('content', 'upload_time'));
207:
208: return $this;
209: }
210:
211: 212: 213: 214: 215: 216: 217: 218: 219:
220: public function renameFile($oldFilename, $oldPath, $newFilename, $newPath)
221: {
222: $adapter = $this->_getWriteAdapter();
223: $dataUpdate = array('filename' => $newFilename, 'directory' => $newPath);
224:
225: $dataWhere = array('filename = ?' => $oldFilename);
226: $dataWhere[] = new Zend_Db_Expr($adapter->prepareSqlCondition('directory', array('seq' => $oldPath)));
227:
228: $adapter->update($this->getMainTable(), $dataUpdate, $dataWhere);
229:
230: return $this;
231: }
232:
233: 234: 235: 236: 237: 238: 239: 240: 241:
242: public function copyFile($oldFilename, $oldPath, $newFilename, $newPath)
243: {
244: $adapter = $this->_getReadAdapter();
245:
246: $select = $adapter->select()
247: ->from(array('e' => $this->getMainTable()))
248: ->where('filename = ?', $oldFilename)
249: ->where($adapter->prepareSqlCondition('directory', array('seq' => $oldPath)));
250:
251: $data = $adapter->fetchRow($select);
252: if (!$data) {
253: return $this;
254: }
255:
256: if (isset($data['file_id']) && isset($data['filename'])) {
257: unset($data['file_id']);
258: $data['filename'] = $newFilename;
259: $data['directory'] = $newPath;
260:
261: $writeAdapter = $this->_getWriteAdapter();
262: $writeAdapter->insertOnDuplicate($this->getMainTable(), $data, array('content', 'upload_time'));
263: }
264:
265: return $this;
266: }
267:
268: 269: 270: 271: 272: 273: 274:
275: public function fileExists($filename, $path)
276: {
277: $adapter = $this->_getReadAdapter();
278:
279: $select = $adapter->select()
280: ->from(array('e' => $this->getMainTable()))
281: ->where('filename = ?', $filename)
282: ->where($adapter->prepareSqlCondition('directory', array('seq' => $path)))
283: ->limit(1);
284:
285: $data = $adapter->fetchRow($select);
286: return (bool)$data;
287: }
288:
289: 290: 291: 292: 293:
294: public function deleteFolder($folderName = '')
295: {
296: $folderName = rtrim($folderName, '/');
297: if (!strlen($folderName)) {
298: return;
299: }
300:
301:
302: $resHelper = Mage::getResourceHelper('core');
303: $likeExpression = $resHelper->addLikeEscape($folderName . '/', array('position' => 'start'));
304: $this->_getWriteAdapter()
305: ->delete($this->getMainTable(), new Zend_Db_Expr('filename LIKE ' . $likeExpression));
306: }
307:
308: 309: 310: 311: 312: 313:
314: public function deleteFile($filename, $directory)
315: {
316: $adapter = $this->_getWriteAdapter();
317:
318: $where = array('filename = ?' => $filename);
319: $where[] = new Zend_Db_Expr($adapter->prepareSqlCondition('directory', array('seq' => $directory)));
320:
321: $adapter->delete($this->getMainTable(), $where);
322: }
323:
324: 325: 326: 327: 328: 329:
330: public function getDirectoryFiles($directory)
331: {
332: $directory = trim($directory, '/');
333: $adapter = $this->_getReadAdapter();
334:
335: $select = $adapter->select()
336: ->from(
337: array('e' => $this->getMainTable()),
338: array(
339: 'filename',
340: 'directory',
341: 'content'
342: )
343: )
344: ->where($adapter->prepareSqlCondition('directory', array('seq' => $directory)))
345: ->order('file_id');
346:
347: $rows = $adapter->fetchAll($select);
348: return $this->_decodeAllFilesContent($rows);
349: }
350: }
351: