Przeglądaj źródła

实现文件上传并创建记录的功能,更新记录控制器以支持文件上传,修改记录实体以适应描述字段类型,调整路由以集成新上传接口。

wui 7 miesięcy temu
rodzic
commit
aacaa30614

+ 59 - 0
src/controllers/records.controller.ts

@@ -1,5 +1,6 @@
 import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
 import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
 import { RecordsService } from '../services/records.service'
 import { RecordsService } from '../services/records.service'
+import { FileService } from '../services/file.service'
 import {
 import {
   ListRecordsQuery,
   ListRecordsQuery,
   CreateRecordsBody,
   CreateRecordsBody,
@@ -8,9 +9,67 @@ import {
 
 
 export class RecordsController {
 export class RecordsController {
   private recordsService: RecordsService
   private recordsService: RecordsService
+  private fileService: FileService
 
 
   constructor(app: FastifyInstance) {
   constructor(app: FastifyInstance) {
     this.recordsService = new RecordsService(app)
     this.recordsService = new RecordsService(app)
+    this.fileService = new FileService(app)
+  }
+
+  /**
+   * 上传文件并创建记录
+   */
+  async uploadAndCreate(request: FastifyRequest, reply: FastifyReply) {
+    try {
+      const data = await request.file()
+      
+      if (!data) {
+        return reply.code(400).send({ message: '请选择要上传的文件' })
+      }
+
+      const buffer = await data.toBuffer()
+      const filename = data.filename
+      const mimeType = data.mimetype
+      
+      // 从表单字段中获取描述
+      const description = (data.fields?.description as any)?.value || ''
+
+      // 上传文件到OSS
+      const uploadResult = await this.fileService.uploadFile(buffer, filename, mimeType, {
+        folder: 'tweb',
+        maxSize: 50 * 1024 * 1024 // 50MB
+      })
+
+      // 创建记录
+      const record = await this.recordsService.create(uploadResult.url, description)
+
+      return reply.code(201).send({
+        message: '文件上传并创建记录成功',
+        data: {
+          record: {
+            id: record.id,
+            url: record.url,
+            description: record.description,
+            createdAt: record.createdAt,
+            updatedAt: record.updatedAt
+          },
+          file: {
+            filename: filename,
+            originalName: filename,
+            url: uploadResult.url,
+            key: uploadResult.key,
+            size: uploadResult.size,
+            mimeType: uploadResult.mimeType,
+            dateFolder: new Date().toISOString().split('T')[0]
+          }
+        }
+      })
+    } catch (error) {
+      return reply.code(500).send({
+        message: '文件上传并创建记录失败',
+        error: error instanceof Error ? error.message : '未知错误'
+      })
+    }
   }
   }
 
 
   async create(request: FastifyRequest<{ Body: CreateRecordsBody }>, reply: FastifyReply) {
   async create(request: FastifyRequest<{ Body: CreateRecordsBody }>, reply: FastifyReply) {

+ 1 - 1
src/entities/records.entity.ts

@@ -8,7 +8,7 @@ export class Records {
   @Column({ length: 1024 })
   @Column({ length: 1024 })
   url: string
   url: string
 
 
-  @Column()
+  @Column({ type: 'text' })
   description: string
   description: string
 
 
   @CreateDateColumn()
   @CreateDateColumn()

+ 0 - 1
src/routes/file.routes.ts

@@ -15,7 +15,6 @@ export default async function fileRoutes(fastify: FastifyInstance) {
   // 上传文件
   // 上传文件
   fastify.post(
   fastify.post(
     '/upload',
     '/upload',
-    { onRequest: [authenticate] },
     fileController.uploadFile.bind(fileController)
     fileController.uploadFile.bind(fileController)
   )
   )
 
 

+ 6 - 0
src/routes/records.routes.ts

@@ -7,6 +7,12 @@ import { UserRole } from '../entities/user.entity'
 export default async function recordsRoutes(fastify: FastifyInstance) {
 export default async function recordsRoutes(fastify: FastifyInstance) {
   const recordsController = new RecordsController(fastify)
   const recordsController = new RecordsController(fastify)
 
 
+  // 上传文件并创建记录
+  fastify.post(
+    '/upload',
+    recordsController.uploadAndCreate.bind(recordsController)
+  )
+
   // 创建记录
   // 创建记录
   fastify.post<{ Body: CreateRecordsBody }>(
   fastify.post<{ Body: CreateRecordsBody }>(
     '/',
     '/',