File size: 1,657 Bytes
f6d320c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22ae2e3
f6d320c
 
 
 
 
 
 
 
 
 
 
 
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
import { Context } from 'koa';
import Router from 'koa-router';

import { handleControllerError } from '@/middleware';
import { getGeneratedFile } from '@/service';
import { downloadDocx } from '@/utils';

const router = new Router({ prefix: '/static' });
/** 图片 */
router.get('/image/:filename', async (ctx: Context) => {
  const filename = ctx.params.filename as string;

  try {
    const result = await getGeneratedFile(filename, 'image/jpeg');
    ctx.type = result.type;
    ctx.body = result.stream;
  } catch (error) {
    // 将捕获到的异常传递给统一的错误处理中间件
    await handleControllerError(ctx, error);
  }
});

/** docx */
router.get('/docx/:filename', async (ctx: Context) => {
  const filename = ctx.params.filename as string;

  try {
    const result = await getGeneratedFile(filename, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    ctx.type = result.type;
    ctx.body = result.stream;
  } catch (error) {
    // 将捕获到的异常传递给统一的错误处理中间件
    await handleControllerError(ctx, error);
  }
});

router.get('/download/docx/:filename', async (ctx: Context) => {
  const filename = ctx.params.filename as string;
  const fullUrl = `https://joey7938-joey.hf.space${ctx.originalUrl}`;

  try {
    await downloadDocx(fullUrl.replace(/\/download/, ''), filename);
    ctx.type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
    ctx.body = '下载成功';
  } catch (error) {
    // 将捕获到的异常传递给统一的错误处理中间件
    await handleControllerError(ctx, error);
  }
});

export default router;