Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat(channel): return the recorded ThumbHash with Channel attachments (#23665)

Arvin Xuc27f54226263
diff --git a/packages/database/src/repositories/channel/__tests__/attachments.test.ts b/packages/database/src/repositories/channel/__tests__/attachments.test.ts
index 57723ebce80..977ef448988 100644
--- a/packages/database/src/repositories/channel/__tests__/attachments.test.ts
+++ b/packages/database/src/repositories/channel/__tests__/attachments.test.ts
@@ -106,17 +106,18 @@ describe('channel message attachments', () => {
});
// Mobile sized an image's box only after fetching it, so the row grew and the feed jumped.
- it("returns an image attachment's recorded pixel size, and nothing else from its metadata", async () => {
+ it("returns an image attachment's recorded pixel size and ThumbHash, and nothing else from its metadata", async () => {
const { asMember, channel } = await fixture.createTeamChannel();
const shot = await createFile('shot.png', 'image/png', {
filename: 'shot.png',
height: 2622,
path: 'files/private/shot.png',
ratio: 1206 / 2622,
+ thumbhash: 'HBkSHYSIeHiPiHh8eJd4eTN0EEQG',
width: 1206,
});
const legacy = await createFile('old.png', 'image/png', { path: 'files/old.png' });
- const half = await createFile('half.png', 'image/png', { width: 10 });
+ const half = await createFile('half.png', 'image/png', { thumbhash: '<script>', width: 10 });
const text = await createFile('a.txt');
await asMember.send(channel.id, {
content: '',
@@ -134,11 +135,12 @@ describe('channel message attachments', () => {
id: shot,
name: 'shot.png',
size: 1,
+ thumbhash: 'HBkSHYSIeHiPiHh8eJd4eTN0EEQG',
url: 'files/shot.png',
width: 1206,
});
for (const id of [legacy, half, text]) {
- expect(byId.get(id)).toMatchObject({ height: null, width: null });
+ expect(byId.get(id)).toMatchObject({ height: null, thumbhash: null, width: null });
}
});
});
diff --git a/packages/database/src/repositories/channel/attachments.ts b/packages/database/src/repositories/channel/attachments.ts
index 4fb1f4ff2d9..cc757420dda 100644
--- a/packages/database/src/repositories/channel/attachments.ts
+++ b/packages/database/src/repositories/channel/attachments.ts
@@ -55,11 +55,16 @@ export const fileIdsByMessage = async (
const pixels = (value: unknown) =>
typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.round(value) : null;
+/** A base64 ThumbHash from caller-written JSON metadata, or `null`. A real one is ~25-35 chars. */
+const thumbhashOf = (value: unknown) =>
+ typeof value === 'string' && /^[\d+/A-Za-z]{8,64}={0,2}$/.test(value) ? value : null;
+
/**
* The attachments a Channel shows, by file id. `width` / `height` are the picture's intrinsic
* pixels as the uploader recorded them in `metadata`, so a client can size the picture's box
* before fetching it; `null` when unknown (not an image, or an upload that did not record them).
- * Nothing else in `metadata` is returned: it also holds the storage path.
+ * `thumbhash` is the base64 ThumbHash recorded at upload, painted as a blurred preview while the
+ * picture loads; `null` when unknown. Nothing else in `metadata` is returned: it also holds the storage path.
*/
export const channelAttachments = async (tx: LobeChatDatabase | Transaction, ids: string[]) => {
if (!ids.length) return [];
@@ -76,10 +81,15 @@ export const channelAttachments = async (tx: LobeChatDatabase | Transaction, ids
.from(files)
.where(inArray(files.id, ids));
return rows.map(({ metadata, ...file }) => {
- const meta = (metadata ?? {}) as { height?: unknown; width?: unknown };
+ const meta = (metadata ?? {}) as { height?: unknown; thumbhash?: unknown; width?: unknown };
const [width, height] = [pixels(meta.width), pixels(meta.height)];
// Half a size reserves nothing: report both or neither.
const known = width !== null && height !== null;
- return { ...file, height: known ? height : null, width: known ? width : null };
+ return {
+ ...file,
+ height: known ? height : null,
+ thumbhash: thumbhashOf(meta.thumbhash),
+ width: known ? width : null,
+ };
});
};