* adjust for save file not with extension.

This commit is contained in:
wangyidong
2017-06-26 14:47:58 +08:00
parent c8f4f4fc0f
commit 794fb575db
20 changed files with 118 additions and 99 deletions
+55 -39
View File
@@ -51,8 +51,9 @@ class fileModel extends model
->fetchAll('id');
foreach($files as $file)
{
$file->webPath = $this->webPath . $file->pathname;
$file->realPath = $this->app->getAppRoot() . "www/data/upload/{$this->app->company->id}/" . $file->pathname;
$realPathName = $this->getRealPathName($file->pathname);
$file->realPath = $this->savePath . $realPathName;
$file->webPath = $this->webPath . $realPathName;
}
return $files;
}
@@ -67,8 +68,10 @@ class fileModel extends model
public function getById($fileID)
{
$file = $this->dao->findById($fileID)->from(TABLE_FILE)->fetch();
$file->webPath = $this->webPath . $file->pathname;
$file->realPath = $this->savePath . $file->pathname;
$realPathName = $this->getRealPathName($file->pathname);
$file->realPath = $this->savePath . $realPathName;
$file->webPath = $this->webPath . $realPathName;
return $file;
}
@@ -92,7 +95,7 @@ class fileModel extends model
foreach($files as $id => $file)
{
if($file['size'] == 0) continue;
if(!move_uploaded_file($file['tmpname'], $this->savePath . $file['pathname'])) return false;
if(!move_uploaded_file($file['tmpname'], $this->savePath . $this->getSaveName($file['pathname']))) return false;
$file = $this->compressImage($file);
@@ -233,6 +236,7 @@ class fileModel extends model
if(!is_dir($tmpFileSavePath)) mkdir($tmpFileSavePath);
$fileName = basename($file['pathname']);
$fileName = strpos($fileName, '.') === false ? $fileName : substr($fileName, 0, strpos($fileName, '.'));
$file['realpath'] = $tmpFileSavePath . $fileName;
if($file['chunks'] > 1)
@@ -297,6 +301,34 @@ class fileModel extends model
return $extension;
}
/**
* Get save name.
*
* @param string $pathName
* @access public
* @return string
*/
public function getSaveName($pathName)
{
$saveName = strpos($pathName, '.') === false ? $pathName : substr($pathName, 0, strpos($pathName, '.'));
return $saveName;
}
/**
* Get real path name.
*
* @param string $pathName
* @access public
* @return string
*/
public function getRealPathName($pathName)
{
$realPath = $this->savePath . $pathName;
if(file_exists($realPath)) return $pathName;
return $this->getSaveName($pathName);
}
/**
* Get export tpl.
*
@@ -393,16 +425,7 @@ class fileModel extends model
$readLinkReg = str_replace(array('%fileID%', '/', '.', '?'), array('[0-9]+', '\/', '\.', '\?'), helper::createLink('file', 'read', 'fileID=(%fileID%)', '\w+'));
$content = preg_replace('/ src="(' . $readLinkReg . ')" /', ' onload="setImageSize(this,' . $maxSize . ')" src="$1" ', $content);
preg_match_all('/ src="{([0-9]+)}" /', $content, $matchs);
if(!empty($matchs[1]))
{
$files = $this->dao->select('id,extension')->from(TABLE_FILE)->where('id')->in($matchs[1])->fetchPairs('id', 'extension');
foreach($matchs[0] as $i => $match)
{
$fileID = $matchs[1][$i];
$content = str_replace($match, ' onload="setImageSize(this,' . $maxSize . ')" src="' . helper::createLink('file', 'read', "fileID=$fileID", $files[$fileID]) . '" ', $content);
}
}
$content = preg_replace('/ src="{([0-9]+)(\.(\w+))?}" /', ' onload="setImageSize(this,' . $maxSize . ')" src="' . helper::createLink('file', 'read', "fileID=$1", "$3") . '" ', $content);
return str_replace(' src="data/upload', ' onload="setImageSize(this,' . $maxSize . ')" src="data/upload', $content);
}
@@ -419,7 +442,7 @@ class fileModel extends model
$file = $files[0];
$filePath = $this->dao->select('pathname')->from(TABLE_FILE)->where('id')->eq($fileID)->fetch();
$pathName = $filePath->pathname;
$realPathName= $this->savePath . $pathName;
$realPathName = $this->savePath . $this->getRealPathName($pathName);
if(!is_dir(dirname($realPathName)))mkdir(dirname($realPathName));
move_uploaded_file($file['tmpname'], $realPathName);
@@ -467,7 +490,7 @@ class fileModel extends model
$file['addedDate'] = helper::today();
$file['title'] = basename($file['pathname']);
file_put_contents($this->savePath . $file['pathname'], $imageData);
file_put_contents($this->savePath . $this->getSaveName($file['pathname']), $imageData);
$this->dao->insert(TABLE_FILE)->data($file)->exec();
$fileID = $this->dao->lastInsertID();
if($uid) $_SESSION['album'][$uid][] = $fileID;
@@ -601,17 +624,20 @@ class fileModel extends model
* @access public
* @return object
*/
public function processEditor($data, $editorList, $uid = '')
public function processImgURL($data, $editorList, $uid = '')
{
if(is_string($editorList)) $editorList = explode(',', str_replace(' ', '', $editorList));
$readLinkReg = basename(helper::createLink('file', 'read', 'fileID=(%fileID%)', '\w+'));
$readLinkReg = basename(helper::createLink('file', 'read', 'fileID=(%fileID%)', '(\w+)'));
$readLinkReg = str_replace(array('%fileID%', '?'), array('[0-9]+', '\?'), $readLinkReg);
foreach($editorList as $editorID)
{
if(empty($editorID) or empty($data->$editorID)) continue;
$imgURL = $this->config->requestType == 'GET' ? '{$2.$1}' : '{$1.$2}';
$data->$editorID = $this->pasteImage($data->$editorID, $uid);
$data->$editorID = preg_replace("/ src=\"$readLinkReg\" /", ' src="{$1}" ', $data->$editorID);
$data->$editorID = preg_replace("/ src=\"" . htmlspecialchars($readLinkReg) . "\" /", ' src="{$1}" ', $data->$editorID);
$data->$editorID = preg_replace("/ src=\"$readLinkReg\" /", ' src="' . $imgURL . '" ', $data->$editorID);
$data->$editorID = preg_replace("/ src=\"" . htmlspecialchars($readLinkReg) . "\" /", ' src="' . $imgURL . '" ', $data->$editorID);
}
return $data;
}
@@ -628,8 +654,8 @@ class fileModel extends model
if(!extension_loaded('gd') or !function_exists('imagecreatefromjpeg')) return $file;
$pathName = $file['pathname'];
$fileName = $this->savePath . $pathName;
$suffix = strrchr($fileName, '.');
$fileName = $this->savePath . $this->getSaveName($pathName);
$suffix = $file['extension'];
$lowerSuffix = strtolower($suffix);
if(!in_array($lowerSuffix, $this->config->file->image2Compress)) return $file;
@@ -639,12 +665,11 @@ class fileModel extends model
$compressedName = str_replace($suffix, $newSuffix, $pathName);
$res = $lowerSuffix == '.bmp' ? $this->imagecreatefrombmp($fileName) : imagecreatefromjpeg($fileName);
imagejpeg($res, $this->savePath . $compressedName, $quality);
if($fileName != $this->savePath . $compressedName) unlink($fileName);
imagejpeg($res, $fileName, $quality);
$file['pathname'] = $compressedName;
$file['extension'] = ltrim($newSuffix, '.');
$file['size'] = filesize($this->savePath . $compressedName);
$file['pathname'] = $compressedName;
$file['extension'] = ltrim($newSuffix, '.');
$file['size'] = filesize($fileName);
return $file;
}
@@ -744,22 +769,13 @@ class fileModel extends model
* @access public
* @return object
*/
public function revertRealSRC($data, $fields)
public function replaceImgURL($data, $fields)
{
if(is_string($fields)) $fields = explode(',', str_replace(' ', '', $fields));
foreach($fields as $field)
{
if(empty($field) or empty($data->$field)) continue;
preg_match_all('/ src="{([0-9]+)}" /', $data->$field, $matchs);
if(!empty($matchs[1]))
{
$files = $this->dao->select('id,extension')->from(TABLE_FILE)->where('id')->in($matchs[1])->fetchPairs('id', 'extension');
foreach($matchs[0] as $i => $match)
{
$fileID = $matchs[1][$i];
$data->$field = str_replace($match, ' src="' . helper::createLink('file', 'read', "fileID=$fileID", $files[$fileID]) . '" ', $data->$field);
}
}
$data->$field = preg_replace('/ src="{([0-9]+)(\.(\w+))?}" /', ' src="' . helper::createLink('file', 'read', "fileID=$1", "$3") . '" ', $data->$field);
}
return $data;