From aa449093e1a5869db04c4f700712ce3177e8fa02 Mon Sep 17 00:00:00 2001 From: augushong Date: Tue, 12 Jul 2022 12:47:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=9D=99=E6=80=81=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=8A=9F=E8=83=BD=E5=B9=B6=E5=8E=BB?= =?UTF-8?q?=E9=99=A4=E5=A4=96=E9=83=A8=E4=BE=9D=E8=B5=96=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/command/OssStatic.php | 45 +++++++++++----------- app/common/service/UploadService.php | 56 ++++++++++++++++++++++------ config/filesystem.php | 10 +++++ 3 files changed, 78 insertions(+), 33 deletions(-) diff --git a/app/common/command/OssStatic.php b/app/common/command/OssStatic.php index b6e73ac..4531027 100644 --- a/app/common/command/OssStatic.php +++ b/app/common/command/OssStatic.php @@ -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')); } - -} \ No newline at end of file +} diff --git a/app/common/service/UploadService.php b/app/common/service/UploadService.php index 458169e..450a30f 100644 --- a/app/common/service/UploadService.php +++ b/app/common/service/UploadService.php @@ -13,7 +13,7 @@ class UploadService protected $uploadType = 'local_public'; - public function __construct($upload_type = 'local_public') + public function __construct($upload_type = null) { $uploadConfig = sysconfig('upload'); @@ -56,10 +56,32 @@ class UploadService return $url; } - public function save(File $file, string $save_name = null) + /** + * 存储文件 + * + * @param File $file + * @param string|null $save_name + * @param boolean $force_save 指定$save_name才可以用,设为true强制写入(不报错,如果存在则删除),否则是驱动的默认行为(覆盖、失败、异常) + * @param string $upload_dir + * @param bool $disable_model + * @return mixed + */ + public function save(File $file, string $save_name = null, $force_save = false, $upload_dir = 'upload', $disable_model = false) { - $model_file = new SystemUploadfile(); + $model_file = null; + + if ($force_save && !is_null($save_name)) { + $file_path = $upload_dir . '/' . $save_name; + if (Filesystem::disk($this->uploadType)->has($file_path)) { + $model_file = SystemUploadfile::where('save_name', $save_name)->where('upload_type', $this->uploadType)->find(); + Filesystem::disk($this->uploadType)->delete($file_path); + } + } + + if (empty($model_file)) { + $model_file = new SystemUploadfile(); + } $model_file->upload_type = $this->uploadType; $model_file->file_ext = strtolower($file->extension()); @@ -74,9 +96,21 @@ class UploadService $model_file->mime_type = $file->getMime(); } - $save_name = Filesystem::disk($this->uploadType)->putFile('upload', $file, function () use ($save_name) { + + $save_name = Filesystem::disk($this->uploadType)->putFile($upload_dir, $file, function () use ($save_name,$file) { if (!is_null($save_name)) { - return $save_name; + + $ext_name = $file->extension(); + + if(empty($ext_name)){ + return $save_name; + } + + $list_name = explode('.',$save_name); + + array_pop($list_name); + + return implode('.', $list_name); } return date('Ymd') . '/' . uniqid(); }); @@ -88,13 +122,11 @@ class UploadService $model_file->sha1 = $file->sha1(); - $model_file->file_size = $file->getSize(); - - $model_file->save(); - return [ - 'url' => $url, - 'save_name' => $save_name - ]; + + if (!$disable_model) { + $model_file->save(); + } + return $model_file; } } diff --git a/config/filesystem.php b/config/filesystem.php index 99439cc..83d0be5 100644 --- a/config/filesystem.php +++ b/config/filesystem.php @@ -22,6 +22,16 @@ return [ // 可见性 'visibility' => 'public', ], + 'local_static' => [ + // 磁盘类型 + 'type' => 'local', + // 磁盘路径 + 'root' => app()->getRootPath() . 'public/static', + // 磁盘路径对应的外部URL路径 + 'url' => Request::domain() . '/static', + // 可见性 + 'visibility' => 'public', + ], 'qnoss' => [ 'type' => 'Qiniu' ],