Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): save an archived image under its own name

Yudhi Armyndharis53687b5dc14f
diff --git a/dashboard/src/components/chats/ArchivedMediaPreview.test.ts b/dashboard/src/components/chats/ArchivedMediaPreview.test.ts
index 7e38f461b..d685f5445 100644
--- a/dashboard/src/components/chats/ArchivedMediaPreview.test.ts
+++ b/dashboard/src/components/chats/ArchivedMediaPreview.test.ts
@@ -165,3 +165,43 @@ test('the lazy-load observer measures against the thread scroll container, so it
delete target.IntersectionObserver;
}
});
+
+test('the viewer downloads under the media file name, or one unique to the message when it has none', async () => {
+ const downloads: string[] = [];
+ // The library saves through a detached <a download> it clicks with dispatchEvent, never in the DOM.
+ // jsdom elements inherit from jsdom's own EventTarget, so the spy goes on the element prototype.
+ const anchorProto = HTMLAnchorElement.prototype as { dispatchEvent?: unknown };
+ anchorProto.dispatchEvent = function (this: HTMLAnchorElement, event: Event): boolean {
+ if (event.type === 'click') downloads.push(this.download);
+ return true;
+ };
+ try {
+ for (const props of [
+ { id: 'msg-1', filename: 'holiday.jpg', alt: 'The sea at dusk' },
+ { id: 'msg-2', alt: 'Image' },
+ ]) {
+ const { container } = rtl.render(
+ createElement(ArchivedMediaPreview, {
+ kind: 'image',
+ fallback,
+ load: async () => new Blob(['x'], { type: 'image/jpeg' }),
+ ...props,
+ }),
+ );
+ const img = await rtl.waitFor(() => {
+ const found = container.querySelector('img.chat-image-media');
+ assert.ok(found, 'the archived image did not render');
+ return found as HTMLImageElement;
+ });
+ rtl.fireEvent.click(img);
+ rtl.fireEvent.click(await rtl.screen.findByRole('button', { name: 'Download' }));
+ // The library clicks its link from a timer once the blob is fetched.
+ await rtl.act(() => new Promise(resolve => setTimeout(resolve, 50)));
+ rtl.cleanup();
+ }
+ // A caption in the alt text must never become the saved name: every photo would save as the same word.
+ assert.deepEqual(downloads, ['holiday.jpg', 'image-msg-2.jpg']);
+ } finally {
+ delete anchorProto.dispatchEvent;
+ }
+});
diff --git a/dashboard/src/components/chats/ArchivedMediaPreview.tsx b/dashboard/src/components/chats/ArchivedMediaPreview.tsx
index 2d5caa187..4c6a8542a 100644
--- a/dashboard/src/components/chats/ArchivedMediaPreview.tsx
+++ b/dashboard/src/components/chats/ArchivedMediaPreview.tsx
@@ -9,6 +9,10 @@ interface ArchivedMediaPreviewProps {
/** Fetches the archived bytes (the per-message media route). Called at most once per mount. */
load: (signal: AbortSignal) => Promise<Blob>;
alt?: string;
+ /** The media's own file name. `alt` is only a caption, never a download name. */
+ filename?: string;
+ /** Names the viewer's download (image-<id>.jpg) when the media has no file name; unique per message. */
+ id?: string;
/**
* The thread's scroll container. The lazy-load observer measures against it: with the implicit viewport
* root the margin below is clipped to the container's own box and the lead never happens.
@@ -37,6 +41,8 @@ function ArchivedMediaPreview({
kind,
load,
alt,
+ filename,
+ id,
scrollRootRef,
fallback,
measureMedia,
@@ -140,7 +146,7 @@ function ArchivedMediaPreview({
/>
</div>
<MediaLightbox
- items={[{ id: 'archived-image', url: state.url, alt, filename: alt }]}
+ items={[{ id: id ?? 'archived-image', url: state.url, alt, filename }]}
index={imageOpen ? 0 : null}
onClose={() => setImageOpen(false)}
onNavigate={() => {}}
diff --git a/dashboard/src/components/chats/ChatThread.tsx b/dashboard/src/components/chats/ChatThread.tsx
index 4ffcce9a8..da9b9feb2 100644
--- a/dashboard/src/components/chats/ChatThread.tsx
+++ b/dashboard/src/components/chats/ChatThread.tsx
@@ -354,6 +354,8 @@ function ChatThread({
kind={previewKind}
load={signal => sessionApi.getMessageMediaBlob(sessionId, activeChat.id, waMessageId, signal)}
alt={mediaInfo.filename || t('chats.media.image')}
+ filename={mediaInfo.filename}
+ id={msg.id}
scrollRootRef={messagesContainerRef}
fallback={downloadButton}
measureMedia={measureMedia}