Skip to content

Commit

Permalink
🐛 fix: fix image input on pglite (lobehub#5167)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Dec 24, 2024
1 parent df0e0aa commit 5c5b37d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/database/server/models/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export class MessageModel {
// **************** Query *************** //
query = async (
{ current = 0, pageSize = 1000, sessionId, topicId }: QueryMessageParams = {},
options: { postProcessUrl?: (path: string | null) => Promise<string> } = {},
options: {
postProcessUrl?: (path: string | null, file: { fileType: string }) => Promise<string>;
} = {},
): Promise<MessageItem[]> => {
const offset = current * pageSize;

Expand Down Expand Up @@ -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),
})),
);

Expand Down
26 changes: 22 additions & 4 deletions src/services/message/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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[];
}
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 5c5b37d

Please sign in to comment.