Upload.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2024/10/6 17:53
  7. */
  8. namespace App\Http\Common;
  9. use Illuminate\Http\UploadedFile;
  10. use Illuminate\Support\Facades\File;
  11. use Illuminate\Support\Facades\Storage;
  12. use Illuminate\Support\Facades\Validator;
  13. class Upload
  14. {
  15. protected $request;
  16. protected $config;//配置
  17. protected $uniacid;
  18. protected $path_type;//图片 音频 视频
  19. protected $attachment_model;
  20. protected $is_weiqin = false;
  21. /**
  22. * 架构函数
  23. * @access public
  24. */
  25. public function __construct()
  26. {
  27. }
  28. //上传
  29. public function upload($type, UploadedFile $file, $config = array(), string $is_check = '')
  30. {
  31. if (!empty($config)) $this->config = $config;
  32. $base_path = '';
  33. switch ($type) {
  34. //图片
  35. //音频
  36. case 'audio':
  37. $base_path .= 'audio/';
  38. $type_data = 2;
  39. break;
  40. //视频
  41. case 'video':
  42. $base_path .= 'video/';
  43. $type_data = 3;
  44. break;
  45. //证书
  46. case 'cert':
  47. $base_path .= 'cert/';
  48. $type_data = 4;
  49. break;
  50. //证书
  51. // case 'wxuploadkey':
  52. // $base_path .= 'wxuploadkey/';
  53. // $type_data = 4;
  54. // break;
  55. //证书
  56. case 'file':
  57. $base_path .= 'file/';
  58. $type_data = 6;
  59. break;
  60. case 'picture':
  61. default:
  62. $base_path .= 'images/';
  63. $type_data = 1;
  64. break;
  65. }
  66. //根据时间生成路径
  67. $base_path = $base_path . date("y/m/d") . '/';
  68. $info = null;
  69. $upload_status = false;
  70. //数据检查
  71. if ($this->checkFile($type, $file, $is_check)) {
  72. $file_name = null;
  73. //本地保存
  74. // if (in_array($type, ['cert', 'wxuploadkey'])) {
  75. // $file_name = $this->uniacid . '_' . $file->getOriginalName();
  76. // $this->config['open_oss'] = 0;
  77. // }
  78. $file_name .= $is_check;
  79. $info_path = $this->fileLoaclSave($base_path, $file, $file_name);
  80. //获取数据
  81. $info = $this->fileInfo($info_path, $file->getClientOriginalName() . $is_check, $type_data);
  82. //云服务器上传
  83. if (isset($this->config['open_oss'])) {
  84. /* switch ($this->config['open_oss']) {
  85. //本地
  86. case 0:
  87. $upload_status = true;
  88. $info['longbing_driver'] = 'loacl';
  89. break;
  90. case 1:
  91. $oss_res = $this->aliyunUpload($info_path);
  92. if (isset($this->config['aliyun_base_dir']) && !empty($this->config['aliyun_base_dir'])) $info_path = $this->config['aliyun_base_dir'] . '/' . $info_path;
  93. if (in_array(substr($info_path, 0, 1), ['/', "/"])) {
  94. $info_path = substr($info_path, 1, (strlen($info_path) - 1));
  95. }
  96. $info['attachment'] = $info_path;
  97. $info['longbing_driver'] = 'aliyun';
  98. if (isset($oss_res['info']['url'])) $upload_status = true;
  99. break;
  100. case 2:
  101. $oss_res = $this->qiniuUpload($info_path);
  102. $info['longbing_driver'] = 'qiniuyun';
  103. if (!empty($oss_res) && empty($oss_res[1])) $upload_status = true;
  104. break;
  105. case 3:
  106. $oss_res = $this->tenxunUpoload($info_path);
  107. $info['longbing_driver'] = 'tengxunyun';
  108. if (isset($oss_res['ETag']) && isset($oss_res['ObjectURL'])) $upload_status = true;
  109. break;
  110. default:
  111. $info['longbing_driver'] = 'loacl';
  112. $upload_status = true;
  113. break;
  114. }*/
  115. } else {
  116. $upload_status = true;
  117. $info['driver'] = 'local';
  118. }
  119. }
  120. if (!$upload_status) $info = null;
  121. return $info;
  122. }
  123. public function checkFile($type, $file, bool $is_check = false)
  124. {
  125. $validator = new Validator();
  126. switch ($type) {
  127. case 'picture':
  128. $validator = Validator::make(['file' => $file], [
  129. 'file' => 'required|file|mimes:jpg,jpeg,png,webp|max:2048',
  130. [
  131. '*' => '文件错误'
  132. ]
  133. ]);
  134. // $result = validate(['file' => ['fileSize' => 10 * 1024 * 1024, 'fileExt' => 'jpg,jpeg,bmp,png,image,gif']])->check(['file' => $file]);
  135. break;
  136. case 'audio':
  137. $check = empty($is_check) ? 'mp3,wma,wav,m4a' : '';
  138. // $result = validate(['file' => ['fileSize' => 50 * 1024 * 1024, 'fileExt' => $check]])->check(['file' => $file]);
  139. break;
  140. case 'video':
  141. // $result = validate(['file' => ['fileSize' => 50 * 1024 * 1024, 'fileExt' => 'wmv,mp4,mp3,avi,mpg,rmvb,MPEG-4,MPEG,MOV,3GP,MPV,quicktime,Quicktime,mov']])->check(['file' => $file]);
  142. break;
  143. case 'cert':
  144. // $result = validate(['file' => ['fileSize' => 50 * 1024 * 1024, 'fileExt' => 'cert,pem']])->check(['file' => $file]);
  145. break;
  146. // case 'wxuploadkey':
  147. // $result = validate(['file' => ['fileSize' => 50 * 1024 * 1024, 'fileExt' => 'cert,pem']])->check(['file' => $file]);
  148. // break;
  149. case 'file':
  150. // $result = validate(['file' => ['fileSize' => 50 * 1024 * 1024, 'fileExt' => 'doc,xls,docx,xlsx,ppt,pptx,pdf']])->check(['file' => $file]);
  151. break;
  152. default:
  153. break;
  154. }
  155. if ($validator->fails()) {
  156. return false;
  157. }
  158. return true;
  159. }
  160. //本地保存
  161. public function fileLoaclSave($path, UploadedFile $file, $file_name = ''): string
  162. {
  163. if (!$file_name) $file_name = $file->hashName();
  164. // $storagePath = Storage::disk('public')->path($path);
  165. // 检查文件夹是否存在,如果不存在,则创建文件夹
  166. if (!Storage::disk('public')->exists($path)) {
  167. Storage::disk('public')->makeDirectory($path);
  168. }
  169. // if (!is_dir($storagePath)) {
  170. // mkdir($storagePath, 0775, true);
  171. // }
  172. //保存
  173. Storage::disk('public')->putFileAs($path, $file, $file_name);
  174. return Storage::url($path . $file_name);
  175. }
  176. //生成返回数据
  177. public function fileInfo($path, $file_name, $type_data)
  178. {
  179. $result = array(
  180. 'attachment' => ltrim($path, '/'),
  181. 'filename' => $file_name,
  182. 'createtime' => time(),
  183. 'type' => $type_data
  184. );
  185. return $result;
  186. }
  187. }