-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
File.php
210 lines (187 loc) · 5.25 KB
/
File.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* OpenMage
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available at https://opensource.org/license/osl-3-0-php
*
* @category Mage
* @package Mage_Downloadable
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
* @copyright Copyright (c) 2020-2023 The OpenMage Contributors (https://www.openmage.org)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Downloadable Products File Helper
*
* @category Mage
* @package Mage_Downloadable
*/
class Mage_Downloadable_Helper_File extends Mage_Core_Helper_Abstract
{
protected $_moduleName = 'Mage_Downloadable';
/**
* @see Mage_Uploader_Helper_File::getMimeTypes
* @var array
*/
protected $_mimeTypes;
/**
* @var Mage_Uploader_Helper_File
*/
protected $_fileHelper;
/**
* Populate self::_mimeTypes array with values that set in config or pre-defined
*/
public function __construct()
{
$this->_mimeTypes = $this->_getFileHelper()->getMimeTypes();
}
/**
* @return Mage_Uploader_Helper_File
*/
protected function _getFileHelper()
{
if (!$this->_fileHelper) {
$this->_fileHelper = Mage::helper('uploader/file');
}
return $this->_fileHelper;
}
/**
* Checking file for moving and move it
*
* @param string $baseTmpPath
* @param string $basePath
* @param array $file
* @return string
*/
public function moveFileFromTmp($baseTmpPath, $basePath, $file)
{
if (isset($file[0])) {
$fileName = $file[0]['file'];
if ($file[0]['status'] == 'new') {
try {
$fileName = $this->_moveFileFromTmp(
$baseTmpPath,
$basePath,
$file[0]['file']
);
} catch (Exception $e) {
Mage::throwException(Mage::helper('downloadable')->__('An error occurred while saving the file(s).'));
}
}
return $fileName;
}
return '';
}
/**
* Move file from tmp path to base path
*
* @param string $baseTmpPath
* @param string $basePath
* @param string $file
* @return string
*/
protected function _moveFileFromTmp($baseTmpPath, $basePath, $file)
{
$ioObject = new Varien_Io_File();
$destDirectory = dirname($this->getFilePath($basePath, $file));
try {
$ioObject->open(['path' => $destDirectory]);
} catch (Exception $e) {
$ioObject->mkdir($destDirectory, 0777, true);
$ioObject->open(['path' => $destDirectory]);
}
if (strrpos($file, '.tmp') == strlen($file) - 4) {
$file = substr($file, 0, -4);
}
$destFile = dirname($file) . $ioObject->dirsep()
. Mage_Core_Model_File_Uploader::getNewFileName($this->getFilePath($basePath, $file));
Mage::helper('core/file_storage_database')->copyFile(
$this->getFilePath($baseTmpPath, $file),
$this->getFilePath($basePath, $destFile)
);
$result = $ioObject->mv(
$this->getFilePath($baseTmpPath, $file),
$this->getFilePath($basePath, $destFile)
);
return str_replace($ioObject->dirsep(), '/', $destFile);
}
/**
* Return full path to file
*
* @param string $path
* @param string $file
* @return string
*/
public function getFilePath($path, $file)
{
$file = $this->_prepareFileForPath($file);
if (substr($file, 0, 1) == DS) {
return $path . DS . substr($file, 1);
}
return $path . DS . $file;
}
/**
* Replace slashes with directory separator
*
* @param string $file
* @return string
*/
protected function _prepareFileForPath($file)
{
return str_replace('/', DS, $file);
}
/**
* Return file name form file path
*
* @param string $pathFile
* @return string
*/
public function getFileFromPathFile($pathFile)
{
$file = '';
$file = substr($pathFile, strrpos($this->_prepareFileForPath($pathFile), DS) + 1);
return $file;
}
/**
* Get MIME type for $filePath
*
* @param string $filePath
* @return string
*/
public function getFileType($filePath)
{
$ext = substr($filePath, strrpos($filePath, '.') + 1);
return $this->_getFileTypeByExt($ext);
}
/**
* Get MIME type by file extension
*
* @param string $ext
* @return string
* @deprecated
*/
protected function _getFileTypeByExt($ext)
{
return $this->_getFileHelper()->getMimeTypeByExtension($ext);
}
/**
* Get all MIME types
*
* @return array
*/
public function getAllFileTypes()
{
return array_values($this->getAllMineTypes());
}
/**
* Get list of all MIME types
*
* @return array
*/
public function getAllMineTypes()
{
return $this->_mimeTypes;
}
}