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:
28: /**
29: * Abstract model class
30: *
31: * @category Mage
32: * @package Mage_Core
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Core_Model_File_Storage_File extends Mage_Core_Model_File_Storage_Abstract
36: {
37: /**
38: * Prefix of model events names
39: *
40: * @var string
41: */
42: protected $_eventPrefix = 'core_file_storage_file';
43:
44: /**
45: * Data at storage
46: *
47: * @var array
48: */
49: protected $_data = null;
50:
51: /**
52: * Collect errors during sync process
53: *
54: * @var array
55: */
56: protected $_errors = array();
57:
58: /**
59: * Class construct
60: */
61: public function __construct()
62: {
63: $this->_init('core/file_storage_file');
64: }
65:
66: /**
67: * Initialization
68: *
69: * @return Mage_Core_Model_File_Storage_File
70: */
71: public function init()
72: {
73: return $this;
74: }
75:
76: /**
77: * Return storage name
78: *
79: * @return string
80: */
81: public function getStorageName()
82: {
83: return Mage::helper('core')->__('File system');
84: }
85:
86: /**
87: * Get files and directories from storage
88: *
89: * @return array
90: */
91: public function getStorageData()
92: {
93: return $this->_getResource()->getStorageData();
94: }
95:
96: /**
97: * Check if there was errors during sync process
98: *
99: * @return bool
100: */
101: public function hasErrors()
102: {
103: return !empty($this->_errors);
104: }
105:
106: /**
107: * Clear files and directories in storage
108: *
109: * @return Mage_Core_Model_File_Storage_File
110: */
111: public function clear()
112: {
113: $this->_getResource()->clear();
114: return $this;
115: }
116:
117: /**
118: * Collect files and directories from storage
119: *
120: * @param int $offset
121: * @param int $count
122: * @param string $type
123: * @return array|bool
124: */
125: public function collectData($offset = 0, $count = 100, $type = 'files')
126: {
127: if (!in_array($type, array('files', 'directories'))) {
128: return false;
129: }
130:
131: $offset = ((int) $offset >= 0) ? (int) $offset : 0;
132: $count = ((int) $count >= 1) ? (int) $count : 1;
133:
134: if (is_null($this->_data)) {
135: $this->_data = $this->getStorageData();
136: }
137:
138: $slice = array_slice($this->_data[$type], $offset, $count);
139: if (empty($slice)) {
140: return false;
141: }
142:
143: return $slice;
144: }
145:
146: /**
147: * Export directories list from storage
148: *
149: * @param int $offset
150: * @param int $count
151: * @return array|bool
152: */
153: public function exportDirectories($offset = 0, $count = 100)
154: {
155: return $this->collectData($offset, $count, 'directories');
156: }
157:
158: /**
159: * Export files list in defined range
160: *
161: * @param int $offset
162: * @param int $count
163: * @return array|bool
164: */
165: public function exportFiles($offset = 0, $count = 1)
166: {
167: $slice = $this->collectData($offset, $count, 'files');
168:
169: if (!$slice) {
170: return false;
171: }
172:
173: $result = array();
174: foreach ($slice as $fileName) {
175: try {
176: $fileInfo = $this->collectFileInfo($fileName);
177: } catch (Exception $e) {
178: Mage::logException($e);
179: continue;
180: }
181:
182: $result[] = $fileInfo;
183: }
184:
185: return $result;
186: }
187:
188: /**
189: * Import entities to storage
190: *
191: * @param array $data
192: * @param string $callback
193: * @return Mage_Core_Model_File_Storage_File
194: */
195: public function import($data, $callback)
196: {
197: if (!is_array($data) || !method_exists($this, $callback)) {
198: return $this;
199: }
200:
201: foreach ($data as $part) {
202: try {
203: $this->$callback($part);
204: } catch (Exception $e) {
205: $this->_errors[] = $e->getMessage();
206: Mage::logException($e);
207: }
208: }
209:
210: return $this;
211: }
212:
213: /**
214: * Import directories to storage
215: *
216: * @param array $dirs
217: * @return Mage_Core_Model_File_Storage_File
218: */
219: public function importDirectories($dirs)
220: {
221: return $this->import($dirs, 'saveDir');
222: }
223:
224: /**
225: * Import files list
226: *
227: * @param array $files
228: * @return Mage_Core_Model_File_Storage_File
229: */
230: public function importFiles($files)
231: {
232: return $this->import($files, 'saveFile');
233: }
234:
235: /**
236: * Save directory to storage
237: *
238: * @param array $dir
239: * @return bool
240: */
241: public function saveDir($dir)
242: {
243: return $this->_getResource()->saveDir($dir);
244: }
245:
246: /**
247: * Save file to storage
248: *
249: * @param array|Mage_Core_Model_File_Storage_Database $file
250: * @param bool $overwrite
251: * @return bool|int
252: */
253: public function saveFile($file, $overwrite = true)
254: {
255: if (isset($file['filename']) && !empty($file['filename'])
256: && isset($file['content']) && !empty($file['content'])
257: ) {
258: try {
259: $filename = (isset($file['directory']) && !empty($file['directory']))
260: ? $file['directory'] . DS . $file['filename']
261: : $file['filename'];
262:
263: return $this->_getResource()
264: ->saveFile($filename, $file['content'], $overwrite);
265: } catch (Exception $e) {
266: Mage::logException($e);
267: Mage::throwException(Mage::helper('core')->__('Unable to save file "%s" at "%s"', $file['filename'], $file['directory']));
268: }
269: } else {
270: Mage::throwException(Mage::helper('core')->__('Wrong file info format'));
271: }
272:
273: return false;
274: }
275: }
276: