作者 lyh

gx

... ... @@ -6,6 +6,7 @@ use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Http\Controllers\type;
use App\Models\File\Image as ImageModel;
use App\Services\CosService;
use App\Services\TencentCosService;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
... ... @@ -139,7 +140,7 @@ class ImageController extends Controller
* @method :post
* @time :2023/6/17 16:30
*/
public function single($files){
public function single($files,$url){
$hash = hash_file('md5', $files->getPathname());
//查看文件是否存在
$imageModel = new ImageModel();
... ... @@ -165,6 +166,8 @@ class ImageController extends Controller
if ($rs === false) {
return $this->response('添加失败', Code::USER_ERROR);
}
$cosService = new CosService();
$cosService->uploadFile($files,$this->path,$fileName);
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
}
... ...
... ... @@ -2,6 +2,7 @@
namespace App\Services;
use Qcloud\Cos\Client;
/**
* @remark :
* @class :CosService.php
... ... @@ -11,4 +12,30 @@ namespace App\Services;
class CosService
{
/**
* @param $file
* @remark :上传图片
* @name :uploadFile
* @author :lyh
* @method :post
* @time :2023/7/19 15:28
*/
public function uploadFile($file,$path,$filename)
{
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
'secretId' => $cos['credentials']['secretId'],
'secretKey' => $cos['credentials']['secretKey'],
],
]);
$key = $path.'/'.$filename;
$cosClient->putObject([
'Bucket' => $cos['bucket'],
'Key' => $key,
'Body' => fopen($file->getRealPath(), 'r'),
]);
return $key;
}
}
... ...