作者 lyh

gx

@@ -26,6 +26,8 @@ class ImageController @@ -26,6 +26,8 @@ class ImageController
26 ]; 26 ];
27 public $path = ''; 27 public $path = '';
28 28
  29 + public $config = '';
  30 +
29 public $thr_path = ''; 31 public $thr_path = '';
30 32
31 public $request = ''; 33 public $request = '';
@@ -33,7 +35,8 @@ class ImageController @@ -33,7 +35,8 @@ class ImageController
33 public function __construct(Request $request) 35 public function __construct(Request $request)
34 { 36 {
35 $this->request = $request; 37 $this->request = $request;
36 - $this->path = config('filesystems.disks')['upload']['root'].config('upload'); 38 + $this->config = config('filesystems.disks.upload');
  39 + $this->path = $this->config['root'];
37 } 40 }
38 41
39 public function index($hash = '', $w = 0 ,$h = 0){ 42 public function index($hash = '', $w = 0 ,$h = 0){
@@ -105,13 +108,18 @@ class ImageController @@ -105,13 +108,18 @@ class ImageController
105 */ 108 */
106 public function single($files){ 109 public function single($files){
107 $hash = hash_file('md5', $files->getPathname()); 110 $hash = hash_file('md5', $files->getPathname());
  111 + //查看文件是否存在
  112 + $imageModel = new ImageModel();
  113 + $image_hash = $imageModel->read(['hash'=>$hash]);
  114 + if($image_hash !== false){
  115 + return $hash;
  116 + }
108 $url = $this->path; 117 $url = $this->path;
109 $filename = date('ymdHis').rand(10000,99999); 118 $filename = date('ymdHis').rand(10000,99999);
110 $res = $this->request->file('image')->move($url,$filename); 119 $res = $this->request->file('image')->move($url,$filename);
111 if ($res === false) { 120 if ($res === false) {
112 return $this->response($files->getError(), Code::USER_ERROR); 121 return $this->response($files->getError(), Code::USER_ERROR);
113 } 122 }
114 - $imageModel = new ImageModel();  
115 $data = [ 123 $data = [
116 'path' => $url.$filename, 124 'path' => $url.$filename,
117 'created_at' => date('Y-m-d H:i:s',time()), 125 'created_at' => date('Y-m-d H:i:s',time()),
@@ -151,7 +159,13 @@ class ImageController @@ -151,7 +159,13 @@ class ImageController
151 $save_data = []; 159 $save_data = [];
152 $data = []; 160 $data = [];
153 foreach ($files as $file) { 161 foreach ($files as $file) {
  162 + $imageModel = new ImageModel();
154 $hash = hash_file('md5', $file->getPathname()); 163 $hash = hash_file('md5', $file->getPathname());
  164 + $image_hash = $imageModel->read(['hash'=>$hash]);
  165 + if($image_hash !== false){
  166 + $data[] = $hash;
  167 + continue;
  168 + }
155 $url = $this->path; 169 $url = $this->path;
156 $filename = date('ymdHis').rand(10000,99999); 170 $filename = date('ymdHis').rand(10000,99999);
157 $res = $file->move($url,$filename); 171 $res = $file->move($url,$filename);
@@ -167,7 +181,6 @@ class ImageController @@ -167,7 +181,6 @@ class ImageController
167 ]; 181 ];
168 $data[] = $hash; 182 $data[] = $hash;
169 } 183 }
170 - $imageModel = new ImageModel();  
171 $imageModel->insert($save_data); 184 $imageModel->insert($save_data);
172 return $data; 185 return $data;
173 } 186 }
@@ -5,7 +5,7 @@ namespace App\Http\Logic\Bside; @@ -5,7 +5,7 @@ namespace App\Http\Logic\Bside;
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
6 use App\Exceptions\BsideGlobalException; 6 use App\Exceptions\BsideGlobalException;
7 use App\Helper\Common; 7 use App\Helper\Common;
8 -use App\Http\Controllers\ImageLogic; 8 +use App\Http\Controllers\ImageController;
9 use App\Http\Logic\Logic; 9 use App\Http\Logic\Logic;
10 use App\Models\Image as ImageModel; 10 use App\Models\Image as ImageModel;
11 use Illuminate\Http\Request; 11 use Illuminate\Http\Request;
@@ -99,8 +99,9 @@ class BaseLogic extends Logic @@ -99,8 +99,9 @@ class BaseLogic extends Logic
99 * @method 99 * @method
100 */ 100 */
101 public function upload(){ 101 public function upload(){
102 - $imageLogic = new ImageLogic();  
103 - return $imageLogic->upload(); 102 + $image = new ImageController();
  103 + $hash = $image->upload();
  104 + return $hash;
104 } 105 }
105 106
106 /** 107 /**
1 -<?php  
2 -  
3 -namespace App\Http\Controllers;  
4 -  
5 -use App\Enums\Common\Code;  
6 -use App\Models\Image as ImageModel;  
7 -use Illuminate\Http\Exceptions\HttpResponseException;  
8 -use Illuminate\Http\JsonResponse;  
9 -use Illuminate\Http\Request;  
10 -use Illuminate\Support\Facades\DB;  
11 -use Illuminate\Support\Facades\Storage;  
12 -use Intervention\Image\Facades\Image;  
13 -  
14 -class ImageLogic  
15 -{  
16 - public $path = '';  
17 -  
18 - public $request = '';  
19 -  
20 - public function __construct(Request $request)  
21 - {  
22 - $this->request = $request;  
23 - $this->path = config('filesystems.disks')['upload']['root'].config('upload');  
24 - }  
25 -  
26 - /**  
27 - * 图片上传  
28 - */  
29 - public function upload() {  
30 - $this->request->validate([  
31 - 'image'=>['required'],  
32 - ],[  
33 - 'image.required'=>'图片必须填写',  
34 - ]);  
35 - $files = $this->request->file('image');  
36 - if (empty($files)) {  
37 - $this->response('没有上传的文件!', 400);  
38 - }  
39 - $type = $this->request->post('type', 'single');  
40 - if ($type == 'multi') {  
41 - return $this->multi($files);  
42 - } else {  
43 - return $this->single($files);  
44 - }  
45 - }  
46 - /**  
47 - * @name :上传图片  
48 - * @return void  
49 - * @author :liyuhang  
50 - * @method  
51 - */  
52 - public function single($files){  
53 - $hash = hash_file('md5', $files->getPathname());  
54 - $url = $this->path;  
55 - $filename = date('ymdHis').rand(10000,99999);  
56 - $res = $this->request->file('image')->move($url,$filename);  
57 - if ($res === false) {  
58 - return $this->response($files->getError(), Code::USER_ERROR);  
59 - }  
60 - $imageModel = new ImageModel();  
61 - $data = [  
62 - 'path' => $url.$filename,  
63 - 'created_at' => date('Y-m-d H:i:s',time()),  
64 - 'size' => $res->getSize(),  
65 - 'hash' => $hash.$filename,  
66 - 'type'=>$files->getClientOriginalExtension(),  
67 - ];  
68 - $rs = $imageModel->add($data);  
69 - if ($rs === false) {  
70 - return $this->response('添加失败', Code::USER_ERROR);  
71 - }  
72 - return $hash.$filename;  
73 - }  
74 -  
75 - /**  
76 - * @name :(删除资源及对应表数据)del  
77 - * @author :lyh  
78 - * @method :post  
79 - * @time :2023/5/4 14:57  
80 - */  
81 - public function del($hash){  
82 - DB::beginTransaction();  
83 - $imageModel = new ImageModel();  
84 - try {  
85 - if(is_array($hash)){  
86 - foreach ($hash as $k => $v){  
87 - $this->del($v);  
88 - }  
89 - }else{  
90 - $info = $imageModel->read(['hash'=>$hash]);  
91 - if($info !== false){  
92 - shell_exec('sudo rm -rf '.$info['path']. ' '.$this->path . $info['hash'] .'*');  
93 - }  
94 - }  
95 - $imageModel->del(['hash'=>['in',$hash]]);  
96 - DB::commit();  
97 - }catch (\Exception $e){  
98 - DB::rollBack();  
99 - $this->response('error',Code::USER_ERROR);  
100 - }  
101 - }  
102 - /**  
103 - * 生成缩略图缓存  
104 - * @param type $info  
105 - * @param type $w  
106 - * @param type $h  
107 - * @return string  
108 - */  
109 - private function cacheImage($info, $w, $h) {  
110 - $path = $info['path'];  
111 - $filename = $this->path . $info['hash'] . $w . '_' . $h;  
112 - Image::make($path)->resize($w, $h)->save($filename);  
113 - return $filename;  
114 - }  
115 -  
116 - /**  
117 - * 多图片上传  
118 - * @param type $files file对象集合  
119 - * @return type  
120 - */  
121 - private function multi($files) {  
122 - if (!is_array($files)) {  
123 - $files = [$files];  
124 - }  
125 - $save_data = [];  
126 - $data = [];  
127 - foreach ($files as $file) {  
128 - $hash = hash_file('md5', $file->getPathname());  
129 - $url = $this->path;  
130 - $filename = date('ymdHis').rand(10000,99999);  
131 - $res = $file->move($url,$filename);  
132 - if ($res === false) {  
133 - return $this->response($file->getError(), Code::USER_ERROR);  
134 - }  
135 - $save_data[] = [  
136 - 'path' => $url.$filename,  
137 - 'created_at' => date('Y-m-d H:i:s',time()),  
138 - 'size' => $res->getSize(),  
139 - 'hash' => $hash.$filename,  
140 - 'type'=>$files->getClientOriginalExtension(),  
141 - ];  
142 - $data[] = $hash.$filename;  
143 - }  
144 - $imageModel = new ImageModel();  
145 - $imageModel->insert($save_data);  
146 - return $data;  
147 - }  
148 -  
149 - //下载  
150 - public function download($filename){  
151 - $path = Storage::path($filename);  
152 - return response()->download($path,time().rand(1,100000));  
153 - }  
154 - /**  
155 - * @name 统一返回参数  
156 - * @return JsonResponse  
157 - * @author :liyuhang  
158 - * @method  
159 - */  
160 - public function response($msg = null,string $code = Code::SUCCESS,$data = [],$result_code = 200,$type = 'application/json'): JsonResponse  
161 - {  
162 - $code = Code::fromValue($code);  
163 - $result = [  
164 - 'msg' => $msg == ' ' ? $code->description : $msg,  
165 - 'code' => $code->value,  
166 - 'data' => $data,  
167 - ];  
168 - $this->header['Content-Type'] = $type;  
169 - $response = response($result,$result_code,$this->header);  
170 - throw new HttpResponseException($response);  
171 - }  
172 -}