From 68e8a80665ed52b0683407756c620ddaabc16378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Sat, 31 May 2025 00:10:08 +0800 Subject: [PATCH] ugly fix to keep the path segment on kubernetes TODO: refactor me --- backend/src/graphql/resolvers/images.spec.ts | 17 ++++++++++++++++- backend/src/graphql/resolvers/images.ts | 10 ++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/backend/src/graphql/resolvers/images.spec.ts b/backend/src/graphql/resolvers/images.spec.ts index 5fa68b279..a5c6708ec 100644 --- a/backend/src/graphql/resolvers/images.spec.ts +++ b/backend/src/graphql/resolvers/images.spec.ts @@ -7,7 +7,7 @@ describe('Image', () => { const args = {} const Location = 'https://fsn1.your-objectstorage.com/ocelot-social-staging/original/f965ea15-1f6b-43aa-a535-927410e2585e-dsc02586.jpg' - const config = { + const defaultConfig = { ...TEST_CONFIG, AWS_ENDPOINT: 'https://fsn1.your-objectstorage.com', S3_PUBLIC_GATEWAY: 'https://your-public-gateway.com', @@ -15,15 +15,30 @@ describe('Image', () => { } describe('.url', () => { + const config = { ...defaultConfig } it('replaces the internal domain of the S3 `Location` with a public domain', () => { const expectedUrl = 'https://your-public-gateway.com/f_qz7PlAWIQx-IrMOZfikzDFM6I=/ocelot-social-staging/original/f965ea15-1f6b-43aa-a535-927410e2585e-dsc02586.jpg' expect(Image.url({ url: Location }, args, { config })).toEqual(expectedUrl) }) + + describe('given `S3_PUBLIC_GATEWAY` has a path segment', () => { + const config = { + ...defaultConfig, + S3_PUBLIC_GATEWAY: 'https://your-public-gateway.com/imagor', + } + + it('keeps the path segment', () => { + const expectedUrl = + 'https://your-public-gateway.com/imagor/f_qz7PlAWIQx-IrMOZfikzDFM6I=/ocelot-social-staging/original/f965ea15-1f6b-43aa-a535-927410e2585e-dsc02586.jpg' + expect(Image.url({ url: Location }, args, { config })).toEqual(expectedUrl) + }) + }) }) describe('.transform', () => { describe('resize transformations', () => { + const config = { ...defaultConfig } const args = { width: 320 } it('encodes `fit-in` imagor transformations in the URL', () => { diff --git a/backend/src/graphql/resolvers/images.ts b/backend/src/graphql/resolvers/images.ts index ecdc6674e..90f8385a4 100644 --- a/backend/src/graphql/resolvers/images.ts +++ b/backend/src/graphql/resolvers/images.ts @@ -29,8 +29,14 @@ const changeDomain: (opts: { transformations: UrlResolver[] }) => UrlResolver = const publicUrl = new URL(S3_PUBLIC_GATEWAY) publicUrl.pathname = originalUrl.pathname - const newUrl = publicUrl.href - return chain(...transformations)({ url: newUrl }, _args, context) + let newUrl = publicUrl.href + newUrl = chain(...transformations)({ url: newUrl }, _args, context) + const result = new URL(newUrl) + if (new URL(S3_PUBLIC_GATEWAY).pathname === '/') { + return result.href + } + result.pathname = new URL(S3_PUBLIC_GATEWAY).pathname + result.pathname + return result.href } const sign: UrlResolver = ({ url }, _args, { config: { IMAGOR_SECRET } }) => {