# All files / modules/audioedit audioedit.controller.ts

0% Statements 0/74
0% Branches 0/1
0% Functions 0/1
0% Lines 0/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103

     import Router from '@koa/router';
    import { audioEditService } from './audioedit.service';

    const router = new Router();

    /**
     * 裁剪音频
     * POST /api/audio/trim
     */
    router.post('/trim', async (ctx) => {
      try {
        const { audioId, startTime, endTime } = ctx.request.body as {
          audioId: string;
          startTime: number;
          endTime: number;
        };

        if (!audioId || startTime === undefined || endTime === undefined) {
          ctx.body = {
            code: 400,
            message: '缺少必填字段',
          };
          return;
        }

        if (startTime >= endTime) {
          ctx.body = {
            code: 400,
            message: '起始时间必须小于结束时间',
          };
          return;
        }

        const result = await audioEditService.trimAudio(audioId, startTime, endTime);
        ctx.body = {
          code: 0,
          message: 'success',
          data: result,
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '裁剪失败',
        };
      }
    });

    /**
     * 合并音频
     * POST /api/audio/merge
     */
    router.post('/merge', async (ctx) => {
      try {
        const { audioIds, order } = ctx.request.body as {
          audioIds: string[];
          order?: number[];
        };

        if (!audioIds || audioIds.length < 2) {
          ctx.body = {
            code: 400,
            message: '至少需要两个音频才能合并',
          };
          return;
        }

        const mergeOrder = order || audioIds.map((_, i) => i);
        const result = await audioEditService.mergeAudios(audioIds, mergeOrder);
        ctx.body = {
          code: 0,
          message: 'success',
          data: result,
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '合并失败',
        };
      }
    });

    /**
     * 获取音频信息
     * GET /api/audio/:audioId/info
     */
    router.get('/:audioId/info', async (ctx) => {
      try {
        const { audioId } = ctx.params;
        const result = await audioEditService.getAudioInfo(audioId);
        ctx.body = {
          code: 0,
          message: 'success',
          data: result,
        };
      } catch (error: any) {
        ctx.body = {
          code: 400,
          message: error.message || '获取音频信息失败',
        };
      }
    });

    export default router;