/code/S3/Model/File/Storage/S3.php

https://github.com/esimionato/magneto-s3 · PHP · 346 lines · 143 code · 59 blank · 144 comment · 7 complexity · e231847e84a25b934ced2d3aaeacd169 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) 2010 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 model class
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Magneto_S3_Model_File_Storage_S3 extends Magneto_S3_Model_File_Storage_S3_Abstract
  34. {
  35. /**
  36. * Prefix of model events names
  37. *
  38. * @var string
  39. */
  40. protected $_eventPrefix = 'core_file_storage_database';
  41. /**
  42. * Collect errors during sync process
  43. *
  44. * @var array
  45. */
  46. protected $_errors = array();
  47. /**
  48. * Class construct
  49. *
  50. * @param string $connectionName
  51. */
  52. public function __construct($connectionName = null)
  53. {
  54. Mage::log(__METHOD__);
  55. $this->_init('core/file_storage_database');
  56. parent::__construct($connectionName);
  57. }
  58. /**
  59. * Create tables for file and directory storages
  60. *
  61. * @return Mage_Core_Model_File_Storage_Database
  62. */
  63. public function init()
  64. {
  65. Mage::log(__METHOD__);
  66. return $this;
  67. }
  68. /**
  69. * Return storage name
  70. *
  71. * @return string
  72. */
  73. public function getStorageName()
  74. {
  75. Mage::log(__METHOD__);
  76. //TODO: replace dummy
  77. return Mage::helper('core')->__('S3 bucket "%s"', $this->getConnectionName());
  78. //return Mage::helper('core')->__('database "%s"', $this->getConnectionName());
  79. }
  80. /**
  81. * Load object data by filename
  82. *
  83. * @param string $filePath
  84. * @return Mage_Core_Model_File_Storage_Database
  85. */
  86. public function loadByFilename($filePath)
  87. {
  88. $filePath = str_replace(array('//', '\\'), '/', $filePath);
  89. Mage::log(__METHOD__.' '.$filePath);
  90. $_content = $this->_s3->getObject($this->_bucket . DS . $filePath);
  91. $this->setFilename(basename($filePath));
  92. $this->setDirectory(dirname($filePath));
  93. $this->setContent( $_content );
  94. $this->setId($_content);
  95. return $this;
  96. }
  97. /**
  98. * Check if there was errors during sync process
  99. *
  100. * @return bool
  101. */
  102. public function hasErrors()
  103. {
  104. Mage::log(__METHOD__);
  105. return (!empty($this->_errors));
  106. }
  107. /**
  108. * Clear files and directories in storage
  109. *
  110. * @return Mage_Core_Model_File_Storage_Database
  111. */
  112. public function clear()
  113. {
  114. Mage::log(__METHOD__);
  115. // $this->getDirectoryModel()->clearDirectories();
  116. // $this->_getResource()->clearFiles();
  117. $this->_s3->cleanBucket($this->_bucket);
  118. return $this;
  119. }
  120. /**
  121. * Export directories from storage
  122. *
  123. * @param int $offset
  124. * @param int $count
  125. * @return bool|array
  126. */
  127. public function exportDirectories($offset = 0, $count = 100)
  128. {
  129. Mage::log(__METHOD__);
  130. //return $this->getDirectoryModel()->exportDirectories($offset, $count);
  131. // TODO: write me
  132. return false;
  133. }
  134. /**
  135. * Import directories to storage
  136. *
  137. * @param array $dirs
  138. * @return Mage_Core_Model_File_Storage_Directory_Database
  139. */
  140. public function importDirectories($dirs)
  141. {
  142. Mage::log(__METHOD__);
  143. //no need to do anything here, since s3 doesn't know about directories
  144. return $this;
  145. }
  146. /**
  147. * Export files list in defined range
  148. *
  149. * @param int $offset
  150. * @param int $count
  151. * @return array|bool
  152. */
  153. public function exportFiles($offset = 0, $count = 100)
  154. {
  155. Mage::log(__METHOD__);
  156. //TODO: write me
  157. return false;
  158. $offset = ((int) $offset >= 0) ? (int) $offset : 0;
  159. $count = ((int) $count >= 1) ? (int) $count : 1;
  160. $result = $this->_getResource()->getFiles($offset, $count);
  161. if (empty($result)) {
  162. return false;
  163. }
  164. return $result;
  165. }
  166. /**
  167. * Import files list
  168. *
  169. * @param array $files
  170. * @return Mage_Core_Model_File_Storage_Database
  171. */
  172. public function importFiles($files)
  173. {
  174. Mage::log(__METHOD__);
  175. if (!is_array($files)) {
  176. return $this;
  177. }
  178. foreach ($files as $file) {
  179. if (!isset($file['filename']) || !strlen($file['filename']) || !isset($file['content'])) {
  180. continue;
  181. }
  182. try {
  183. $this->saveFile($file['directory'].DS.$file['filename']);
  184. } catch (Exception $e) {
  185. $this->_errors[] = $e->getMessage();
  186. Mage::logException($e);
  187. }
  188. }
  189. return $this;
  190. }
  191. /**
  192. * Store file into database
  193. *
  194. * @param string $filename
  195. * @return Mage_Core_Model_File_Storage_Database
  196. */
  197. public function saveFile($filename)
  198. {
  199. $filename = str_replace(array('//', '\\'), '/', $filename);
  200. Mage::log(__METHOD__.' '.$filename);
  201. $fileInfo = $this->collectFileInfo($filename);
  202. $filePath = $fileInfo['directory'];
  203. $this->_s3->putObject($this->_bucket . DS . $fileInfo['directory'] . DS . $fileInfo['filename'], $fileInfo['content']);
  204. return $this;
  205. }
  206. /**
  207. * Check whether file exists in DB
  208. *
  209. * @param string $filePath
  210. * @return bool
  211. */
  212. public function fileExists($filePath)
  213. {
  214. Mage::log(__METHOD__.' '.$filePath);
  215. return $this->_s3->isObjectAvailable($this->_bucket.DS.$filePath);
  216. }
  217. /**
  218. * Copy files
  219. *
  220. * @param string $oldFilePath
  221. * @param string $newFilePath
  222. * @return Mage_Core_Model_File_Storage_Database
  223. */
  224. public function copyFile($oldFilePath, $newFilePath)
  225. {
  226. Mage::log(__METHOD__);
  227. $_content = $this->_s3->getObject($this->_bucket.DS.$oldFilePath);
  228. $this->_s3->putObject($this->_bucket.DS.$newFilePath, $_content);
  229. return $this;
  230. }
  231. /**
  232. * Rename files in database
  233. *
  234. * @param string $oldFilePath
  235. * @param string $newFilePath
  236. * @return Mage_Core_Model_File_Storage_Database
  237. */
  238. public function renameFile($oldFilePath, $newFilePath)
  239. {
  240. Mage::log(__METHOD__);
  241. $_content = $this->_s3->getObject($this->_bucket.DS.$oldFilePath);
  242. $this->_s3->putObject($this->_bucket.DS.$newFilePath, $_content);
  243. $this->_s3->removeObject($this->_bucket.DS.$oldFilePath);
  244. return $this;
  245. }
  246. /**
  247. * Return directory listing
  248. *
  249. * @param string $directory
  250. * @return mixed
  251. */
  252. public function getDirectoryFiles($directory)
  253. {
  254. $directory = str_replace(array('//', '\\'), '/', $directory);
  255. Mage::log(__METHOD__ . ' ' . $directory);
  256. $directory = Mage::helper('core/file_storage_database')->getMediaRelativePath($directory);
  257. Mage::log(__METHOD__ . ' ' . $directory);
  258. $files = $this->_s3->getObjectsByBucket($this->_bucket, array('prefix'=>$directory));
  259. $return = array();
  260. foreach($files as $file)
  261. {
  262. if(substr($file, -1, 1) == '/') continue;
  263. $return[] = array(
  264. 'filename' => basename($file),
  265. 'directory'=> dirname($file),
  266. 'content' => $this->_s3->getObject($this->_bucket.DS.$file)
  267. );
  268. }
  269. return $return;
  270. }
  271. /**
  272. * Delete file from database
  273. *
  274. * @param string $path
  275. * @return Mage_Core_Model_File_Storage_Database
  276. */
  277. public function deleteFile($path)
  278. {
  279. Mage::log(__METHOD__ . ' ' . $path);
  280. $this->_s3->removeObject($this->_bucket.DS.$path);
  281. return $this;
  282. }
  283. public function deleteFolder($path)
  284. {
  285. Mage::getModel('core/file_storage_directory_database')->deleteDirectory($path);
  286. }
  287. }