From 5c5b37d7175e3709e83135946e943b923029a65c Mon Sep 17 00:00:00 2001 From: Arvin Xu Date: Tue, 24 Dec 2024 19:32:21 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20fix=20image=20input=20on?= =?UTF-8?q?=20pglite=20(#5167)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/database/server/models/message.ts | 8 ++++++-- src/services/message/client.ts | 26 ++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/database/server/models/message.ts b/src/database/server/models/message.ts index 543b71a742ce..fae31617a644 100644 --- a/src/database/server/models/message.ts +++ b/src/database/server/models/message.ts @@ -48,7 +48,9 @@ export class MessageModel { // **************** Query *************** // query = async ( { current = 0, pageSize = 1000, sessionId, topicId }: QueryMessageParams = {}, - options: { postProcessUrl?: (path: string | null) => Promise } = {}, + options: { + postProcessUrl?: (path: string | null, file: { fileType: string }) => Promise; + } = {}, ): Promise => { const offset = current * pageSize; @@ -130,7 +132,9 @@ export class MessageModel { const relatedFileList = await Promise.all( rawRelatedFileList.map(async (file) => ({ ...file, - url: options.postProcessUrl ? await options.postProcessUrl(file.url) : (file.url as string), + url: options.postProcessUrl + ? await options.postProcessUrl(file.url, file as any) + : (file.url as string), })), ); diff --git a/src/services/message/client.ts b/src/services/message/client.ts index f6ad84d53925..477457e02bbe 100644 --- a/src/services/message/client.ts +++ b/src/services/message/client.ts @@ -5,6 +5,7 @@ import { clientDB } from '@/database/client/db'; import { MessageItem } from '@/database/schemas'; import { MessageModel } from '@/database/server/models/message'; import { BaseClientService } from '@/services/baseClientService'; +import { clientS3Storage } from '@/services/file/ClientS3'; import { ChatMessage, ChatMessageError, @@ -34,10 +35,20 @@ export class ClientService extends BaseClientService implements IMessageService } async getMessages(sessionId: string, topicId?: string) { - const data = await this.messageModel.query({ - sessionId: this.toDbSessionId(sessionId), - topicId, - }); + const data = await this.messageModel.query( + { + sessionId: this.toDbSessionId(sessionId), + topicId, + }, + { + postProcessUrl: async (url, file) => { + const hash = (url as string).replace('client-s3://', ''); + const base64 = await this.getBase64ByFileHash(hash); + + return `data:${file.fileType};base64,${base64}`; + }, + }, + ); return data as unknown as ChatMessage[]; } @@ -115,4 +126,11 @@ export class ClientService extends BaseClientService implements IMessageService private toDbSessionId(sessionId: string | undefined) { return sessionId === INBOX_SESSION_ID ? undefined : sessionId; } + + private async getBase64ByFileHash(hash: string) { + const fileItem = await clientS3Storage.getObject(hash); + if (!fileItem) throw new Error('file not found'); + + return Buffer.from(await fileItem.arrayBuffer()).toString('base64'); + } }