作者 lyh

gx

@@ -7,6 +7,7 @@ use App\Models\File\File; @@ -7,6 +7,7 @@ use App\Models\File\File;
7 use App\Models\File\Image as ImageModel; 7 use App\Models\File\Image as ImageModel;
8 use Illuminate\Http\Exceptions\HttpResponseException; 8 use Illuminate\Http\Exceptions\HttpResponseException;
9 use Illuminate\Http\JsonResponse; 9 use Illuminate\Http\JsonResponse;
  10 +use Illuminate\Support\Facades\Cache;
10 11
11 class FileController 12 class FileController
12 { 13 {
@@ -21,20 +22,42 @@ class FileController @@ -21,20 +22,42 @@ class FileController
21 'Content-Description' => 'File Transfer', 22 'Content-Description' => 'File Transfer',
22 ], 23 ],
23 ]; 24 ];
24 - public $path = '';  
25 25
26 - public $config = ''; 26 + public $path = '';//路径
27 27
28 - public $uploads = ''; 28 + public $config = '';//存储默认配置
29 29
30 - public $request = ''; 30 + public $request = '';//request
31 31
  32 + public $param = '';//参数
  33 +
  34 + public $token = '';//token
  35 +
  36 + public $cache = '';//缓存数据
  37 +
  38 + public $image_type = [
  39 + 1 => 'file_other',
  40 + ];
32 public function __construct() 41 public function __construct()
33 { 42 {
34 $this->request = request(); 43 $this->request = request();
  44 + $this->param = $this->request->all();
35 $this->config = config('filesystems.disks.upload'); 45 $this->config = config('filesystems.disks.upload');
36 $this->uploads = config('upload.default_file'); 46 $this->uploads = config('upload.default_file');
37 - $this->path = $this->config['root'].$this->uploads['path'].'/'; 47 + $this->token = $this->request->header('token');
  48 + $this->cache = Cache::get($this->token);
  49 + if(!isset($this->token) || empty($this->cache)){
  50 + $this->response('请登录后上传',Code::USER_LOGIN_ERROE);
  51 + }
  52 + //上传路径设置
  53 + if(!isset($this->param['image_type'])){
  54 + $this->response('类型不能为空',Code::USER_ERROR);
  55 + }
  56 + if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
  57 + $this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['file_type']].'/'.date('Y-m');
  58 + }else{
  59 + $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['file_type']].'/'.date('Y-m');
  60 + }
