|
@@ -12,30 +12,38 @@ export class FileService {
|
|
root_path;
|
|
root_path;
|
|
|
|
|
|
async upload(files, fields, route) {
|
|
async upload(files, fields, route) {
|
|
- const path = `${this.root_path}${sep}${route}`;
|
|
|
|
- if (!path) {
|
|
|
|
|
|
+ const dirs = [];
|
|
|
|
+ if (route && route !== '_') {
|
|
|
|
+ const subs = route.split('_');
|
|
|
|
+ dirs.push(...subs);
|
|
|
|
+ }
|
|
|
|
+ const rootPath = `${this.root_path}${sep}`;
|
|
|
|
+ if (!rootPath) {
|
|
throw new ServiceError(
|
|
throw new ServiceError(
|
|
'服务端没有设置存储路径',
|
|
'服务端没有设置存储路径',
|
|
FrameworkErrorEnum.SERVICE_FAULT
|
|
FrameworkErrorEnum.SERVICE_FAULT
|
|
);
|
|
);
|
|
}
|
|
}
|
|
- if (!fs.existsSync(path)) {
|
|
|
|
|
|
+ if (!fs.existsSync(rootPath)) {
|
|
// 如果不存在文件夹,就创建
|
|
// 如果不存在文件夹,就创建
|
|
- this.mkdir(path);
|
|
|
|
|
|
+ this.mkdir(rootPath);
|
|
|
|
+ }
|
|
|
|
+ // TODO: 检查分级目录是否存在,不存在则创建
|
|
|
|
+ for (let i = 0; i < dirs.length; i++) {
|
|
|
|
+ const p = `${rootPath}${sep}${dirs.slice(0, i + 1).join(sep)}`;
|
|
|
|
+ if (!fs.existsSync(p)) {
|
|
|
|
+ this.mkdir(p);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
// TODO: 指定文件名或者按时间生成文件名
|
|
// TODO: 指定文件名或者按时间生成文件名
|
|
// const name = moment().format('YYYYMMDDHHmmss');
|
|
// const name = moment().format('YYYYMMDDHHmmss');
|
|
|
|
+ const path = `${rootPath}${sep}${dirs.join(sep)}${sep}`;
|
|
const filePath = `${path}${sep}`;
|
|
const filePath = `${path}${sep}`;
|
|
for (const val of files) {
|
|
for (const val of files) {
|
|
const filename = moment().format('YYYYMMDDHHmmss');
|
|
const filename = moment().format('YYYYMMDDHHmmss');
|
|
await sharp(val.data)
|
|
await sharp(val.data)
|
|
.toFormat('jpg', { mozjpeg: true })
|
|
.toFormat('jpg', { mozjpeg: true })
|
|
.toFile(`${filePath}${filename}.jpg`);
|
|
.toFile(`${filePath}${filename}.jpg`);
|
|
- const dirs = [];
|
|
|
|
- if (route && route !== '_') {
|
|
|
|
- const subs = route.split('_');
|
|
|
|
- dirs.push(...subs);
|
|
|
|
- }
|
|
|
|
const uri = `/files/${dirs.join('/')}/${filename}.jpg`;
|
|
const uri = `/files/${dirs.join('/')}/${filename}.jpg`;
|
|
return {
|
|
return {
|
|
id: filename,
|
|
id: filename,
|