|
|
@@ -0,0 +1,67 @@
|
|
|
+import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi';
|
|
|
+import { FileService } from '@/server/modules/files/file.service';
|
|
|
+import { ErrorSchema } from '@/server/utils/errorHandler';
|
|
|
+import { AppDataSource } from '@/server/data-source';
|
|
|
+import { AuthContext } from '@/server/types/context';
|
|
|
+import { authMiddleware } from '@/server/middleware/auth.middleware';
|
|
|
+
|
|
|
+// 获取文件下载URL路由
|
|
|
+const downloadFileRoute = createRoute({
|
|
|
+ method: 'get',
|
|
|
+ path: '/{id}/download',
|
|
|
+ middleware: [authMiddleware],
|
|
|
+ request: {
|
|
|
+ params: z.object({
|
|
|
+ id: z.coerce.number().openapi({
|
|
|
+ param: { name: 'id', in: 'path' },
|
|
|
+ example: 1,
|
|
|
+ description: '文件ID'
|
|
|
+ })
|
|
|
+ })
|
|
|
+ },
|
|
|
+ responses: {
|
|
|
+ 200: {
|
|
|
+ description: '获取文件下载URL成功',
|
|
|
+ content: {
|
|
|
+ 'application/json': {
|
|
|
+ schema: z.object({
|
|
|
+ url: z.string().url().openapi({
|
|
|
+ description: '文件下载URL(带Content-Disposition头)',
|
|
|
+ example: 'https://minio.example.com/bucket/file-key?response-content-disposition=attachment%3B%20filename%3D%22example.jpg%22'
|
|
|
+ }),
|
|
|
+ filename: z.string().openapi({
|
|
|
+ description: '原始文件名',
|
|
|
+ example: 'example.jpg'
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ 404: {
|
|
|
+ description: '文件不存在',
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
+ },
|
|
|
+ 500: {
|
|
|
+ description: '服务器错误',
|
|
|
+ content: { 'application/json': { schema: ErrorSchema } }
|
|
|
+ }
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+// 创建文件服务实例
|
|
|
+const fileService = new FileService(AppDataSource);
|
|
|
+
|
|
|
+// 创建路由实例
|
|
|
+const app = new OpenAPIHono<AuthContext>().openapi(downloadFileRoute, async (c) => {
|
|
|
+ try {
|
|
|
+ const { id } = c.req.valid('param');
|
|
|
+ const result = await fileService.getFileDownloadUrl(id);
|
|
|
+ return c.json(result, 200);
|
|
|
+ } catch (error) {
|
|
|
+ const message = error instanceof Error ? error.message : '获取文件下载URL失败';
|
|
|
+ const code = (error instanceof Error && error.message === '文件不存在') ? 404 : 500;
|
|
|
+ return c.json({ code, message }, code);
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+export default app;
|