|
@@ -1,9 +1,20 @@
|
|
|
-import { Body, Controller, Get, Inject, Post } from '@midwayjs/core';
|
|
|
+import { Body, Config, Controller, Fields, Files, Get, Inject, Post } from '@midwayjs/core';
|
|
|
import { UtilService } from '../service/util.service';
|
|
|
import { LoginType } from '../interface/login.interface';
|
|
|
+import { Context } from '@midwayjs/koa';
|
|
|
+import { FileService, FrameworkErrorEnum, ServiceError } from 'free-midway-component';
|
|
|
+import { join, sep } from 'path';
|
|
|
|
|
|
@Controller('/')
|
|
|
export class HomeController {
|
|
|
+ @Inject()
|
|
|
+ ctx: Context;
|
|
|
+ @Config('koa.globalPrefix')
|
|
|
+ globalPrefix: string;
|
|
|
+ @Config('upload.realdir')
|
|
|
+ uploadDir;
|
|
|
+ @Inject()
|
|
|
+ fileService: FileService;
|
|
|
@Get('/')
|
|
|
async home(): Promise<string> {
|
|
|
return 'Hello Midwayjs!';
|
|
@@ -15,4 +26,23 @@ export class HomeController {
|
|
|
const res = await this.utilService.checkAccountIsSame('test', LoginType.Admin);
|
|
|
return res;
|
|
|
}
|
|
|
+ @Post('/files/chat_voice/:speakerId/upload')
|
|
|
+ async upload(@Files() files, @Fields() fields) {
|
|
|
+ const { speakerId } = this.ctx.params;
|
|
|
+ if (!speakerId) throw new ServiceError('缺少讲话人信息!', FrameworkErrorEnum.BAD_PARAMS);
|
|
|
+ const dirs = ['chat_voice', speakerId];
|
|
|
+ let path = this.uploadDir;
|
|
|
+ // 检查分级目录是否存在,不存在则创建
|
|
|
+ for (let i = 0; i < dirs.length; i++) {
|
|
|
+ const p = `${path}${sep}${dirs.slice(0, i + 1).join(sep)}`;
|
|
|
+ this.fileService.mkdir(p);
|
|
|
+ }
|
|
|
+ path = `${join(path, dirs.join(sep))}${sep}`;
|
|
|
+ const file = files[0];
|
|
|
+ const ext = '.mp3';
|
|
|
+ const filename = `${this.fileService.getNowDateTime()}${ext}`;
|
|
|
+ const uri = `${this.globalPrefix}/files/${dirs.join('/')}/${filename}`;
|
|
|
+ this.fileService.moveFile(file.data, `${path}${filename}`);
|
|
|
+ return { id: filename, name: filename, uri };
|
|
|
+ }
|
|
|
}
|