FileManager.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\common\model;
  4. use think\facade\Config;
  5. use think\File;
  6. use think\exception\FileException;
  7. class FileManager extends \think\Model
  8. {
  9. protected $pk = "fileid";
  10. protected $schema = [
  11. 'fileid' => "int", // '文件id'
  12. 'filename' => "varchar", // '文件名称'
  13. 'filesize' => "int", // '文件大小字节'
  14. 'filetime' => "int", // '文件上传时间'
  15. 'filepath' => "varchar", // '文件路径'
  16. 'fileextension' => "varchar", // '文件扩展名'
  17. 'title' => "varchar", // '文件title'
  18. 'type' => "int", // '0为附件,1为图片,2为Flash文件,3为多媒体文件'
  19. 'onclick' => "int", // '下载量'
  20. 'username' => "varchar", // '上传者'
  21. 'infoid' => "int", // '信息ID',
  22. 'cjid' => "int", // '信息临时ID'
  23. 'hash_md5' => "varcahr" // hash值(MD5)
  24. ];
  25. protected $autoWriteTimestamp = false;
  26. public static function queryPage(array $param = [])
  27. {
  28. $limit = (int)$param['limit'];
  29. $where = [];
  30. return self::where($where)->field('fileid,filename,filesize,filetime,filepath,fileextension,title,username')->order('fileid DESC')->paginate(['list_rows'=>$limit, 'query' => $param]);
  31. }
  32. public static function queryList(array $param = [])
  33. {
  34. $where = [];
  35. $cjid = isset($param['cjid']) ? (int) $param['cjid'] : 0;
  36. $infoid = isset($param['infoid']) ? (int) $param['infoid'] : 0;
  37. if ($cjid) {
  38. $where[] = ['cjid', '=', $cjid];
  39. }
  40. if ($infoid) {
  41. $where[] = ['infoid', '=', $infoid];
  42. }
  43. return self::where($where)->field('fileid,filename,filesize,filetime,filepath,fileextension,title,username')->order('fileid DESC')->select();
  44. }
  45. public static function saveFile(File $file)
  46. {
  47. $fileinfo = self::where('hash_md5', $file->md5())->find();
  48. $publicRootPath = str_replace('\\', '/', Config::get('filesystem.disks.public.root'));
  49. $publicUrlPath = Config::get('filesystem.disks.public.url');
  50. if ($fileinfo == null) {
  51. $fileinfo = new static();
  52. } elseif ($publicRootPath . $fileinfo->filepath == $file->getPathname()) { // 路径不同的文件
  53. $fileinfo = new static();
  54. }
  55. $fileinfo->filename = $file->getFilename();
  56. $fileinfo->filesize = $file->getSize();
  57. $fileinfo->filetime = $file->getCTime();
  58. $fileinfo->filepath = $publicUrlPath . str_replace($publicRootPath, '', $file->getPathname());
  59. $fileinfo->fileextension = $file->getExtension();
  60. $fileinfo->hash_md5 = $file->md5();
  61. $fileinfo->username = 'system';
  62. $fileinfo->save();
  63. }
  64. public static function saveFileInfo($file, $savename, $originalName = '', $id = 0, $cjid = 0, $username = 'system')
  65. {
  66. if (is_string($file)) {
  67. $file = new File($file);
  68. }
  69. if (!$file->isFile()) {
  70. throw new FileException('file not exist');
  71. }
  72. $publicUrlPath = Config::get('filesystem.disks.public.url');
  73. $fileinfo = new static();
  74. $fileinfo->filename = basename($savename);
  75. $fileinfo->filepath = $publicUrlPath . '/'. $savename;
  76. $fileinfo->title = $originalName;
  77. $fileinfo->id = $id;
  78. $fileinfo->cjid = $cjid;
  79. $fileinfo->username = $username;
  80. $fileinfo->filesize = $file->getSize();
  81. $fileinfo->filetime = $file->getCTime();
  82. $fileinfo->fileextension = $file->extension();
  83. $fileinfo->hash_md5 = $file->md5();
  84. $fileinfo->save();
  85. return $fileinfo;
  86. }
  87. public static function saveInfoid($infoid, $cjid)
  88. {
  89. self::where('cjid', 'IN', $cjid)->data(['infoid'=>$infoid, 'cjid'=>0])->update();
  90. }
  91. }