|
|
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); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
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; |
|
|
|