PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Resource/File/Storage/Database.php

https://bitbucket.org/sevenly/magento-ce
PHP | 350 lines | 188 code | 42 blank | 120 comment | 6 complexity | 3f119cd4defe9b433537d9a35ee4cf7c MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * File storage database resource resource model class
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Core_Model_Resource_File_Storage_Database extends Mage_Core_Model_Resource_File_Storage_Abstract
  34. {
  35. /**
  36. * Define table name and id field for resource
  37. */
  38. protected function _construct()
  39. {
  40. $this->_init('core/file_storage', 'file_id');
  41. }
  42. /**
  43. * Create database scheme for storing files
  44. *
  45. * @return Mage_Core_Model_Resource_File_Storage_Database
  46. */
  47. public function createDatabaseScheme()
  48. {
  49. $adapter = $this->_getWriteAdapter();
  50. $table = $this->getMainTable();
  51. if ($adapter->isTableExists($table)) {
  52. return $this;
  53. }
  54. $dirStorageTable = $this->getTable('core/directory_storage'); // For foreign key
  55. $ddlTable = $adapter->newTable($table)
  56. ->addColumn('file_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
  57. 'identity' => true,
  58. 'unsigned' => true,
  59. 'nullable' => false,
  60. 'primary' => true
  61. ), 'File Id')
  62. ->addColumn('content', Varien_Db_Ddl_Table::TYPE_VARBINARY, Varien_Db_Ddl_Table::MAX_VARBINARY_SIZE, array(
  63. 'nullable' => false
  64. ), 'File Content')
  65. ->addColumn('upload_time', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
  66. 'nullable' => false,
  67. 'default' => Varien_Db_Ddl_Table::TIMESTAMP_INIT
  68. ), 'Upload Timestamp')
  69. ->addColumn('filename', Varien_Db_Ddl_Table::TYPE_TEXT, 100, array(
  70. 'nullable' => false
  71. ), 'Filename')
  72. ->addColumn('directory_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
  73. 'unsigned' => true,
  74. 'default' => null
  75. ), 'Identifier of Directory where File is Located')
  76. ->addColumn('directory', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
  77. 'default' => null
  78. ), 'Directory Path')
  79. ->addIndex($adapter->getIndexName($table, array('filename', 'directory_id'),
  80. Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE),
  81. array('filename', 'directory_id'), array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE))
  82. ->addIndex($adapter->getIndexName($table, array('directory_id')), array('directory_id'))
  83. ->addForeignKey($adapter->getForeignKeyName($table, 'directory_id', $dirStorageTable, 'directory_id'),
  84. 'directory_id', $dirStorageTable, 'directory_id',
  85. Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
  86. ->setComment('File Storage');
  87. $adapter->createTable($ddlTable);
  88. return $this;
  89. }
  90. /**
  91. * Decodes blob content retrieved by DB driver
  92. *
  93. * @param array $row Table row with 'content' key in it
  94. * @return array
  95. */
  96. protected function _decodeFileContent($row)
  97. {
  98. $row['content'] = $this->_getReadAdapter()->decodeVarbinary($row['content']);
  99. return $row;
  100. }
  101. /**
  102. * Decodes blob content retrieved by Database driver
  103. *
  104. * @param array $rows Array of table rows (files), each containing 'content' key
  105. * @return array
  106. */
  107. protected function _decodeAllFilesContent($rows)
  108. {
  109. foreach ($rows as $key => $row) {
  110. $rows[$key] = $this->_decodeFileContent($row);
  111. }
  112. return $rows;
  113. }
  114. /**
  115. * Load entity by filename
  116. *
  117. * @param Mage_Core_Model_File_Storage_Database $object
  118. * @param string $filename
  119. * @param string $path
  120. * @return Mage_Core_Model_Mysql4_File_Storage_Database
  121. */
  122. public function loadByFilename(Mage_Core_Model_File_Storage_Database $object, $filename, $path)
  123. {
  124. $adapter = $this->_getReadAdapter();
  125. $select = $adapter->select()
  126. ->from(array('e' => $this->getMainTable()))
  127. ->where('filename = ?', $filename)
  128. ->where($adapter->prepareSqlCondition('directory', array('seq' => $path)));
  129. $row = $adapter->fetchRow($select);
  130. if ($row) {
  131. $row = $this->_decodeFileContent($row);
  132. $object->setData($row);
  133. $this->_afterLoad($object);
  134. }
  135. return $this;
  136. }
  137. /**
  138. * Clear files in storage
  139. *
  140. * @return Mage_Core_Model_Mysql4_File_Storage_Database
  141. */
  142. public function clearFiles()
  143. {
  144. $adapter = $this->_getWriteAdapter();
  145. $adapter->delete($this->getMainTable());
  146. return $this;
  147. }
  148. /**
  149. * Get files from storage at defined range
  150. *
  151. * @param int $offset
  152. * @param int $count
  153. * @return array
  154. */
  155. public function getFiles($offset = 0, $count = 100)
  156. {
  157. $adapter = $this->_getReadAdapter();
  158. $select = $adapter->select()
  159. ->from(
  160. array('e' => $this->getMainTable()),
  161. array('filename', 'content', 'directory')
  162. )
  163. ->order('file_id')
  164. ->limit($count, $offset);
  165. $rows = $adapter->fetchAll($select);
  166. return $this->_decodeAllFilesContent($rows);
  167. }
  168. /**
  169. * Save file to storage
  170. *
  171. * @param Mage_Core_Model_File_Storage_Database|array $object
  172. * @return Mage_Core_Model_Mysql4_File_Storage_Database
  173. */
  174. public function saveFile($file)
  175. {
  176. $adapter = $this->_getWriteAdapter();
  177. $contentParam = new Varien_Db_Statement_Parameter($file['content']);
  178. $contentParam->setIsBlob(true);
  179. $data = array(
  180. 'content' => $contentParam,
  181. 'upload_time' => $file['update_time'],
  182. 'filename' => $file['filename'],
  183. 'directory_id' => $file['directory_id'],
  184. 'directory' => $file['directory']
  185. );
  186. $adapter->insertOnDuplicate($this->getMainTable(), $data, array('content', 'upload_time'));
  187. return $this;
  188. }
  189. /**
  190. * Rename files in database
  191. *
  192. * @param string $oldFilename
  193. * @param string $oldPath
  194. * @param string $newFilename
  195. * @param string $newPath
  196. * @return Mage_Core_Model_Mysql4_File_Storage_Database
  197. */
  198. public function renameFile($oldFilename, $oldPath, $newFilename, $newPath)
  199. {
  200. $adapter = $this->_getWriteAdapter();
  201. $dataUpdate = array('filename' => $newFilename, 'directory' => $newPath);
  202. $dataWhere = array('filename = ?' => $oldFilename);
  203. $dataWhere[] = new Zend_Db_Expr($adapter->prepareSqlCondition('directory', array('seq' => $oldPath)));
  204. $adapter->update($this->getMainTable(), $dataUpdate, $dataWhere);
  205. return $this;
  206. }
  207. /**
  208. * Copy files in database
  209. *
  210. * @param string $oldFilename
  211. * @param string $oldPath
  212. * @param string $newFilename
  213. * @param string $newPath
  214. * @return Mage_Core_Model_Mysql4_File_Storage_Database
  215. */
  216. public function copyFile($oldFilename, $oldPath, $newFilename, $newPath)
  217. {
  218. $adapter = $this->_getReadAdapter();
  219. $select = $adapter->select()
  220. ->from(array('e' => $this->getMainTable()))
  221. ->where('filename = ?', $oldFilename)
  222. ->where($adapter->prepareSqlCondition('directory', array('seq' => $oldPath)));
  223. $data = $adapter->fetchRow($select);
  224. if (!$data) {
  225. return $this;
  226. }
  227. if (isset($data['file_id']) && isset($data['filename'])) {
  228. unset($data['file_id']);
  229. $data['filename'] = $newFilename;
  230. $data['directory'] = $newPath;
  231. $writeAdapter = $this->_getWriteAdapter();
  232. $writeAdapter->insertOnDuplicate($this->getMainTable(), $data, array('content', 'upload_time'));
  233. }
  234. return $this;
  235. }
  236. /**
  237. * Check whether file exists in DB
  238. *
  239. * @param string $filename
  240. * @param string $path
  241. * @return bool
  242. */
  243. public function fileExists($filename, $path)
  244. {
  245. $adapter = $this->_getReadAdapter();
  246. $select = $adapter->select()
  247. ->from(array('e' => $this->getMainTable()))
  248. ->where('filename = ?', $filename)
  249. ->where($adapter->prepareSqlCondition('directory', array('seq' => $path)))
  250. ->limit(1);
  251. $data = $adapter->fetchRow($select);
  252. return (bool)$data;
  253. }
  254. /**
  255. * Delete files that starts with given $folderName
  256. *
  257. * @param string $folderName
  258. */
  259. public function deleteFolder($folderName = '')
  260. {
  261. $folderName = rtrim($folderName, '/');
  262. if (!strlen($folderName)) {
  263. return;
  264. }
  265. /* @var $resHelper Mage_Core_Model_Resource_Helper_Abstract */
  266. $resHelper = Mage::getResourceHelper('core');
  267. $likeExpression = $resHelper->addLikeEscape($folderName . '/', array('position' => 'start'));
  268. $this->_getWriteAdapter()
  269. ->delete($this->getMainTable(), new Zend_Db_Expr('filename LIKE ' . $likeExpression));
  270. }
  271. /**
  272. * Delete file
  273. *
  274. * @param string $filename
  275. * @param string $directory
  276. */
  277. public function deleteFile($filename, $directory)
  278. {
  279. $adapter = $this->_getWriteAdapter();
  280. $where = array('filename = ?' => $filename);
  281. $where[] = new Zend_Db_Expr($adapter->prepareSqlCondition('directory', array('seq' => $directory)));
  282. $adapter->delete($this->getMainTable(), $where);
  283. }
  284. /**
  285. * Return directory file listing
  286. *
  287. * @param string $directory
  288. * @return mixed
  289. */
  290. public function getDirectoryFiles($directory)
  291. {
  292. $directory = trim($directory, '/');
  293. $adapter = $this->_getReadAdapter();
  294. $select = $adapter->select()
  295. ->from(
  296. array('e' => $this->getMainTable()),
  297. array(
  298. 'filename',
  299. 'directory',
  300. 'content'
  301. )
  302. )
  303. ->where($adapter->prepareSqlCondition('directory', array('seq' => $directory)))
  304. ->order('file_id');
  305. $rows = $adapter->fetchAll($select);
  306. return $this->_decodeAllFilesContent($rows);
  307. }
  308. }