wangqifan vor 3 Jahren
Ursprung
Commit
af7bbcc612
2 geänderte Dateien mit 15 neuen und 19 gelöschten Zeilen
  1. 4 4
      src/chat-pdf/chat-pdf.controller.ts
  2. 11 15
      src/chat-pdf/chat-pdf.service.ts

+ 4 - 4
src/chat-pdf/chat-pdf.controller.ts

@@ -1,4 +1,4 @@
-import { Body, Controller, Get, Post, Render, Req, UploadedFile, UseInterceptors } from '@nestjs/common'
+import { Body, Controller, Get, Post, Render, Req, UploadedFile, UseInterceptors, Param } from '@nestjs/common'
 import { Public } from '../auth/public.decorator'
 import { FileInterceptor } from '@nestjs/platform-express'
 import { ChatPdfService } from './chat-pdf.service'
@@ -8,10 +8,10 @@ export class ChatPdfController {
     constructor(private readonly chatPdfService: ChatPdfService) {}
 
     @Public()
-    @Post('upload')
+    @Post('upload/:code')
     @UseInterceptors(FileInterceptor('file'))
-    public async uploadFile(@UploadedFile() file: Express.Multer.File, @Req() req) {
-        return await this.chatPdfService.upload(file, req.user.id)
+    public async uploadFile(@UploadedFile() file: Express.Multer.File, @Param('code') code: string) {
+        return await this.chatPdfService.upload(file, code)
     }
 
     // @Public()

+ 11 - 15
src/chat-pdf/chat-pdf.service.ts

@@ -73,26 +73,22 @@ export class ChatPdfService {
         this.sequelize.sync()
     }
 
-    public async upload(file: Express.Multer.File, userId: number) {
+    public async upload(file: Express.Multer.File, code: string) {
         const { originalname, buffer, mimetype } = file
         let md5 = this.calculateMD5(buffer)
-        const user = await this.apiUserService.findByUserId(userId)
-        if (!user) {
-            const res = await ChatEmbedding.findAll({
-                where: {
-                    name: md5
-                }
-            })
-            if (res.length) {
-                return {
-                    name: md5
-                }
+        const res = await ChatEmbedding.findAll({
+            where: {
+                name: md5
             }
-        } else {
-            if (!!user.code) {
-                md5 = user.code
+        })
+        if (res.length) {
+            return {
+                name: md5
             }
         }
+        if (!!code) {
+            md5 = code
+        }
         const pdf = await PdfParse(buffer)
         const contents = []
         let paragraph = ''