|
@@ -0,0 +1,207 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * @Name
|
|
|
+ * @Description
|
|
|
+ * @Author 刘学玺
|
|
|
+ * @Date 2024/10/6 17:53
|
|
|
+ */
|
|
|
+
|
|
|
+namespace App\Http\Common;
|
|
|
+
|
|
|
+use Illuminate\Http\UploadedFile;
|
|
|
+use Illuminate\Support\Facades\File;
|
|
|
+use Illuminate\Support\Facades\Storage;
|
|
|
+use Illuminate\Support\Facades\Validator;
|
|
|
+
|
|
|
+class Upload
|
|
|
+{
|
|
|
+ protected $request;
|
|
|
+ protected $config;//配置
|
|
|
+ protected $uniacid;
|
|
|
+ protected $path_type;//图片 音频 视频
|
|
|
+ protected $attachment_model;
|
|
|
+ protected $is_weiqin = false;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 架构函数
|
|
|
+ * @access public
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //上传
|
|
|
+ public function upload($type, UploadedFile $file, $config = array(), string $is_check = '')
|
|
|
+ {
|
|
|
+ if (!empty($config)) $this->config = $config;
|
|
|
+
|
|
|
+ $base_path = '';
|
|
|
+ switch ($type) {
|
|
|
+ //图片
|
|
|
+ //音频
|
|
|
+ case 'audio':
|
|
|
+ $base_path .= 'audio/';
|
|
|
+ $type_data = 2;
|
|
|
+ break;
|
|
|
+ //视频
|
|
|
+ case 'video':
|
|
|
+ $base_path .= 'video/';
|
|
|
+ $type_data = 3;
|
|
|
+ break;
|
|
|
+ //证书
|
|
|
+ case 'cert':
|
|
|
+ $base_path .= 'cert/';
|
|
|
+ $type_data = 4;
|
|
|
+ break;
|
|
|
+ //证书
|
|
|
+// case 'wxuploadkey':
|
|
|
+// $base_path .= 'wxuploadkey/';
|
|
|
+// $type_data = 4;
|
|
|
+// break;
|
|
|
+ //证书
|
|
|
+ case 'file':
|
|
|
+ $base_path .= 'file/';
|
|
|
+ $type_data = 6;
|
|
|
+ break;
|
|
|
+ case 'picture':
|
|
|
+ default:
|
|
|
+ $base_path .= 'images/';
|
|
|
+ $type_data = 1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ //根据时间生成路径
|
|
|
+ $base_path = $base_path . date("y/m/d") . '/';
|
|
|
+ $info = null;
|
|
|
+ $upload_status = false;
|
|
|
+
|
|
|
+ //数据检查
|
|
|
+ if ($this->checkFile($type, $file, $is_check)) {
|
|
|
+ $file_name = null;
|
|
|
+ //本地保存
|
|
|
+// if (in_array($type, ['cert', 'wxuploadkey'])) {
|
|
|
+// $file_name = $this->uniacid . '_' . $file->getOriginalName();
|
|
|
+// $this->config['open_oss'] = 0;
|
|
|
+// }
|
|
|
+
|
|
|
+ $file_name .= $is_check;
|
|
|
+
|
|
|
+ $info_path = $this->fileLoaclSave($base_path, $file, $file_name);
|
|
|
+ //获取数据
|
|
|
+ $info = $this->fileInfo($info_path, $file->getClientOriginalName() . $is_check, $type_data);
|
|
|
+ //云服务器上传
|
|
|
+ if (isset($this->config['open_oss'])) {
|
|
|
+ /* switch ($this->config['open_oss']) {
|
|
|
+ //本地
|
|
|
+ case 0:
|
|
|
+ $upload_status = true;
|
|
|
+ $info['longbing_driver'] = 'loacl';
|
|
|
+ break;
|
|
|
+ case 1:
|
|
|
+ $oss_res = $this->aliyunUpload($info_path);
|
|
|
+
|
|
|
+ if (isset($this->config['aliyun_base_dir']) && !empty($this->config['aliyun_base_dir'])) $info_path = $this->config['aliyun_base_dir'] . '/' . $info_path;
|
|
|
+ if (in_array(substr($info_path, 0, 1), ['/', "/"])) {
|
|
|
+ $info_path = substr($info_path, 1, (strlen($info_path) - 1));
|
|
|
+ }
|
|
|
+ $info['attachment'] = $info_path;
|
|
|
+ $info['longbing_driver'] = 'aliyun';
|
|
|
+ if (isset($oss_res['info']['url'])) $upload_status = true;
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ $oss_res = $this->qiniuUpload($info_path);
|
|
|
+ $info['longbing_driver'] = 'qiniuyun';
|
|
|
+ if (!empty($oss_res) && empty($oss_res[1])) $upload_status = true;
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ $oss_res = $this->tenxunUpoload($info_path);
|
|
|
+
|
|
|
+ $info['longbing_driver'] = 'tengxunyun';
|
|
|
+ if (isset($oss_res['ETag']) && isset($oss_res['ObjectURL'])) $upload_status = true;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ $info['longbing_driver'] = 'loacl';
|
|
|
+ $upload_status = true;
|
|
|
+ break;
|
|
|
+ }*/
|
|
|
+ } else {
|
|
|
+ $upload_status = true;
|
|
|
+ $info['driver'] = 'local';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!$upload_status) $info = null;
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function checkFile($type, $file, bool $is_check = false)
|
|
|
+ {
|
|
|
+ $validator = new Validator();
|
|
|
+ switch ($type) {
|
|
|
+ case 'picture':
|
|
|
+ $validator = Validator::make(['file' => $file], [
|
|
|
+ 'file' => 'required|file|mimes:jpg,jpeg,png,webp|max:2048',
|
|
|
+ [
|
|
|
+ '*' => '文件错误'
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+// $result = validate(['file' => ['fileSize' => 10 * 1024 * 1024, 'fileExt' => 'jpg,jpeg,bmp,png,image,gif']])->check(['file' => $file]);
|
|
|
+ break;
|
|
|
+ case 'audio':
|
|
|
+ $check = empty($is_check) ? 'mp3,wma,wav,m4a' : '';
|
|
|
+// $result = validate(['file' => ['fileSize' => 50 * 1024 * 1024, 'fileExt' => $check]])->check(['file' => $file]);
|
|
|
+ break;
|
|
|
+ case 'video':
|
|
|
+// $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]);
|
|
|
+ break;
|
|
|
+ case 'cert':
|
|
|
+// $result = validate(['file' => ['fileSize' => 50 * 1024 * 1024, 'fileExt' => 'cert,pem']])->check(['file' => $file]);
|
|
|
+ break;
|
|
|
+// case 'wxuploadkey':
|
|
|
+// $result = validate(['file' => ['fileSize' => 50 * 1024 * 1024, 'fileExt' => 'cert,pem']])->check(['file' => $file]);
|
|
|
+// break;
|
|
|
+ case 'file':
|
|
|
+// $result = validate(['file' => ['fileSize' => 50 * 1024 * 1024, 'fileExt' => 'doc,xls,docx,xlsx,ppt,pptx,pdf']])->check(['file' => $file]);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if ($validator->fails()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ //本地保存
|
|
|
+ public function fileLoaclSave($path, UploadedFile $file, $file_name = ''): string
|
|
|
+ {
|
|
|
+ if (!$file_name) $file_name = $file->hashName();
|
|
|
+
|
|
|
+// $storagePath = Storage::disk('public')->path($path);
|
|
|
+
|
|
|
+ // 检查文件夹是否存在,如果不存在,则创建文件夹
|
|
|
+ if (!Storage::disk('public')->exists($path)) {
|
|
|
+ Storage::disk('public')->makeDirectory($path);
|
|
|
+ }
|
|
|
+
|
|
|
+// if (!is_dir($storagePath)) {
|
|
|
+// mkdir($storagePath, 0775, true);
|
|
|
+// }
|
|
|
+
|
|
|
+ //保存
|
|
|
+ Storage::disk('public')->putFileAs($path, $file, $file_name);
|
|
|
+ return Storage::url($path . $file_name);
|
|
|
+ }
|
|
|
+
|
|
|
+ //生成返回数据
|
|
|
+ public function fileInfo($path, $file_name, $type_data)
|
|
|
+ {
|
|
|
+ $result = array(
|
|
|
+ 'attachment' => ltrim($path, '/'),
|
|
|
+ 'filename' => $file_name,
|
|
|
+ 'createtime' => time(),
|
|
|
+ 'type' => $type_data
|
|
|
+ );
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+}
|