发货及后台文件上传优化

This commit is contained in:
Dev
2026-10-14 16:26:41 +08:00
parent 8eb253784a
commit 645a037126
6 changed files with 162 additions and 44 deletions
+5 -28
View File
@@ -4,9 +4,8 @@ namespace app\admin\controller;
use app\common\controller\Backend;
use app\common\exception\UploadException;
use app\common\library\OssUpload;
use app\common\library\Upload;
use app\common\service\OssService;
use fast\Random;
use think\addons\Service;
use think\Cache;
use think\Config;
@@ -143,7 +142,6 @@ class Ajax extends Backend
//必须还原upload配置,否则分片及cdnurl函数计算错误
Config::load(APP_PATH . 'extra/upload.php', 'upload');
$ossService = OssService::instance();
$chunkid = $this->request->post("chunkid");
if ($chunkid) {
if (!Config::get('upload.chunking')) {
@@ -158,15 +156,12 @@ class Ajax extends Backend
$attachment = null;
//合并分片文件
try {
$upload = new Upload();
$upload = new OssUpload();
$attachment = $upload->merge($chunkid, $chunkcount, $filename);
} catch (UploadException $e) {
$this->error($e->getMessage());
}
// 上传到OSS
$objectUrl = 'uploads/' . date('Ymd/') . Random::alnum(16) . '/' . $attachment->filename;
$this->uploadToOss($attachment, $ossService, $objectUrl);
$this->success(__('Uploaded successful'), '', ['url' => cdnurl('/' . $objectUrl), 'fullurl' => cdnurl('/' . $objectUrl)]);
$this->success(__('Uploaded successful'), '', ['url' => cdnurl($attachment->url), 'fullurl' => cdnurl($attachment->url)]);
} elseif ($method == 'clean') {
//删除冗余的分片文件
try {
@@ -193,15 +188,12 @@ class Ajax extends Backend
//默认普通上传文件
$file = $this->request->file('file');
try {
$upload = new Upload($file);
$upload = new OssUpload($file);
$attachment = $upload->upload();
} catch (UploadException $e) {
$this->error($e->getMessage());
}
// 上传到OSS
$objectUrl = 'uploads/' . date('Ymd/') . Random::alnum(16) . '/' . $attachment->filename;
$this->uploadToOss($attachment, $ossService, $objectUrl);
$this->success(__('Uploaded successful'), '', ['url' => cdnurl('/' . $objectUrl), 'fullurl' => cdnurl('/' . $objectUrl)]);
$this->success(__('Uploaded successful'), '', ['url' => cdnurl($attachment->url), 'fullurl' => cdnurl($attachment->url)]);
}
}
@@ -398,19 +390,4 @@ class Ajax extends Backend
return $response;
}
/**
* @param $attachment
* @param OssService $ossService
* @return string
* @throws
*/
protected function uploadToOss($attachment, OssService $ossService, $objectUrl): string
{
$filePath = ROOT_PATH . 'public' . str_replace('/', DS, $attachment->url);
$url = $ossService->uploadFile($filePath, $objectUrl);
@unlink($filePath);
return $url;
}
}