Ver Fonte

音频下载,返回

lrf há 1 ano atrás
pai
commit
55b9bf746f
2 ficheiros alterados com 34 adições e 1 exclusões
  1. 3 0
      src/config/config.default.ts
  2. 31 1
      src/controller/home.controller.ts

+ 3 - 0
src/config/config.default.ts

@@ -37,4 +37,7 @@ export default {
   rabbitmq: {
     url: 'amqp://visit:visit@127.0.0.1/visit',
   },
+  upload: {
+    whitelist: null,
+  },
 } as MidwayConfig;

+ 31 - 1
src/controller/home.controller.ts

@@ -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 };
+  }
 }