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 file storage model class
30: *
31: * @category Mage
32: * @package Mage_Core
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: abstract class Mage_Core_Model_File_Storage_Abstract extends Mage_Core_Model_Abstract
36: {
37: /**
38: * Store media base directory path
39: *
40: * @var string
41: */
42: protected $_mediaBaseDirectory = null;
43:
44: /**
45: * Retrieve media base directory path
46: *
47: * @return string
48: */
49: public function getMediaBaseDirectory()
50: {
51: if (null === $this->_mediaBaseDirectory) {
52: /** @var $helper Mage_Core_Helper_File_Storage_Database */
53: $helper = Mage::helper('core/file_storage_database');
54: $this->_mediaBaseDirectory = $helper->getMediaBaseDir();
55: }
56:
57: return $this->_mediaBaseDirectory;
58: }
59:
60: /**
61: * Collect file info
62: *
63: * Return array(
64: * filename => string
65: * content => string|bool
66: * update_time => string
67: * directory => string
68: * )
69: *
70: * @param string $path
71: * @return array
72: */
73: public function collectFileInfo($path)
74: {
75: $path = ltrim($path, '\\/');
76: $fullPath = $this->getMediaBaseDirectory() . DS . $path;
77:
78: if (!file_exists($fullPath) || !is_file($fullPath)) {
79: Mage::throwException(Mage::helper('core')->__('File %s does not exist', $fullPath));
80: }
81: if (!is_readable($fullPath)) {
82: Mage::throwException(Mage::helper('core')->__('File %s is not readable', $fullPath));
83: }
84:
85: $path = str_replace(array('/', '\\'), '/', $path);
86: $directory = dirname($path);
87: if ($directory == '.') {
88: $directory = null;
89: }
90:
91: return array(
92: 'filename' => basename($path),
93: 'content' => @file_get_contents($fullPath),
94: 'update_time' => Mage::getSingleton('core/date')->date(),
95: 'directory' => $directory
96: );
97: }
98: }
99: