From f88576613a8b27659043e4b15b58383aeac863f3 Mon Sep 17 00:00:00 2001 From: cool Date: Thu, 28 Mar 2024 15:11:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/plugin/hooks/upload/index.ts | 26 ++++++++++++++---------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/modules/plugin/hooks/upload/index.ts b/src/modules/plugin/hooks/upload/index.ts index b8b24fe..003bec7 100644 --- a/src/modules/plugin/hooks/upload/index.ts +++ b/src/modules/plugin/hooks/upload/index.ts @@ -88,20 +88,23 @@ export class PluginUpload extends BasePluginHook implements BaseUpload { if (_.isEmpty(ctx.files)) { throw new CoolCommException('上传文件为空'); } + // 检查public/uploads文件夹是否存在,不存在则创建 + const basePath = path.join( + this.app.getBaseDir(), + '..', + 'public', + 'uploads' + ); + if (!fs.existsSync(basePath)) { + fs.mkdirSync(basePath); + } + const file = ctx.files[0]; const extension = file.filename.split('.').pop(); const name = moment().format('YYYYMMDD') + '/' + (key || `${uuid()}.${extension}`); - const target = path.join( - this.app.getBaseDir(), - '..', - `public/uploads/${name}` - ); - const dirPath = path.join( - this.app.getBaseDir(), - '..', - `public/uploads/${moment().format('YYYYMMDD')}` - ); + const target = path.join(basePath, name); + const dirPath = path.join(basePath, moment().format('YYYYMMDD')); if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath); } @@ -109,7 +112,8 @@ export class PluginUpload extends BasePluginHook implements BaseUpload { fs.writeFileSync(target, data); return domain + '/public/uploads/' + name; } catch (err) { - throw new CoolCommException('上传失败'); + console.error(err); + throw new CoolCommException('上传失败' + err.message); } } }