完成静态文件上传功能并去除外部依赖;

This commit is contained in:
2022-07-12 12:47:32 +08:00
parent 4ab8630617
commit aa449093e1
3 changed files with 78 additions and 33 deletions

View File

@@ -3,14 +3,12 @@
namespace app\common\command;
use EasyAdmin\console\CliEcho;
use EasyAdmin\tool\CommonTool;
use EasyAdmin\upload\driver\alioss\Oss;
use app\common\service\UploadService;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\facade\Filesystem;
use think\File;
class OssStatic extends Command
{
@@ -24,26 +22,31 @@ class OssStatic extends Command
protected function execute(Input $input, Output $output)
{
$output->writeln("========正在上传静态资源到OSS上========" . date('Y-m-d H:i:s'));
$dir = root_path() . 'public' . DIRECTORY_SEPARATOR . 'static';
$list = CommonTool::readDirAllFiles($dir);
$uploadConfig = sysconfig('upload');
$list = Filesystem::disk('local_static')->listContents('/', true);
$upload_service = new UploadService();
$uploadPrefix = config('app.oss_static_prefix', 'oss_static_prefix');
foreach ($list as $key => $val) {
list($objectName, $filePath) = [$uploadPrefix . DIRECTORY_SEPARATOR . $key, $val];
try {
$upload = Oss::instance($uploadConfig)
->save($objectName, $filePath);
} catch (\Exception $e) {
CliEcho::error('文件上传失败:' . $filePath . '。错误信息:' . $e->getMessage());
foreach ($list as $file_item) {
if ($file_item['type'] != 'file') {
continue;
}
if ($upload['save'] == true) {
CliEcho::success('文件上传成功:' . $filePath . '。上传地址:' . $upload['url']);
} else {
CliEcho::error('文件上传失败:' . $filePath . '。错误信息:' . $upload['msg']);
$file_path = $file_item['path'];
$file_path = Filesystem::disk('local_static')->path($file_path);
$file = new File($file_path, false);
$save_name = $file_item['path'];
try {
$model_file = $upload_service->save($file, $save_name, true, $uploadPrefix, true);
$output->info('文件上传成功:' . $save_name . '。上传地址:' . $model_file['url']);
} catch (\Throwable $th) {
$output->error('文件上传失败:' . $save_name . '。错误信息:' . $th->getMessage());
}
}
$output->writeln("========已完成静态资源上传到OSS上========" . date('Y-m-d H:i:s'));
}
}
}