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;