38 } 61 }
39 62
40 /** 63 /**
@@ -44,7 +67,7 @@ class FileController @@ -44,7 +67,7 @@ class FileController
44 * @method :post 67 * @method :post
45 * @time :2023/5/9 9:15 68 * @time :2023/5/9 9:15
46 */ 69 */
47 - public function index($hash = '', $w = 1) 70 + public function index($hash = '')
48 { 71 {
49 // 检查是否有修改日期或ETag头部 72 // 检查是否有修改日期或ETag头部
50 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) { 73 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
@@ -56,7 +79,7 @@ class FileController @@ -56,7 +79,7 @@ class FileController
56 if ($info === false) { 79 if ($info === false) {
57 $this->response('指定文件不存在!', Code::USER_ERROR); 80 $this->response('指定文件不存在!', Code::USER_ERROR);
58 } 81 }
59 - $path = $this->path.$info['path']; 82 + $path = $this->config['url'].'/'.$info['path'];
60 if (!is_file($path)) { 83 if (!is_file($path)) {
61 $this->response('指定文件已被系统删除!', Code::USER_ERROR); 84 $this->response('指定文件已被系统删除!', Code::USER_ERROR);
62 } 85 }
@@ -122,12 +145,12 @@ class FileController @@ -122,12 +145,12 @@ class FileController
122 ],[ 145 ],[
123 'file.required'=>'必须填写', 146 'file.required'=>'必须填写',
124 ]); 147 ]);
125 - $files = $this->request->file('file'); 148 + $files = $this->param['file'];
126 149
127 if (empty($files)) { 150 if (empty($files)) {
128 $this->response('没有上传的文件!', 400); 151 $this->response('没有上传的文件!', 400);
129 } 152 }
130 - $type = $this->request->post('type', 'single'); 153 + $type = $this->param['type'];
131 if ($type == 'multi') { 154 if ($type == 'multi') {
132 return $this->multi($files); 155 return $this->multi($files);
133 } else { 156 } else {
@@ -151,14 +174,14 @@ class FileController @@ -151,14 +174,14 @@ class FileController
151 if($file_hash !== false){ 174 if($file_hash !== false){
152 return $this->response('资源',Code::SUCCESS,['file'=>$hash]); 175 return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
153 } 176 }
154 - $url = $this->path; 177 + $url = $this->config['url'].'/'.$this->path;
155 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension(); 178 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
156 $res = $files->move($url,$fileName); 179 $res = $files->move($url,$fileName);
157 if ($res === false) { 180 if ($res === false) {
158 return $this->response($files->getError(), Code::USER_ERROR); 181 return $this->response($files->getError(), Code::USER_ERROR);
159 } 182 }
160 $data = [ 183 $data = [
161 - 'path' => $fileName, 184 + 'path' => $this->path.'/'.$fileName,
162 'created_at' => date('Y-m-d H:i:s',time()), 185 'created_at' => date('Y-m-d H:i:s',time()),
163 'size' => $res->getSize(), 186 'size' => $res->getSize(),
164 'hash' => $hash, 187 'hash' => $hash,
@@ -193,14 +216,14 @@ class FileController @@ -193,14 +216,14 @@ class FileController
193 $data[] = $hash; 216 $data[] = $hash;
194 continue; 217 continue;
195 } 218 }
196 - $url = $this->path; 219 + $url = $this->config['url'].'/'.$this->path;
197 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension(); 220 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
198 $res = $file->move($url,$fileName); 221 $res = $file->move($url,$fileName);
199 if ($res === false) { 222 if ($res === false) {
200 return $this->response($file->getError(), Code::USER_ERROR); 223 return $this->response($file->getError(), Code::USER_ERROR);
201 } 224 }
202 $save_data[] = [ 225 $save_data[] = [
203 - 'path' => $fileName, 226 + 'path' => $this->path.'/'.$fileName,
204 'created_at' => date('Y-m-d H:i:s',time()), 227 'created_at' => date('Y-m-d H:i:s',time()),
205 'size' => $res->getSize(), 228 'size' => $res->getSize(),
206 'hash' => $hash, 229 'hash' => $hash,
@@ -281,7 +304,7 @@ class FileController @@ -281,7 +304,7 @@ class FileController
281 if ($info === false) { 304 if ($info === false) {
282 $this->response('指定文件不存在!', Code::USER_ERROR); 305 $this->response('指定文件不存在!', Code::USER_ERROR);
283 } 306 }
284 - $path = $info['path']; 307 + $path = $this->config['url'].'/'.$info['path'];
285 if (!is_file($path)) { 308 if (!is_file($path)) {
286 $this->response('指定文件已被系统删除!', Code::USER_ERROR); 309 $this->response('指定文件已被系统删除!', Code::USER_ERROR);
287 } 310 }
@@ -3,14 +3,16 @@ @@ -3,14 +3,16 @@
3 namespace App\Http\Controllers\File; 3 namespace App\Http\Controllers\File;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
  6 +use App\Http\Controllers\Controller;
6 use App\Http\Controllers\type; 7 use App\Http\Controllers\type;
7 use App\Models\File\Image as ImageModel; 8 use App\Models\File\Image as ImageModel;
8 use Illuminate\Http\Exceptions\HttpResponseException; 9 use Illuminate\Http\Exceptions\HttpResponseException;
9 use Illuminate\Http\JsonResponse; 10 use Illuminate\Http\JsonResponse;
  11 +use Illuminate\Support\Facades\Cache;
10 use Illuminate\Support\Facades\Storage; 12 use Illuminate\Support\Facades\Storage;
11 use Intervention\Image\Facades\Image; 13 use Intervention\Image\Facades\Image;
12 14
13 -class ImageController 15 +class ImageController extends Controller
14 { 16 {
15 public $upload_img = [ 17 public $upload_img = [
16 //设置静态缓存参数(304) 18 //设置静态缓存参数(304)
@@ -25,20 +27,45 @@ class ImageController @@ -25,20 +27,45 @@ class ImageController
25 ]; 27 ];
26 const TYPE = 1; 28 const TYPE = 1;
27 29
28 - public $path = ''; 30 + public $path = '';//路径
29 31
30 - public $config = ''; 32 + public $config = '';//存储默认配置
31 33
32 - public $thr_path = ''; 34 + public $request = '';//request
33 35
34 - public $request = ''; 36 + public $param = '';//参数
  37 +
  38 + public $token = '';//token
  39 +
  40 + public $cache = '';//缓存数据
  41 +
  42 + public $image_type = [
  43 + 1 => 'image_product',
  44 + 2 => 'image_news',
  45 + 3 => 'image_blog',
  46 + 4 => 'image_other',
  47 + ];
35 48
36 public function __construct() 49 public function __construct()
37 { 50 {
38 $this->request = request(); 51 $this->request = request();
  52 + $this->token = $this->request->header('token');
  53 + $this->cache = Cache::get($this->token);
  54 + if(!isset($this->token) || empty($this->cache)){
  55 + $this->response('请登录后上传',Code::USER_LOGIN_ERROE);
  56 + }
  57 + $this->param = $this->request->all();
39 $this->config = config('filesystems.disks.upload'); 58 $this->config = config('filesystems.disks.upload');
40 $this->uploads = config('upload.default_image'); 59 $this->uploads = config('upload.default_image');
41 - $this->path = $this->config['root'].$this->uploads['path'].'/'; 60 + //上传路径设置
  61 + if(!isset($this->param['image_type'])){
  62 + $this->response('类型不能为空',Code::USER_ERROR);
  63 + }
  64 + if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
  65 + $this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['image_type']].'/'.date('Y-m');
  66 + }else{
  67 + $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['image_type']].'/'.date('Y-m');
  68 + }
42 } 69 }
43 70
44 /** 71 /**
@@ -61,7 +88,7 @@ class ImageController @@ -61,7 +88,7 @@ class ImageController
61 $this->response('指定图片不存在!', Code::USER_ERROR); 88 $this->response('指定图片不存在!', Code::USER_ERROR);
62 } 89 }
63 //查看缩略图是否存在 90 //查看缩略图是否存在
64 - $filename = $this->path . $info['hash'] . $w . '_' . $h; 91 + $filename = $this->config['url'].'/'.$this->path . '/' . $info['hash'] . $w . '_' . $h;
65 if(is_file($filename)){ 92 if(is_file($filename)){
66 $last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT"; 93 $last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";
67 $header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'], 94 $header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'],
@@ -69,7 +96,7 @@ class ImageController @@ -69,7 +96,7 @@ class ImageController
69 $content = file_get_contents($filename); 96 $content = file_get_contents($filename);
70 $header['Content-Length'] = strlen($content); 97 $header['Content-Length'] = strlen($content);
71 }else{ 98 }else{
72 - $path = $this->path.$info['path']; 99 + $path = $this->config['url'].'/'.$info['path'];
73 if (!is_file($path)) { 100 if (!is_file($path)) {
74 $this->response('指定图片已被系统删除!', Code::USER_ERROR); 101 $this->response('指定图片已被系统删除!', Code::USER_ERROR);
75 } 102 }
@@ -106,7 +133,7 @@ class ImageController @@ -106,7 +133,7 @@ class ImageController
106 if (empty($files)) { 133 if (empty($files)) {
107 $this->response('没有上传的文件!', 400); 134 $this->response('没有上传的文件!', 400);
108 } 135 }
109 - $type = $this->request->post('type', 'single'); 136 + $type = $this->param['type'];
110 if ($type == 'multi') { 137 if ($type == 'multi') {
111 return $this->multi($files); 138 return $this->multi($files);
112 } else { 139 } else {
@@ -130,19 +157,19 @@ class ImageController @@ -130,19 +157,19 @@ class ImageController
130 if($image_hash !== false){ 157 if($image_hash !== false){
131 return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]); 158 return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
132 } 159 }
133 - $url = $this->path; 160 + $url = $this->config['url'].$this->path;
134 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension(); 161 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
135 $res = $files->move($url,$fileName); 162 $res = $files->move($url,$fileName);
136 if ($res === false) { 163 if ($res === false) {
137 return $this->response($files->getError(), Code::USER_ERROR); 164 return $this->response($files->getError(), Code::USER_ERROR);
138 } 165 }
139 $data = [ 166 $data = [
140 - 'path' => $fileName, 167 + 'path' => $this->path.'/'.$fileName,
141 'created_at' => date('Y-m-d H:i:s',time()), 168 'created_at' => date('Y-m-d H:i:s',time()),
142 'size' => $res->getSize(), 169 'size' => $res->getSize(),
143 'hash' => $hash, 170 'hash' => $hash,
144 'type'=>$files->getClientOriginalExtension(), 171 'type'=>$files->getClientOriginalExtension(),
145 - 'refer'=>$this->request->post('refer') ?? '', 172 + 'refer'=>$this->param['refer'] ?? '',
146 ]; 173 ];
147 $rs = $imageModel->add($data); 174 $rs = $imageModel->add($data);
148 if ($rs === false) { 175 if ($rs === false) {
@@ -163,7 +190,7 @@ class ImageController @@ -163,7 +190,7 @@ class ImageController
163 */ 190 */
164 private function cacheImage($info, $w, $h) { 191 private function cacheImage($info, $w, $h) {
165 $path = $info['path']; 192 $path = $info['path'];
166 - $filename = $this->path . $info['hash'] . $w . '_' . $h; 193 + $filename = $this->config['url'] . $this->path . $info['hash'] . $w . '_' . $h;
167 Image::make($path)->resize($w, $h)->save($filename); 194 Image::make($path)->resize($w, $h)->save($filename);
168 return $filename; 195 return $filename;
169 } 196 }
@@ -191,20 +218,20 @@ class ImageController @@ -191,20 +218,20 @@ class ImageController
191 $data[] = ['image'=>$hash]; 218 $data[] = ['image'=>$hash];
192 continue; 219 continue;
193 } 220 }
194 - $url = $this->path; 221 + $url = $this->config['url'].$this->path;
195 $fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension(); 222 $fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();
196 $res = $file->move($url,$fileName); 223 $res = $file->move($url,$fileName);
197 if ($res === false) { 224 if ($res === false) {
198 return $this->response($file->getError(), Code::USER_ERROR); 225 return $this->response($file->getError(), Code::USER_ERROR);
199 } 226 }
200 $save_data[] = [ 227 $save_data[] = [
201 - 'path' => $fileName, 228 + 'path' => $this->path.'/'.$fileName,
202 'created_at' => date('Y-m-d H:i:s',time()), 229 'created_at' => date('Y-m-d H:i:s',time()),
203 'updated_at'=>date('Y-m-d H:i:s',time()), 230 'updated_at'=>date('Y-m-d H:i:s',time()),
204 'size' => $res->getSize(), 231 'size' => $res->getSize(),
205 'hash' => $hash, 232 'hash' => $hash,
206 'type'=>$file->getClientOriginalExtension(), 233 'type'=>$file->getClientOriginalExtension(),
207 - 'refer'=>$this->request->post('refer') ?? '', 234 + 'refer'=>$this->param['refer'] ?? '',
208 ]; 235 ];
209 $data[] = ['image'=>$hash]; 236 $data[] = ['image'=>$hash];
210 } 237 }
@@ -284,4 +311,5 @@ class ImageController @@ -284,4 +311,5 @@ class ImageController
284 $lists = $imageModel->list([],$order = 'id',['id','hash','type','path','created_at']); 311 $lists = $imageModel->list([],$order = 'id',['id','hash','type','path','created_at']);
285 $this->response('success',Code::SUCCESS,$lists); 312 $this->response('success',Code::SUCCESS,$lists);
286 } 313 }
  314 +
