PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Helper/File/Storage/Database.php

https://bitbucket.org/kdms/sh-magento
PHP | 307 lines | 150 code | 21 blank | 136 comment | 17 complexity | 4aca07a674d2443681d90b4c947baf49 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  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://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Database saving file helper
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Core_Helper_File_Storage_Database extends Mage_Core_Helper_Abstract
  34. {
  35. /**
  36. * Database storage model
  37. * @var null|Mage_Core_Model_File_Storage_Database
  38. */
  39. protected $_databaseModel = null;
  40. /**
  41. * Storage resource model
  42. * @var null|Mage_Core_Model_Mysql4_File_Storage_Database
  43. */
  44. protected $_resourceModel = null;
  45. /**
  46. * Db usage flag
  47. *
  48. * @var bool
  49. */
  50. protected $_useDb = null;
  51. /**
  52. * Media dir
  53. *
  54. * @var string
  55. */
  56. protected $_mediaBaseDirectory;
  57. /**
  58. * Check if we use DB storage
  59. * Note: Disabled as not completed feature
  60. *
  61. * @return bool
  62. */
  63. public function checkDbUsage()
  64. {
  65. if (null === $this->_useDb) {
  66. $currentStorage = (int) Mage::app()->getConfig()
  67. ->getNode(Mage_Core_Model_File_Storage::XML_PATH_STORAGE_MEDIA);
  68. $this->_useDb = ($currentStorage == Mage_Core_Model_File_Storage::STORAGE_MEDIA_DATABASE);
  69. }
  70. return $this->_useDb;
  71. }
  72. /**
  73. * Get database storage model
  74. *
  75. * @return Mage_Core_Model_File_Storage_Database
  76. */
  77. public function getStorageDatabaseModel()
  78. {
  79. if (is_null($this->_databaseModel)) {
  80. $this->_databaseModel = Mage::getModel('core/file_storage_database');
  81. }
  82. return $this->_databaseModel;
  83. }
  84. /**
  85. * Get file storage model
  86. *
  87. * @return Mage_Core_Model_File_Storage_File
  88. */
  89. public function getStorageFileModel()
  90. {
  91. return Mage::getSingleton('core/file_storage_file');
  92. }
  93. /**
  94. * Get storage model
  95. *
  96. * @return Mage_Core_Model_Mysql4_File_Storage_Database
  97. */
  98. public function getResourceStorageModel()
  99. {
  100. if (is_null($this->_resourceModel)) {
  101. $this->_resourceModel = $this->getStorageDatabaseModel()->getResource();
  102. }
  103. return $this->_resourceModel;
  104. }
  105. /**
  106. * Save file in DB storage
  107. *
  108. * @param string $filename
  109. */
  110. public function saveFile($filename)
  111. {
  112. if ($this->checkDbUsage()) {
  113. $this->getStorageDatabaseModel()->saveFile($this->_removeAbsPathFromFileName($filename));
  114. }
  115. }
  116. /**
  117. * Rename file in DB storage
  118. *
  119. * @param string $oldName
  120. * @param string $newName
  121. */
  122. public function renameFile($oldName, $newName)
  123. {
  124. if ($this->checkDbUsage()) {
  125. $this->getStorageDatabaseModel()
  126. ->renameFile($this->_removeAbsPathFromFileName($oldName), $this->_removeAbsPathFromFileName($newName));
  127. }
  128. }
  129. /**
  130. * Copy file in DB storage
  131. *
  132. * @param string $oldName
  133. * @param string $newName
  134. */
  135. public function copyFile($oldName, $newName) {
  136. if ($this->checkDbUsage()) {
  137. $this->getStorageDatabaseModel()
  138. ->copyFile($this->_removeAbsPathFromFileName($oldName), $this->_removeAbsPathFromFileName($newName));
  139. }
  140. }
  141. /**
  142. * Check whether file exists in DB
  143. *
  144. * @param string $filename can be both full path or partial (like in DB)
  145. * @return bool|null
  146. */
  147. public function fileExists($filename)
  148. {
  149. if ($this->checkDbUsage()) {
  150. return $this->getStorageDatabaseModel()->fileExists($this->_removeAbsPathFromFileName($filename));
  151. } else {
  152. return null;
  153. }
  154. }
  155. /**
  156. * Get unique name for passed file in case this file already exists
  157. *
  158. * @param string $directory - can be both full path or partial (like in DB)
  159. * @param string $filename - not just a filename. Can have directory chunks. return will have this form
  160. * @return string
  161. */
  162. public function getUniqueFilename($directory, $filename)
  163. {
  164. if ($this->checkDbUsage()) {
  165. $directory = $this->_removeAbsPathFromFileName($directory);
  166. if($this->fileExists($directory . $filename)) {
  167. $index = 1;
  168. $extension = strrchr($filename, '.');
  169. $filenameWoExtension = substr($filename, 0, -1 * strlen($extension));
  170. while ($this->fileExists($directory . $filenameWoExtension . '_' . $index . $extension)) {
  171. $index ++;
  172. }
  173. $filename = $filenameWoExtension . '_' . $index . $extension;
  174. }
  175. }
  176. return $filename;
  177. }
  178. /**
  179. * Save database file to file system
  180. *
  181. * @param string $filename
  182. * @return bool|int
  183. */
  184. public function saveFileToFilesystem($filename) {
  185. if ($this->checkDbUsage()) {
  186. /** @var $file Mage_Core_Model_File_Storage_Database */
  187. $file = Mage::getModel('core/file_storage_database')
  188. ->loadByFilename($this->_removeAbsPathFromFileName($filename));
  189. if (!$file->getId()) {
  190. return false;
  191. }
  192. return $this->getStorageFileModel()->saveFile($file, true);
  193. }
  194. }
  195. /**
  196. * Return relative uri for media content by full path
  197. *
  198. * @param string $fullPath
  199. * @return string
  200. */
  201. public function getMediaRelativePath($fullPath)
  202. {
  203. $relativePath = ltrim(str_replace($this->getMediaBaseDir(), '', $fullPath), '\\/');
  204. return str_replace(DS, '/', $relativePath);
  205. }
  206. /**
  207. * Deletes from DB files, which belong to one folder
  208. *
  209. * @param string $folderName
  210. */
  211. public function deleteFolder($folderName)
  212. {
  213. if ($this->checkDbUsage()) {
  214. $this->getResourceStorageModel()->deleteFolder($this->_removeAbsPathFromFileName($folderName));
  215. }
  216. }
  217. /**
  218. * Deletes from DB files, which belong to one folder
  219. *
  220. * @param string $filename
  221. */
  222. public function deleteFile($filename)
  223. {
  224. if ($this->checkDbUsage()) {
  225. $this->getStorageDatabaseModel()->deleteFile($this->_removeAbsPathFromFileName($filename));
  226. }
  227. }
  228. /**
  229. * Saves uploaded by Mage_Core_Model_File_Uploader file to DB with existence tests
  230. *
  231. * param $result should be result from Mage_Core_Model_File_Uploader::save() method
  232. * Checks in DB, whether uploaded file exists ($result['file'])
  233. * If yes, renames file on FS (!!!!!)
  234. * Saves file with unique name into DB
  235. * If passed file exists returns new name, file was renamed to (in the same context)
  236. * Otherwise returns $result['file']
  237. *
  238. * @param array $result
  239. * @return string
  240. */
  241. public function saveUploadedFile($result = array())
  242. {
  243. if ($this->checkDbUsage()) {
  244. $path = rtrim(str_replace(array('\\', '/'), DS, $result['path']), DS);
  245. $file = '/' . ltrim($result['file'], '\\/');
  246. $uniqueResultFile = $this->getUniqueFilename($path, $file);
  247. if ($uniqueResultFile !== $file) {
  248. $ioFile = new Varien_Io_File();
  249. $ioFile->open(array('path' => $path));
  250. $ioFile->mv($path . $file, $path . $uniqueResultFile);
  251. }
  252. $this->saveFile($path . $uniqueResultFile);
  253. return $uniqueResultFile;
  254. } else {
  255. return $result['file'];
  256. }
  257. }
  258. /**
  259. * Convert full file path to local (as used by model)
  260. * If not - returns just a filename
  261. *
  262. * @param string $filename
  263. * @return string
  264. */
  265. protected function _removeAbsPathFromFileName($filename)
  266. {
  267. return $this->getMediaRelativePath($filename);
  268. }
  269. /**
  270. * Return Media base dir
  271. *
  272. * @return string
  273. */
  274. public function getMediaBaseDir()
  275. {
  276. if (null === $this->_mediaBaseDirectory) {
  277. $this->_mediaBaseDirectory = rtrim(Mage::getBaseDir('media'), '\\/');
  278. }
  279. return $this->_mediaBaseDirectory;
  280. }
  281. }