From 784771465ab1c07019df638ba2f495a930ca3b6a Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sat, 19 Sep 2026 15:57:16 +0200 Subject: [PATCH] fix: serve giphy media in the format its id asks for --- src/proxy.ts | 7 ++++--- test/proxy.spec.ts | 13 ++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/proxy.ts b/src/proxy.ts index d513c01..4e4df9c 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -89,13 +89,14 @@ function buildMultipartRedirect(targetLocation: string): Response { /** * proxy a giphy call - * example id: yXPquATCb8kGk + * example id: yXPquATCb8kGk.gif * - * @param {string} id the id of the giphy media + * @param {string} id the id of the giphy media, with the extension giphy picks the format from * @return {*} {Response} the multipart response to return to the requesting homeserver */ function proxyGiphy(id: string): Response { - return buildMultipartRedirect(`https://i.giphy.com/${id}.webp`); + const file = /\.[a-z0-9]+$/i.test(id) ? id : `${id}.gif`; + return buildMultipartRedirect(`https://i.giphy.com/${file}`); } function proxyTenor(id: string): Response { diff --git a/test/proxy.spec.ts b/test/proxy.spec.ts index b0069ec..3a0f67e 100644 --- a/test/proxy.spec.ts +++ b/test/proxy.spec.ts @@ -28,9 +28,16 @@ describe('proxyMediaCall', () => { // to ensure it has the Location part needed for forwarding expect(body).toContain('Location:'); - expect(body).toContain('https://i.giphy.com/yXPquATCb8kGk.webp'); + expect(body).toContain('https://i.giphy.com/yXPquATCb8kGk.gif'); - expect(body).not.toContain('https://i.giphy.com/giphy_yXPquATCb8kGk.webp'); + expect(body).not.toContain('https://i.giphy.com/giphy_yXPquATCb8kGk.gif'); + }); + it('keeps the extension the id carries, and defaults to gif without one', async () => { + const withFormat = await proxyMediaCall('giphy_eVhQcXVBVENiOGtHay53ZWJw'); + expect(await withFormat.text()).toContain('https://i.giphy.com/yXPquATCb8kGk.webp'); + + const withoutFormat = await proxyMediaCall('giphy_eVhQcXVBVENiOGtHaw'); + expect(await withoutFormat.text()).toContain('https://i.giphy.com/yXPquATCb8kGk.gif'); }); it('creates a valid multipart redirect response for giphy', async () => { const response = await proxyMediaCall('giphy_eVhQcXVBVENiOGtHaw'); @@ -50,7 +57,7 @@ describe('proxyMediaCall', () => { `{}\r\n` + `--${boundary}\r\n` + `Content-Type: application/octet-stream\r\n` + - `Location: https://i.giphy.com/yXPquATCb8kGk.webp\r\n` + + `Location: https://i.giphy.com/yXPquATCb8kGk.gif\r\n` + `\r\n` + `\r\n` + `--${boundary}--\r\n`;