123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- declare(strict_types=1);
- namespace app\common\model;
- use think\facade\Config;
- use think\File;
- use think\exception\FileException;
- class FileManager extends \think\Model
- {
- protected $pk = "fileid";
- protected $schema = [
- 'fileid' => "int", // '文件id'
- 'filename' => "varchar", // '文件名称'
- 'filesize' => "int", // '文件大小字节'
- 'filetime' => "int", // '文件上传时间'
- 'filepath' => "varchar", // '文件路径'
- 'fileextension' => "varchar", // '文件扩展名'
- 'title' => "varchar", // '文件title'
- 'type' => "int", // '0为附件,1为图片,2为Flash文件,3为多媒体文件'
- 'onclick' => "int", // '下载量'
- 'username' => "varchar", // '上传者'
- 'infoid' => "int", // '信息ID',
- 'cjid' => "int", // '信息临时ID'
- 'hash_md5' => "varcahr" // hash值(MD5)
- ];
- protected $autoWriteTimestamp = false;
- public static function queryPage(array $param = [])
- {
- $limit = (int)$param['limit'];
- $where = [];
- return self::where($where)->field('fileid,filename,filesize,filetime,filepath,fileextension,title,username')->order('fileid DESC')->paginate(['list_rows'=>$limit, 'query' => $param]);
- }
- public static function queryList(array $param = [])
- {
- $where = [];
- $cjid = isset($param['cjid']) ? (int) $param['cjid'] : 0;
- $infoid = isset($param['infoid']) ? (int) $param['infoid'] : 0;
- if ($cjid) {
- $where[] = ['cjid', '=', $cjid];
- }
- if ($infoid) {
- $where[] = ['infoid', '=', $infoid];
- }
- return self::where($where)->field('fileid,filename,filesize,filetime,filepath,fileextension,title,username')->order('fileid DESC')->select();
- }
- public static function saveFile(File $file)
- {
- $fileinfo = self::where('hash_md5', $file->md5())->find();
- $publicRootPath = str_replace('\\', '/', Config::get('filesystem.disks.public.root'));
- $publicUrlPath = Config::get('filesystem.disks.public.url');
- if ($fileinfo == null) {
- $fileinfo = new static();
- } elseif ($publicRootPath . $fileinfo->filepath == $file->getPathname()) { // 路径不同的文件
- $fileinfo = new static();
- }
- $fileinfo->filename = $file->getFilename();
- $fileinfo->filesize = $file->getSize();
- $fileinfo->filetime = $file->getCTime();
- $fileinfo->filepath = $publicUrlPath . str_replace($publicRootPath, '', $file->getPathname());
- $fileinfo->fileextension = $file->getExtension();
- $fileinfo->hash_md5 = $file->md5();
- $fileinfo->username = 'system';
- $fileinfo->save();
- }
- public static function saveFileInfo($file, $savename, $originalName = '', $id = 0, $cjid = 0, $username = 'system')
- {
- if (is_string($file)) {
- $file = new File($file);
- }
- if (!$file->isFile()) {
- throw new FileException('file not exist');
- }
- $publicUrlPath = Config::get('filesystem.disks.public.url');
- $fileinfo = new static();
-
- $fileinfo->filename = basename($savename);
- $fileinfo->filepath = $publicUrlPath . '/'. $savename;
- $fileinfo->title = $originalName;
- $fileinfo->id = $id;
- $fileinfo->cjid = $cjid;
- $fileinfo->username = $username;
- $fileinfo->filesize = $file->getSize();
- $fileinfo->filetime = $file->getCTime();
- $fileinfo->fileextension = $file->extension();
- $fileinfo->hash_md5 = $file->md5();
-
- $fileinfo->save();
- return $fileinfo;
- }
- public static function saveInfoid($infoid, $cjid)
- {
- self::where('cjid', 'IN', $cjid)->data(['infoid'=>$infoid, 'cjid'=>0])->update();
- }
- }
|