00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Downloadable_Helper_File extends Mage_Core_Helper_Abstract
00035 {
00036
00037
00038
00039
00040
00041
00042
00043
00044 public function moveFileFromTmp($baseTmpPath, $basePath, $file)
00045 {
00046 if (isset($file[0])) {
00047 $fileName = $file[0]['file'];
00048 if ($file[0]['status'] == 'new') {
00049 try {
00050 $fileName = $this->_moveFileFromTmp(
00051 $baseTmpPath, $basePath, $file[0]['file']
00052 );
00053 } catch (Exception $e) {
00054 Mage::throwException(Mage::helper('downloadable')->__('An error occurred while saving the file(s).'));
00055 }
00056 }
00057 return $fileName;
00058 }
00059 return '';
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 protected function _moveFileFromTmp($baseTmpPath, $basePath, $file)
00071 {
00072 $ioObject = new Varien_Io_File();
00073 $destDirectory = dirname($this->getFilePath($basePath, $file));
00074 try {
00075 $ioObject->open(array('path'=>$destDirectory));
00076 } catch (Exception $e) {
00077 $ioObject->mkdir($destDirectory, 0777, true);
00078 $ioObject->open(array('path'=>$destDirectory));
00079 }
00080
00081 if (strrpos($file, '.tmp') == strlen($file)-4) {
00082 $file = substr($file, 0, strlen($file)-4);
00083 }
00084
00085 $destFile = dirname($file) . $ioObject->dirsep()
00086 . Varien_File_Uploader::getNewFileName($this->getFilePath($basePath, $file));
00087 $result = $ioObject->mv(
00088 $this->getFilePath($baseTmpPath, $file),
00089 $this->getFilePath($basePath, $destFile)
00090 );
00091 return str_replace($ioObject->dirsep(), '/', $destFile);
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101 public function getFilePath($path, $file)
00102 {
00103 $file = $this->_prepareFileForPath($file);
00104
00105 if(substr($file, 0, 1) == DS) {
00106 return $path . DS . substr($file, 1);
00107 }
00108
00109 return $path . DS . $file;
00110 }
00111
00112
00113
00114
00115
00116
00117
00118 protected function _prepareFileForPath($file)
00119 {
00120 return str_replace('/', DS, $file);
00121 }
00122
00123
00124
00125
00126
00127
00128
00129 public function getFileFromPathFile($pathFile)
00130 {
00131 $file = '';
00132
00133 $file = substr($pathFile, strrpos($this->_prepareFileForPath($pathFile), DS)+1);
00134
00135 return $file;
00136 }
00137
00138 public function getFileType($filePath)
00139 {
00140 $ext = substr($filePath, strrpos($filePath, '.')+1);
00141 return $this->_getFileTypeByExt($ext);
00142 }
00143
00144 protected function _getFileTypeByExt($ext)
00145 {
00146 $type = Mage::getConfig()->getNode('global/mime/types/x' . $ext);
00147 if ($type) {
00148 return $type;
00149 }
00150 return 'application/octet-stream';
00151 }
00152 }