UploadService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\common\service;
  4. use app\common\model\Attachment;
  5. /**
  6. * 上传文件服务
  7. */
  8. abstract class UploadService extends BaseService
  9. {
  10. //上传配置
  11. protected $config = [
  12. 'cdnurl' => '',
  13. 'maxsize' => 2,
  14. 'mimetype' => 'jpg,png,bmp,jpeg,gif',
  15. 'thumb' => false,
  16. 'compress' => false,
  17. 'watermark'=>false
  18. ];
  19. //存储方式
  20. protected $disks;
  21. /* @var \think\File $file */
  22. protected $file;
  23. protected $category;
  24. protected $admin_id;
  25. protected $user_id;
  26. //保存文件
  27. abstract protected function putFile():array;
  28. //生成缩略图
  29. abstract protected function thumb(string $url,string $fullurl):string;
  30. //压缩图片
  31. abstract protected function compress(string $url,string $fullurl);
  32. //图片加水印
  33. abstract protected function watermark(string $url,string $fullurl);
  34. abstract protected function imageFileinfo(string $url,string $fullurl);
  35. //删除文件
  36. abstract public static function deleteFile(Attachment $attachment);
  37. //检查文件
  38. public function save()
  39. {
  40. $sha1 = $this->file->sha1();
  41. $weigh=Attachment::max('weigh');
  42. $attachment = Attachment::where(['sha1'=>$sha1,'storage'=>$this->disks])->find();
  43. if ($attachment) {
  44. $attachment->weigh=$weigh+1;
  45. $attachment->category=$this->category;
  46. $attachment->save();
  47. return [
  48. 'url'=>$attachment->url,
  49. 'fullurl'=>$attachment->fullurl,
  50. 'thumburl'=>$attachment->thumburl
  51. ];
  52. }
  53. [$url,$fullurl] = $this->putFile();
  54. $thumburl=$this->getThumbUrl($fullurl);
  55. $imagetype=null;
  56. $imagewidth=null;
  57. $imageheight=null;
  58. $filesize=$this->file->getSize();
  59. if($this->isImage()){
  60. if($this->config['thumb']){
  61. $thumburl=$this->thumb($url,$fullurl);
  62. }
  63. if($this->config['compress']){
  64. $this->compress($url,$fullurl);
  65. }
  66. if($this->config['watermark']){
  67. $this->watermark($url,$fullurl);
  68. }
  69. //重新获取处理后的图片信息
  70. [$filesize,$imagetype,$imagewidth,$imageheight] = $this->imageFileinfo($url,$fullurl);
  71. }
  72. $attachment = new Attachment();
  73. $time = time();
  74. $attachment->save([
  75. 'category' => $this->category,
  76. 'admin_id' => $this->admin_id,
  77. 'user_id' => $this->user_id,
  78. 'url' => $url,
  79. 'fullurl' => $fullurl,
  80. 'thumburl' => $thumburl,
  81. 'is_image' => $this->isImage() ? 1 : 0,
  82. 'imagetype'=>$imagetype,
  83. 'imagewidth'=>$imagewidth,
  84. 'imageheight'=>$imageheight,
  85. 'filename'=>$this->file->getOriginalName(),
  86. 'filesize'=>$filesize,
  87. 'storage' => $this->disks,
  88. 'sha1' => $sha1,
  89. 'weigh'=>$weigh,
  90. 'uploadtime'=>$time,
  91. 'createtime'=>$time,
  92. 'updatetime'=>$time
  93. ]);
  94. return [
  95. 'url'=>$url,
  96. 'fullurl'=>$fullurl,
  97. 'thumburl'=>$thumburl
  98. ];
  99. }
  100. protected function init()
  101. {
  102. if (!$this->file) {
  103. throw new \Exception(__('上传文件不存在'));
  104. }
  105. //验证文件$this->file
  106. $maxsize = $this->config['maxsize'];
  107. $mimetype = $this->config['mimetype'];
  108. $ext = strtolower($this->file->extension());
  109. $size = $this->file->getSize();
  110. if ($size > $maxsize * 1024 * 1024) {
  111. throw new \Exception(__('上传文件大小超过限制'));
  112. }
  113. if (!in_array($ext, explode(',', $mimetype))) {
  114. throw new \Exception(__('上传文件类型不允许'));
  115. }
  116. }
  117. protected function isImage():bool
  118. {
  119. $ext = $this->file->extension();
  120. $ext= strtolower($ext);
  121. if (in_array($ext, ['jpg', 'png', 'bmp', 'jpeg', 'gif'])) {
  122. return true;
  123. }
  124. return false;
  125. }
  126. protected function getThumbUrl(string $fullurl):string
  127. {
  128. $domain=request()->domain();
  129. $ext = $this->file->extension();
  130. if (in_array($ext, ['jpg', 'png', 'bmp', 'jpeg', 'gif'])) {
  131. return $fullurl;
  132. }else if (in_array($ext, ['doc', 'docx'])) {
  133. return $domain.'/assets/img/fileicon/doc.png';
  134. }else if (in_array($ext, ['ppt', 'pptx'])) {
  135. return $domain.'/assets/img/fileicon/ppt.png';
  136. }else if (in_array($ext, ['xls', 'xlsx'])) {
  137. return $domain.'/assets/img/fileicon/xls.png';
  138. }else if (in_array($ext, ['mp3','wav','wma','ogg'])) {
  139. return $domain.'/assets/img/fileicon/audio.png';
  140. }else if (in_array($ext, ['mp4', 'avi', 'rmvb','swf', 'flv','rm', 'ram', 'mpeg', 'mpg', 'wmv', 'mov'])) {
  141. return $domain.'/assets/img/fileicon/video.png';
  142. }else if (in_array($ext, ['zip', 'rar', '7z', 'gz', 'tar'])) {
  143. return $domain.'/assets/img/fileicon/zip.png';
  144. }else if(in_array($ext,['apk','tiff','exe','html','pdf','psd','visio','svg','txt','xml'])){
  145. return $domain.'/assets/img/fileicon/'.$ext.'.png';
  146. }else{
  147. return $domain.'/assets/img/fileicon/wz.png';
  148. }
  149. }
  150. }