mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2024-11-01 22:20:30 +08:00
完善文件上传
This commit is contained in:
parent
71fa978de2
commit
f88576613a
@ -88,20 +88,23 @@ export class PluginUpload extends BasePluginHook implements BaseUpload {
|
|||||||
if (_.isEmpty(ctx.files)) {
|
if (_.isEmpty(ctx.files)) {
|
||||||
throw new CoolCommException('上传文件为空');
|
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 file = ctx.files[0];
|
||||||
const extension = file.filename.split('.').pop();
|
const extension = file.filename.split('.').pop();
|
||||||
const name =
|
const name =
|
||||||
moment().format('YYYYMMDD') + '/' + (key || `${uuid()}.${extension}`);
|
moment().format('YYYYMMDD') + '/' + (key || `${uuid()}.${extension}`);
|
||||||
const target = path.join(
|
const target = path.join(basePath, name);
|
||||||
this.app.getBaseDir(),
|
const dirPath = path.join(basePath, moment().format('YYYYMMDD'));
|
||||||
'..',
|
|
||||||
`public/uploads/${name}`
|
|
||||||
);
|
|
||||||
const dirPath = path.join(
|
|
||||||
this.app.getBaseDir(),
|
|
||||||
'..',
|
|
||||||
`public/uploads/${moment().format('YYYYMMDD')}`
|
|
||||||
);
|
|
||||||
if (!fs.existsSync(dirPath)) {
|
if (!fs.existsSync(dirPath)) {
|
||||||
fs.mkdirSync(dirPath);
|
fs.mkdirSync(dirPath);
|
||||||
}
|
}
|
||||||
@ -109,7 +112,8 @@ export class PluginUpload extends BasePluginHook implements BaseUpload {
|
|||||||
fs.writeFileSync(target, data);
|
fs.writeFileSync(target, data);
|
||||||
return domain + '/public/uploads/' + name;
|
return domain + '/public/uploads/' + name;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new CoolCommException('上传失败');
|
console.error(err);
|
||||||
|
throw new CoolCommException('上传失败' + err.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user