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: * File storage helper
29: *
30: * @category Mage
31: * @package Mage_Core
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Core_Helper_File_Storage extends Mage_Core_Helper_Abstract
35: {
36: /**
37: * Current storage code
38: *
39: * @var int
40: */
41: protected $_currentStorage = null;
42:
43: /**
44: * List of internal storages
45: *
46: * @var array
47: */
48: protected $_internalStorageList = array(
49: Mage_Core_Model_File_Storage::STORAGE_MEDIA_FILE_SYSTEM
50: );
51:
52: /**
53: * Return saved storage code
54: *
55: * @return int
56: */
57: public function getCurrentStorageCode()
58: {
59: if (is_null($this->_currentStorage)) {
60: $this->_currentStorage = (int) Mage::app()
61: ->getConfig()->getNode(Mage_Core_Model_File_Storage::XML_PATH_STORAGE_MEDIA);
62: }
63:
64: return $this->_currentStorage;
65: }
66:
67: /**
68: * Retrieve file system storage model
69: *
70: * @return Mage_Core_Model_File_Storage_File
71: */
72: public function getStorageFileModel()
73: {
74: return Mage::getSingleton('core/file_storage_file');
75: }
76:
77: /**
78: * Check if storage is internal
79: *
80: * @param int|null $storage
81: * @return bool
82: */
83: public function isInternalStorage($storage = null)
84: {
85: $storage = (!is_null($storage)) ? (int) $storage : $this->getCurrentStorageCode();
86:
87: return in_array($storage, $this->_internalStorageList);
88: }
89:
90: /**
91: * Retrieve storage model
92: *
93: * @param int|null $storage
94: * @param array $params
95: * @return Mage_Core_Model_Abstract|bool
96: */
97: public function getStorageModel($storage = null, $params = array())
98: {
99: return Mage::getSingleton('core/file_storage')->getStorageModel($storage, $params);
100: }
101:
102: /**
103: * Check if needed to copy file from storage to file system and
104: * if file exists in the storage
105: *
106: * @param string $filename
107: * @return bool|int
108: */
109: public function processStorageFile($filename)
110: {
111: if ($this->isInternalStorage()) {
112: return false;
113: }
114:
115: $dbHelper = Mage::helper('core/file_storage_database');
116:
117: $relativePath = $dbHelper->getMediaRelativePath($filename);
118: $file = $this->getStorageModel()->loadByFilename($relativePath);
119:
120: if (!$file->getId()) {
121: return false;
122: }
123:
124: return $this->saveFileToFileSystem($file);
125: }
126:
127: /**
128: * Save file to file system
129: *
130: * @param Mage_Core_Model_File_Storage_Database $file
131: * @return bool|int
132: */
133: public function saveFileToFileSystem($file)
134: {
135: return $this->getStorageFileModel()->saveFile($file, true);
136: }
137: }
138: