Upload.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace app\admin\controller;
  3. /**
  4. * @title 图片上传接口
  5. * @desc
  6. * @author Rock
  7. * @version 1.0
  8. * @icon fa fa-file-image-o
  9. */
  10. use think\Exception;
  11. use upload\Uploader;
  12. use app\common\model\base\Upload as UploadModel;
  13. class Upload extends Base
  14. {
  15. /** 无需登录的方法 */
  16. protected $noNeedLogin = ['getMime'];
  17. /**
  18. * @title: 自定义上传配置
  19. * @desc: 此处的配置会覆盖upload\Uploader类里的配置
  20. * @return {*}
  21. * @author: Rock
  22. * @method: POST
  23. * @Date: 2023-05-18 15:00:44
  24. */
  25. private $Config = [
  26. 'attach_format_image' => 'jpg,jpeg,png,gif,bmp',
  27. 'attach_format_image_size' => '2MB',
  28. 'attach_format_video' => 'mp4,3gp,avi,mpeg',
  29. 'attach_format_video_size' => '50MB',
  30. 'attach_format_audio' => 'mp3,wav',
  31. 'attach_format_audio_size' => '5MB',
  32. 'attach_format_file' => 'xls,xlsx,txt',
  33. 'attach_format_file_size' => '2MB',
  34. 'allowExts' => ['jpg','png','jpeg','bmp','pdf','xls','doc','xlsx','docx','zip'],//允许上传的文件类型
  35. ];
  36. public function initialize()
  37. {
  38. parent::initialize();
  39. }
  40. /**
  41. * @title: 获取MIME类型列表
  42. * @desc:
  43. * @param {}
  44. * @return {}
  45. * @Author: Rock
  46. * @Date: 2021-05-17 14:49:01
  47. * @LastEditTime: Do not edit
  48. */
  49. public function getMime()
  50. {
  51. $mimes = Uploader::$mimes;
  52. return res(1,"获取成功",$mimes);
  53. }
  54. /**
  55. * @title 文件上传接口
  56. * @desc 文件上传接口,注意大于10M时,不会返回base64;图片文件会自动生成预览图,预览图文件命名为原图文件名拼接.prev加.后缀,如123.jpg,预览图为123.jpg.prev.jpg
  57. * @method GET/POST
  58. * @param {string} {file} {} {上传的文件}
  59. * @return {string} {data} {} {上传成功后的文件}
  60. * @author Rock
  61. */
  62. public function index()
  63. {
  64. try {
  65. $mimes = Uploader::$mimes;
  66. //上传文件是否压缩
  67. $Compress=input('Compress')??0;
  68. $this->Config['Compress'] = $Compress;
  69. $upload = new Uploader($this->Config);// 实例化上传类
  70. if(!$upload->upload()) {
  71. $errormsg = $upload->getErrorMsg();
  72. WLog('Uploader',$errormsg);
  73. return res(2,$errormsg);
  74. }else{
  75. $info = $upload->getUploadFileInfo();
  76. $saveData = [];
  77. $hashAry = array_column($info,'hash');
  78. $list = UploadModel::where('md5','IN',$hashAry)->select()->toArray();
  79. $list = array_column($list,null,'md5');
  80. foreach($info as &$file){
  81. // 文件小于10M时返回base64,大于则不返回
  82. $imgurl=str_replace(public_path(),'',$file['savepath']).$file['savename'];
  83. if($file['size']<5242880){
  84. $base64 = file2base64($file['savepath'].$file['savename']);
  85. $file['base64']=$base64;
  86. }
  87. $file['src'] = DS.$imgurl;
  88. $file['fullpath'] = WEBURL.DS.$imgurl;
  89. if(!isset($list[$file['hash']])){
  90. $saveData[] = [
  91. 'name' => $file['name'],
  92. 'savename'=>$file['savename'],
  93. 'size'=>$file['size'],
  94. 'ext'=>strtolower($file['extension']),
  95. 'path'=>$file['src'],
  96. 'fullpath'=>$file['savepath'].$file['savename'],
  97. 'md5'=>$file['hash'],
  98. 'type' => $file['type'],
  99. 'uid' => $this->userinfo['user_id']
  100. ];
  101. }else{
  102. unlink($file['savepath'].$file['savename']);
  103. $oldFile = $list[$file['hash']];
  104. $oldFile['path'] = str_replace('\\',"/",$oldFile['path']);
  105. $file['src'] = $oldFile['path'];
  106. $file['savename'] = $oldFile['savename'];
  107. $file['fullpath'] = WEBURL.$oldFile['path'];
  108. }
  109. }
  110. if(!empty($saveData)){
  111. (new UploadModel)->saveAll($saveData);
  112. }
  113. return res(1,'上传成功',$info);
  114. }
  115. }catch (\Exception $e){
  116. WLog('Uploader',$e->getFile().' '.$e->getLine().':'.$e->getMessage());
  117. return res(2,'上传失败',$e->getFile().' '.$e->getLine().':'.$e->getMessage());
  118. }
  119. }
  120. /**
  121. * @title: 通过Md5检查文件是否曾经上传过
  122. * @desc:
  123. * @param {string} {md5} {必传} {文件MD5值}
  124. * @return {*}
  125. * @Author: Rock
  126. * @Date: 2022-02-22 19:09:19
  127. * @LastEditTime: Do not edit
  128. */
  129. public function checkMd5($md5='')
  130. {
  131. $info = UploadModel::where('md5',$md5)->find();
  132. $mimeList= Uploader::$mimes;
  133. if($info){
  134. $ext = strtolower($info->ext);
  135. $data = [
  136. 'base64' => in_array($ext,['jpg','png','jpeg','gif'])?file2base64($info->fullpath):'',
  137. 'extension' => $info->ext,
  138. 'fullpath' => WEBURL.$info->path,
  139. 'hash' => $info->md5,
  140. 'key' => 'file',
  141. 'mime' => isset($mimeList[$ext])?$mimeList[$ext]:'',
  142. 'name' => $info->name,
  143. 'savename' => $info->savename,
  144. 'savepath' => $info->fullpath,
  145. 'size' => $info->size,
  146. 'src' => $info->path,
  147. ];
  148. // 如果不是当前用户上传的,给当前用户创建一份上传记录,以便当前用户在文件库里使用
  149. $newData = $info->toArray();
  150. unset($newData['id']);
  151. $newData['uid'] = $this->userinfo['user_id'];
  152. UploadModel::create($newData);
  153. return res(1,'已上传',$data);
  154. }else{
  155. return res(1,"未上传");
  156. }
  157. }
  158. /**
  159. * @title: 获取已上传文件列表
  160. * @desc: 描述
  161. * @param {int} {pageNo} {1} {页码}
  162. * @param {int} {pageSize} {20} {每页数量}
  163. * @return {*}
  164. * @author: Rock
  165. * @method: POST
  166. * @Date: 2023-04-10 10:55:39
  167. */
  168. public function getList(int $pageNo=1,int $pageSize=20)
  169. {
  170. $data = $this->request->param();
  171. $where = [];
  172. if(!$this->userinfo['is_developer']){
  173. $where[] = ['uid','=',$this->userinfo->user_id];
  174. }
  175. if(!empty($data['keyword'])){
  176. $keyword = $data['keyword'];
  177. $where[] = ['name|savename','LIKE',"%$keyword%"];
  178. }
  179. if(!empty($data['type'])){
  180. $where[] = ['type','=',$data['type']];
  181. }
  182. $res = UploadModel::where($where)->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]);
  183. $list = $res->items();
  184. $total = $res->total();
  185. return pageRes(1,"获取成功",$total,$list,$where);
  186. }
  187. /**
  188. * @title: 获取可选项
  189. * @desc: 描述
  190. * @return {*}
  191. * @author: Rock
  192. * @method: POST
  193. * @Date: 2023-04-10 11:37:38
  194. */
  195. public function getOptions()
  196. {
  197. $data = [
  198. 'typeList' => UploadModel::typeList(),
  199. ];
  200. return res(1,"获取成功",$data);
  201. }
  202. /**
  203. * @title: 删除上传文件
  204. * @desc: 描述
  205. * @param {int} {id} {} {上传文件ID}
  206. * @return {*}
  207. * @author: Rock
  208. * @method: POST
  209. * @Date: 2023-06-08 14:42:45
  210. */
  211. public function doDelete(int $id)
  212. {
  213. $info = UploadModel::find($id);
  214. try{
  215. if($info){
  216. if(file_exists($info->fullpath)){
  217. @unlink($info->fullpath);
  218. }
  219. $prePath = $info->fullpath.'.prev.jpg';
  220. if(file_exists($prePath)){
  221. @unlink($prePath);
  222. }
  223. }
  224. }catch(\Exception $e){
  225. $msg = $e->getMessage();
  226. WLog('UploadDelete',$msg);
  227. if(false!==strpos($msg,'file_exists(): open_basedir restriction in effect')){
  228. return res(2,"删除失败:网站开启防跨站攻击,无法删除文件,请检查是否属于本站文件");
  229. }
  230. }
  231. $info->delete();
  232. return res(1,"删除成功");
  233. }
  234. }