|
...
|
...
|
@@ -6,6 +6,8 @@ use App\Enums\Common\Code; |
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Controllers\type;
|
|
|
|
use App\Models\File\Image as ImageModel;
|
|
|
|
use App\Models\Project\Project;
|
|
|
|
use App\Services\CosService;
|
|
|
|
use App\Services\TencentCosService;
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
...
|
...
|
@@ -26,8 +28,6 @@ class ImageController extends Controller |
|
|
|
'Content-Description' => 'File Transfer',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
const TYPE = 1;
|
|
|
|
|
|
|
|
public $path = '';//路径
|
|
|
|
|
|
|
|
public $config = '';//存储默认配置
|
|
...
|
...
|
@@ -40,11 +40,12 @@ class ImageController extends Controller |
|
|
|
|
|
|
|
public $cache = '';//缓存数据
|
|
|
|
|
|
|
|
public $upload_location = 0;//是否同步到cos
|
|
|
|
public $image_type = [
|
|
|
|
1 => 'image_product',
|
|
|
|
2 => 'image_news',
|
|
|
|
3 => 'image_blog',
|
|
|
|
4 => 'image_other',
|
|
|
|
0 => 'image_other',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct()
|
|
...
|
...
|
@@ -85,7 +86,7 @@ class ImageController extends Controller |
|
|
|
$content = file_get_contents($filename);
|
|
|
|
$header['Content-Length'] = strlen($content);
|
|
|
|
}else{
|
|
|
|
$path = $this->config['url'].'/'.$info['path'];
|
|
|
|
$path = $this->config['root'].'/'.$info['path'];
|
|
|
|
if (!is_file($path)) {
|
|
|
|
$this->response('指定图片已被系统删除!', Code::USER_ERROR);
|
|
|
|
}
|
|
...
|
...
|
@@ -119,6 +120,8 @@ class ImageController extends Controller |
|
|
|
'image.required'=>'图片必须填写',
|
|
|
|
]);
|
|
|
|
$files = $this->request->file('image');
|
|
|
|
$size = $files->getSize();
|
|
|
|
$image_type = $files->getClientOriginalExtension();
|
|
|
|
if (empty($files)) {
|
|
|
|
$this->response('没有上传的文件!', 400);
|
|
|
|
}
|
|
...
|
...
|
@@ -126,8 +129,8 @@ class ImageController extends Controller |
|
|
|
$this->setUrl();
|
|
|
|
if ($type == 'multi') {
|
|
|
|
return $this->multi($files);
|
|
|
|
} else {
|
|
|
|
return $this->single($files);
|
|
|
|
}else{
|
|
|
|
return $this->single($files,$size,$image_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -139,7 +142,7 @@ class ImageController extends Controller |
|
|
|
* @method :post
|
|
|
|
* @time :2023/6/17 16:30
|
|
|
|
*/
|
|
|
|
public function single($files){
|
|
|
|
public function single(&$files,$size,$image_type){
|
|
|
|
$hash = hash_file('md5', $files->getPathname());
|
|
|
|
//查看文件是否存在
|
|
|
|
$imageModel = new ImageModel();
|
|
...
|
...
|
@@ -149,25 +152,42 @@ class ImageController extends Controller |
|
|
|
}
|
|
|
|
$url = $this->config['root'].$this->path;
|
|
|
|
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
|
|
|
|
//同步数据到cos
|
|
|
|
if($this->upload_location == 1){
|
|
|
|
$cosService = new CosService();
|
|
|
|
$cosService->uploadFile($files,$this->path,$fileName);
|
|
|
|
}else{
|
|
|
|
$res = $files->move($url,$fileName);
|
|
|
|
if ($res === false) {
|
|
|
|
return $this->response($files->getError(), Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->saveMysql($imageModel,$size,$image_type,$fileName,$hash);
|
|
|
|
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :保存数据库
|
|
|
|
* @name :saveMysql
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2023/7/19 16:38
|
|
|
|
*/
|
|
|
|
public function saveMysql(&$imageModel,$size,$image_type,$fileName,$hash){
|
|
|
|
$data = [
|
|
|
|
'path' => $this->path.'/'.$fileName,
|
|
|
|
'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
'size' => $res->getSize(),
|
|
|
|
'size' => $size,
|
|
|
|
'hash' => $hash,
|
|
|
|
'type'=>$files->getClientOriginalExtension(),
|
|
|
|
'refer'=>$this->param['refer'] ?? 0,
|
|
|
|
'type'=>$image_type,
|
|
|
|
'refer'=>$this->param['refer'] ?? 1,
|
|
|
|
];
|
|
|
|
$rs = $imageModel->add($data);
|
|
|
|
if ($rs === false) {
|
|
|
|
return $this->response('添加失败', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $info
|
|
|
|
* @param $w
|
|
...
|
...
|
@@ -193,13 +213,12 @@ class ImageController extends Controller |
|
|
|
* @method :post
|
|
|
|
* @time :2023/6/17 16:31
|
|
|
|
*/
|
|
|
|
private function multi($files) {
|
|
|
|
if (!is_array($files)) {
|
|
|
|
$files = [$files];
|
|
|
|
}
|
|
|
|
private function multi(&$files) {
|
|
|
|
$save_data = [];
|
|
|
|
$data = [];
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$size = $file->getSize();
|
|
|
|
$image_type = $file->getClientOriginalExtension();
|
|
|
|
$imageModel = new ImageModel();
|
|
|
|
$hash = hash_file('md5', $file->getPathname());
|
|
|
|
$image_hash = $imageModel->read(['hash'=>$hash]);
|
|
...
|
...
|
@@ -209,18 +228,24 @@ class ImageController extends Controller |
|
|
|
}
|
|
|
|
$url = $this->config['root'].$this->path;
|
|
|
|
$fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();
|
|
|
|
//同步数据到cos
|
|
|
|
if($this->upload_location == 1){
|
|
|
|
$cosService = new CosService();
|
|
|
|
$cosService->uploadFile($file,$this->path,$fileName);
|
|
|
|
}else{
|
|
|
|
$res = $file->move($url,$fileName);
|
|
|
|
if ($res === false) {
|
|
|
|
return $this->response($file->getError(), Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$save_data[] = [
|
|
|
|
'path' => $this->path.'/'.$fileName,
|
|
|
|
'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
'updated_at'=>date('Y-m-d H:i:s',time()),
|
|
|
|
'size' => $res->getSize(),
|
|
|
|
'size' => $size,
|
|
|
|
'hash' => $hash,
|
|
|
|
'type'=>$file->getClientOriginalExtension(),
|
|
|
|
'refer'=>$this->param['refer'] ?? '',
|
|
|
|
'type'=>$image_type,
|
|
|
|
'refer'=>$this->param['refer'] ?? 0,
|
|
|
|
];
|
|
|
|
$data[] = ['image'=>$hash];
|
|
|
|
}
|
|
...
|
...
|
@@ -274,7 +299,8 @@ class ImageController extends Controller |
|
|
|
//获取操作人
|
|
|
|
switch ((string) $k) {
|
|
|
|
case 'image':
|
|
|
|
$data['image_link'] = url('/b/image/' . $v);
|
|
|
|
$cos = new CosService();
|
|
|
|
$data['image_link'] = $cos->getImageUrl('/p/1/image_other/2023-07/64b7abf16721020421.png');
|
|
|
|
break;
|
|
|
|
case 'hash':
|
|
|
|
$data['image_link'] = url('/b/image/' . $v);
|
|
...
|
...
|
@@ -315,6 +341,9 @@ class ImageController extends Controller |
|
|
|
if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
|
|
|
|
$this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
|
|
|
|
}else{
|
|
|
|
$projectModel = new Project();
|
|
|
|
$project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']);
|
|
|
|
$this->upload_location = $project_info['upload_location'];
|
|
|
|
$this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
|
|
|
|
}
|
|
|
|
}
|
...
|
...
|
|