|
@@ -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
|
} |