287 } 315 }
@@ -67,7 +67,20 @@ return [ @@ -67,7 +67,20 @@ return [
67 'visibility' => 'public', 67 'visibility' => 'public',
68 ], 68 ],
69 69
70 - 70 + //腾讯云对象存储
  71 + 'cos' => [
  72 + 'driver' => 'cosv5',
  73 + 'region' => env('COS_REGION'),
  74 + 'credentials' => [
  75 + 'appId' => env('COS_APP_ID'),
  76 + 'secretId' => env('COS_SECRET_ID'),
  77 + 'secretKey' => env('COS_SECRET_KEY'),
  78 + ],
  79 + 'bucket' => env('COS_BUCKET'),
  80 + 'cdn' => env('COS_CDN'),
  81 + 'timeout' => 60,
  82 + 'connect_timeout' => 60,
  83 + ],
71 84
72 85
73 ], 86 ],
@@ -19,34 +19,15 @@ return [ @@ -19,34 +19,15 @@ return [
19 'size' => [ 19 'size' => [
20 'max' => 1024*1024*2, // 2M 20 'max' => 1024*1024*2, // 2M
21 ], 21 ],
22 - 'path' => '/images' 22 + 'path_b' => '/p',
  23 + 'path_a' => '/m',
23 ], 24 ],
24 //默认视频 25 //默认视频
25 'default_file' =>[ 26 'default_file' =>[
26 'size' => [ 27 'size' => [
27 'max' => 1024*1024*20, // 2M 28 'max' => 1024*1024*20, // 2M
28 ], 29 ],
29 - 'path' => '/files'  
30 - ],  
31 - //博客图  
32 - 'blog' =>[  
33 - 'size' => [  
34 - 'max' => 1024*1024*2, // 2M  
35 - ],  
36 - 'path' => '/blog'  
37 - ],  
38 - //新闻图  
39 - 'news' =>[  
40 - 'size' => [  
41 - 'max' => 1024*1024*2, // 2M  
42 - ],  
43 - 'path' => '/news'  
44 - ],  
45 - //项目相关  
46 - 'project' =>[  
47 - 'size' => [  
48 - 'max' => 1024*1024*1024, // 2M  
49 - ],  
50 - 'path' => '/project' 30 + 'path_b' => '/p',
  31 + 'path_a' => '/m',
51 ], 32 ],
52 ]; 33 ];