/app/code/core/Mage/Downloadable/Helper/File.php

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