作者 lyh

gx

... ... @@ -7,6 +7,7 @@ use App\Models\File\File;
use App\Models\File\Image as ImageModel;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
class FileController
{
... ... @@ -21,20 +22,42 @@ class FileController
'Content-Description' => 'File Transfer',
],
];
public $path = '';
public $config = '';
public $path = '';//路径
public $uploads = '';
public $config = '';//存储默认配置
public $request = '';
public $request = '';//request
public $param = '';//参数
public $token = '';//token
public $cache = '';//缓存数据
public $image_type = [
1 => 'file_other',
];
public function __construct()
{
$this->request = request();
$this->param = $this->request->all();
$this->config = config('filesystems.disks.upload');
$this->uploads = config('upload.default_file');
$this->path = $this->config['root'].$this->uploads['path'].'/';
$this->token = $this->request->header('token');
$this->cache = Cache::get($this->token);
if(!isset($this->token) || empty($this->cache)){
$this->response('请登录后上传',Code::USER_LOGIN_ERROE);
}
//上传路径设置
if(!isset($this->param['image_type'])){
$this->response('类型不能为空',Code::USER_ERROR);
}
if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
$this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['file_type']].'/'.date('Y-m');
}else{
$this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['file_type']].'/'.date('Y-m');
}
}
/**
... ... @@ -44,7 +67,7 @@ class FileController
* @method :post
* @time :2023/5/9 9:15
*/
public function index($hash = '', $w = 1)
public function index($hash = '')
{
// 检查是否有修改日期或ETag头部
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
... ... @@ -56,7 +79,7 @@ class FileController
if ($info === false) {
$this->response('指定文件不存在!', Code::USER_ERROR);
}
$path = $this->path.$info['path'];
$path = $this->config['url'].'/'.$info['path'];
if (!is_file($path)) {
$this->response('指定文件已被系统删除!', Code::USER_ERROR);
}
... ... @@ -122,12 +145,12 @@ class FileController
],[
'file.required'=>'必须填写',
]);
$files = $this->request->file('file');
$files = $this->param['file'];
if (empty($files)) {
$this->response('没有上传的文件!', 400);
}
$type = $this->request->post('type', 'single');
$type = $this->param['type'];
if ($type == 'multi') {
return $this->multi($files);
} else {
... ... @@ -151,14 +174,14 @@ class FileController
if($file_hash !== false){
return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
}
$url = $this->path;
$url = $this->config['url'].'/'.$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
$res = $files->move($url,$fileName);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
}
$data = [
'path' => $fileName,
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => $hash,
... ... @@ -193,14 +216,14 @@ class FileController
$data[] = $hash;
continue;
}
$url = $this->path;
$url = $this->config['url'].'/'.$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
$res = $file->move($url,$fileName);
if ($res === false) {
return $this->response($file->getError(), Code::USER_ERROR);
}
$save_data[] = [
'path' => $fileName,
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => $hash,
... ... @@ -281,7 +304,7 @@ class FileController
if ($info === false) {
$this->response('指定文件不存在!', Code::USER_ERROR);
}
$path = $info['path'];
$path = $this->config['url'].'/'.$info['path'];
if (!is_file($path)) {
$this->response('指定文件已被系统删除!', Code::USER_ERROR);
}
... ...
... ... @@ -3,14 +3,16 @@
namespace App\Http\Controllers\File;
use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Http\Controllers\type;
use App\Models\File\Image as ImageModel;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
class ImageController
class ImageController extends Controller
{
public $upload_img = [
//设置静态缓存参数(304)
... ... @@ -25,20 +27,45 @@ class ImageController
];
const TYPE = 1;
public $path = '';
public $path = '';//路径
public $config = '';
public $config = '';//存储默认配置
public $thr_path = '';
public $request = '';//request
public $request = '';
public $param = '';//参数
public $token = '';//token
public $cache = '';//缓存数据
public $image_type = [
1 => 'image_product',
2 => 'image_news',
3 => 'image_blog',
4 => 'image_other',
];
public function __construct()
{
$this->request = request();
$this->token = $this->request->header('token');
$this->cache = Cache::get($this->token);
if(!isset($this->token) || empty($this->cache)){
$this->response('请登录后上传',Code::USER_LOGIN_ERROE);
}
$this->param = $this->request->all();
$this->config = config('filesystems.disks.upload');
$this->uploads = config('upload.default_image');
$this->path = $this->config['root'].$this->uploads['path'].'/';
//上传路径设置
if(!isset($this->param['image_type'])){
$this->response('类型不能为空',Code::USER_ERROR);
}
if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
$this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['image_type']].'/'.date('Y-m');
}else{
$this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['image_type']].'/'.date('Y-m');
}
}
/**
... ... @@ -61,7 +88,7 @@ class ImageController
$this->response('指定图片不存在!', Code::USER_ERROR);
}
//查看缩略图是否存在
$filename = $this->path . $info['hash'] . $w . '_' . $h;
$filename = $this->config['url'].'/'.$this->path . '/' . $info['hash'] . $w . '_' . $h;
if(is_file($filename)){
$last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";
$header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'],
... ... @@ -69,7 +96,7 @@ class ImageController
$content = file_get_contents($filename);
$header['Content-Length'] = strlen($content);
}else{
$path = $this->path.$info['path'];
$path = $this->config['url'].'/'.$info['path'];
if (!is_file($path)) {
$this->response('指定图片已被系统删除!', Code::USER_ERROR);
}
... ... @@ -106,7 +133,7 @@ class ImageController
if (empty($files)) {
$this->response('没有上传的文件!', 400);
}
$type = $this->request->post('type', 'single');
$type = $this->param['type'];
if ($type == 'multi') {
return $this->multi($files);
} else {
... ... @@ -130,19 +157,19 @@ class ImageController
if($image_hash !== false){
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
}
$url = $this->path;
$url = $this->config['url'].$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
$res = $files->move($url,$fileName);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
}
$data = [
'path' => $fileName,
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => $hash,
'type'=>$files->getClientOriginalExtension(),
'refer'=>$this->request->post('refer') ?? '',
'refer'=>$this->param['refer'] ?? '',
];
$rs = $imageModel->add($data);
if ($rs === false) {
... ... @@ -163,7 +190,7 @@ class ImageController
*/
private function cacheImage($info, $w, $h) {
$path = $info['path'];
$filename = $this->path . $info['hash'] . $w . '_' . $h;
$filename = $this->config['url'] . $this->path . $info['hash'] . $w . '_' . $h;
Image::make($path)->resize($w, $h)->save($filename);
return $filename;
}
... ... @@ -191,20 +218,20 @@ class ImageController
$data[] = ['image'=>$hash];
continue;
}
$url = $this->path;
$url = $this->config['url'].$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();
$res = $file->move($url,$fileName);
if ($res === false) {
return $this->response($file->getError(), Code::USER_ERROR);
}
$save_data[] = [
'path' => $fileName,
'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(),
'hash' => $hash,
'type'=>$file->getClientOriginalExtension(),
'refer'=>$this->request->post('refer') ?? '',
'refer'=>$this->param['refer'] ?? '',
];
$data[] = ['image'=>$hash];
}
... ... @@ -284,4 +311,5 @@ class ImageController
$lists = $imageModel->list([],$order = 'id',['id','hash','type','path','created_at']);
$this->response('success',Code::SUCCESS,$lists);
}
}
... ...
... ... @@ -67,7 +67,20 @@ return [
'visibility' => 'public',
],
//腾讯云对象存储
'cos' => [
'driver' => 'cosv5',
'region' => env('COS_REGION'),
'credentials' => [
'appId' => env('COS_APP_ID'),
'secretId' => env('COS_SECRET_ID'),
'secretKey' => env('COS_SECRET_KEY'),
],
'bucket' => env('COS_BUCKET'),
'cdn' => env('COS_CDN'),
'timeout' => 60,
'connect_timeout' => 60,
],
],
... ...
... ... @@ -19,34 +19,15 @@ return [
'size' => [
'max' => 1024*1024*2, // 2M
],
'path' => '/images'
'path_b' => '/p',
'path_a' => '/m',
],
//默认视频
'default_file' =>[
'size' => [
'max' => 1024*1024*20, // 2M
],
'path' => '/files'
],
//博客图
'blog' =>[
'size' => [
'max' => 1024*1024*2, // 2M
],
'path' => '/blog'
],
//新闻图
'news' =>[
'size' => [
'max' => 1024*1024*2, // 2M
],
'path' => '/news'
],
//项目相关
'project' =>[
'size' => [
'max' => 1024*1024*1024, // 2M
],
'path' => '/project'
'path_b' => '/p',
'path_a' => '/m',
],
];
... ...