* Add function setFielAndRealPaths to file model.

This commit is contained in:
wangjianhua
2022-11-09 23:28:37 +08:00
parent fc4302e603
commit cd5749835e
8 changed files with 91 additions and 44 deletions
+2 -1
View File
@@ -616,6 +616,7 @@ class doc extends control
*/
public function delete($docID, $confirm = 'no', $from = 'list')
{
$this->loadModel('file');
if($confirm == 'no')
{
$type = $this->dao->select('type')->from(TABLE_DOC)->where('id')->eq($docID)->fetch('type');
@@ -637,7 +638,7 @@ class doc extends control
$this->loadModel('action')->create($file->objectType, $file->objectID, 'deletedFile', '', $extra=$file->title);
$fileRecord = $this->dao->select('id')->from(TABLE_FILE)->where('pathname')->eq($file->pathname)->fetch();
if(empty($fileRecord)) @unlink($file->realPath);
if(empty($fileRecord)) $this->file->unlinkFile($file);
}
/* if ajax request, send result. */
+2 -6
View File
@@ -643,9 +643,7 @@ class docModel extends model
{
foreach($files as $file)
{
$pathName = $this->file->getRealPathName($file->pathname);
$file->webPath = $this->file->webPath . $pathName;
$file->realPath = $this->file->savePath . $pathName;
$this->loadModel('file')->setFileWebAndRealPaths($file);
if(strpos(",{$docContent->files},", ",{$file->id},") !== false) $docFiles[$file->id] = $file;
}
}
@@ -1748,9 +1746,7 @@ class docModel extends model
foreach($files as $fileID => $file)
{
$pathName = $this->file->getRealPathName($file->pathname);
$file->realPath = $this->file->savePath . $pathName;
$file->webPath = $this->file->webPath . $pathName;
$this->file->setFileWebAndRealPaths($file);
}
return $files;
+2 -6
View File
@@ -84,12 +84,8 @@
<div class='col'>
<div class='lib-file'>
<?php
$imageWidth = 0;
if(stripos('jpg|jpeg|gif|png|bmp', $file->extension) !== false and file_exists($file->realPath))
{
$imageSize = getimagesize($file->realPath);
$imageWidth = $imageSize ? $imageSize[0] : 0;
}
$imageSize = $this->loadModel('file')->getImageSize($file);
$imageWidth = $imageSize[0];
$fileID = $file->id;
$url = helper::createLink('file', 'download', 'fileID=' . $fileID);
+3 -3
View File
@@ -146,7 +146,7 @@ class file extends control
$file->extension = $extension;
}
if(file_exists($file->realPath))
if($this->file->fileExists($file))
{
/* If the mode is open, locate directly. */
if($mode == 'open')
@@ -290,7 +290,7 @@ class file extends control
/* Fix Bug #1518. */
$fileRecord = $this->dao->select('id')->from(TABLE_FILE)->where('pathname')->eq($file->pathname)->fetch();
if(empty($fileRecord)) @unlink($file->realPath);
if(empty($fileRecord)) $this->file->unlinkFile($file);
/* Update test case version for test case synchronization. */
if($file->objectType == 'testcase') $this->file->updateTestcaseVersion($file);
@@ -500,7 +500,7 @@ class file extends control
public function read($fileID)
{
$file = $this->file->getById($fileID);
if(empty($file) or !file_exists($file->realPath)) return false;
if(empty($file) or !$this->file->fileExists($file)) return false;
$obLevel = ob_get_level();
for($i = 0; $i < $obLevel; $i++) ob_end_clean();
+71 -12
View File
@@ -60,9 +60,7 @@ class fileModel extends model
continue;
}
$realPathName = $this->getRealPathName($file->pathname);
$file->realPath = $this->savePath . $realPathName;
$file->webPath = $this->webPath . $realPathName;
$this->setFileWebAndRealPaths($file);
}
return $files;
@@ -88,9 +86,7 @@ class fileModel extends model
return $file;
}
$realPathName = $this->getRealPathName($file->pathname);
$file->realPath = $this->savePath . $realPathName;
$file->webPath = $this->webPath . $realPathName;
$this->setFileWebAndRealPaths($file);
return $file;
}
@@ -117,9 +113,7 @@ class fileModel extends model
continue;
}
$realPathName = $this->getRealPathName($file->pathname);
$file->realPath = $this->savePath . $realPathName;
$file->webPath = $this->webPath . $realPathName;
$this->setFileWebAndRealPaths($file);
}
return $files;
@@ -487,6 +481,20 @@ class fileModel extends model
$this->webPath = $this->app->getWebRoot() . "data/upload/{$this->app->company->id}/";
}
/**
* Set paths: realPath and webPath.
*
* @param object $file
* @access public
* @return void
*/
public function setFileWebAndRealPaths(&$file)
{
$pathName = $this->getRealPathName($file->pathname);
$file->realPath = $this->savePath . $pathName;
$file->webPath = $this->webPath . $pathName;
}
/**
* Insert the set image size code.
*
@@ -512,6 +520,30 @@ class fileModel extends model
return str_replace(' src="data/upload', ' onload="setImageSize(this,' . $maxSize . ')" src="data/upload', $content);
}
/**
* Check file exists or not.
*
* @param object $file
* @access public
* @return bool
*/
public function fileExists($file)
{
return file_exists($file->realPath);
}
/**
* Unlink file.
*
* @param object $file
* @access public
* @return bool|null
*/
public function unlinkFile($file)
{
return @unlink($file->realPath);
}
/**
* Replace a file.
*
@@ -1021,7 +1053,7 @@ class fileModel extends model
{
$file = $this->getById($imageID);
$this->dao->delete()->from(TABLE_FILE)->where('id')->eq($imageID)->exec();
@unlink($file->realPath);
$this->unlinkFile($file);
}
}
unset($_SESSION['album'][$uid]);
@@ -1087,9 +1119,12 @@ class fileModel extends model
*/
public function getImageSize($file)
{
if(stripos('jpg|jpeg|gif|png|bmp', $file->extension) === false) return array(0, 0, $$file->extension);
if($this->config->file->storageType == 'fs')
{
return file_exists($file->realPath) ? getimagesize($file->realPath) : 0;
return file_exists($file->realPath) ? getimagesize($file->realPath) : array(0, 0, $$file->extension);
}
else if($this->config->file->storageType == 's3')
{
@@ -1155,7 +1190,7 @@ class fileModel extends model
$this->dao->delete()->from(TABLE_FILE)->where('id')->in($deleteFiles)->exec();
foreach($deleteFiles as $fileID)
{
@unlink($oldObject->files[$fileID]->realPath);
$this->unlinkFile($oldObject->files[$fileID]);
$oldFiles = empty($oldFiles) ? '' : trim(str_replace(",$fileID,", ',', ",$oldFiles,"), ',');
}
}
@@ -1167,4 +1202,28 @@ class fileModel extends model
$newObject->files = trim($oldFiles . $addedFiles, ',');
$oldObject->files = join(',', array_keys($oldObject->files));
}
/**
* Get last modified timestamp of file.
*
* @param object $file
* @access public
* @return int
*/
public function fileMTime($file)
{
return filemtime($file->realPath);
}
/**
* Get file size.
*
* @param object $file
* @access public
* @return int
*/
public function fileSize($file)
{
return filesize($file->realPath);
}
}
+3 -6
View File
@@ -54,12 +54,9 @@
$uploadDate = $lang->file->uploadDate . substr($file->addedDate, 0, 10);
$fileTitle = "<i class='icon icon-file-text'></i> &nbsp;" . $file->title;
if(strpos($file->title, ".{$file->extension}") === false && $file->extension != 'txt') $fileTitle .= ".{$file->extension}";
$imageWidth = 0;
if(stripos('jpg|jpeg|gif|png|bmp', $file->extension) !== false)
{
$imageSize = $this->file->getImageSize($file);
$imageWidth = $imageSize ? $imageSize[0] : 0;
}
$imageSize = $this->file->getImageSize($file);
$imageWidth = $imageSize[0];
$fileSize = 0;
/* Show size info. */
+7 -7
View File
@@ -38,8 +38,8 @@ $(document).ready(function()
/**
* Delete a file.
*
* @param int $fileID
* @param object $obj
* @param int fileID
* @param object obj
* @access public
* @return void
*/
@@ -58,10 +58,10 @@ function deleteFile(fileID, obj)
/**
* Download a file, append the mouse to the link. Thus we call decide to open the file in browser no download it.
*
* @param int $fileID
* @param int $extension
* @param int $imageWidth
* @param string $fileTitle
* @param int fileID
* @param int extension
* @param int imageWidth
* @param string fileTitle
* @access public
* @return void
*/
@@ -157,7 +157,7 @@ function setFileName(fileID)
if(stripos('jpg|jpeg|gif|png|bmp', $file->extension) !== false)
{
$imageSize = $this->file->getImageSize($file);
$imageWidth = $imageSize ? $imageSize[0] : 0;
$imageWidth = $imageSize[0];
}
$fileSize = 0;
+1 -3
View File
@@ -1397,9 +1397,7 @@ class testtaskModel extends model
$stepFiles = array();
foreach($files as $file)
{
$pathName = $this->file->getRealPathName($file->pathname);
$file->webPath = $this->file->webPath . $pathName;
$file->realPath = $this->file->savePath . $pathName;
$this->file->setFileWebAndRealPaths($file);
if($file->objectType == 'caseResult')
{
$resultFiles[$file->objectID][$file->id] = $file;