Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(image): cfi flexible variants #313

Merged
merged 3 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions services/image/src/routes/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import { etag } from 'hono/etag'
import { allowedOrigin } from '@kodadot/workers-utils'
import { CACHE_DAY, CACHE_MONTH, Env } from '../utils/constants'
import { fetchIPFS, toIpfsGw } from '../utils/ipfs'
import { getImageByPath, ipfsToCFI } from '../utils/cloudflare-images'
import {
getCFIFlexibleVariant,
getImageByPath,
ipfsToCFI,
} from '../utils/cloudflare-images'
import type { ResponseType } from '../utils/types'

const app = new Hono<{ Bindings: Env }>()

app.use(etag())
app.use('/*', cors({ origin: allowedOrigin }))
app.get('/*', async (c) => {
const { original } = c.req.query()
const query = c.req.query()
const { original } = query
const isOriginal = original === 'true'
const isHead = c.req.method === 'HEAD'

Expand Down Expand Up @@ -48,7 +53,7 @@ app.get('/*', async (c) => {
})

if (publicUrl) {
return c.redirect(publicUrl, 301)
return c.redirect(getCFIFlexibleVariant(query, publicUrl), 301)
}
}

Expand All @@ -63,7 +68,7 @@ app.get('/*', async (c) => {
})

if (imageUrl) {
return c.redirect(imageUrl, 301)
return c.redirect(getCFIFlexibleVariant(query, imageUrl), 301)
}
}

Expand Down
13 changes: 13 additions & 0 deletions services/image/src/tests/ipfs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,16 @@
}
`)
})

test('ipfs - flexible variant - image ', async () => {
const res = await fetch(
'https://image-beta.w.kodadot.xyz/ipfs/bafybeidv3wgydacgpre67lkciihrttvwl5nibzftxfppy6lfanjja4v7zm?w=100',
{ redirect: 'manual' },
)

const redirectURL = res.headers.get('location')

expect(redirectURL).toBe(

Check failure on line 155 in services/image/src/tests/ipfs.test.ts

View workflow job for this annotation

GitHub Actions / test-image-workers

src/tests/ipfs.test.ts > ipfs - flexible variant - image

AssertionError: expected 'https://imagedelivery.net/jk5b6spi_m_…' to be 'https://imagedelivery.net/jk5b6spi_m_…' // Object.is equality - Expected + Received - https://imagedelivery.net/jk5b6spi_m_-9qC4VTnjpg/bafybeidv3wgydacgpre67lkciihrttvwl5nibzftxfppy6lfanjja4v7zm/w=100 + https://imagedelivery.net/jk5b6spi_m_-9qC4VTnjpg/bafybeidv3wgydacgpre67lkciihrttvwl5nibzftxfppy6lfanjja4v7zm/public ❯ src/tests/ipfs.test.ts:155:23
'https://imagedelivery.net/jk5b6spi_m_-9qC4VTnjpg/bafybeidv3wgydacgpre67lkciihrttvwl5nibzftxfppy6lfanjja4v7zm/w=100',
)
})
14 changes: 14 additions & 0 deletions services/image/src/tests/utils/cloudflare-images.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, test } from 'vitest'
import { getCFIFlexibleVariant } from '../../utils/cloudflare-images'

test('utils getCFIFlexibleVariant', async () => {
const url =
'https://imagedelivery.net/jk5b6spi_m_-9qC4VTnjpg/bafybeiak6zlsmrhder2epl7qzxcpq6zqt6razp7wl66balp33nfno2fu7u/public'

expect(getCFIFlexibleVariant({ w: '100' }, url).endsWith('/w=100')).toBe(true)
expect(
getCFIFlexibleVariant({ w: '100', blur: '50' }, url).endsWith(
'/w=100,blur=50',
),
).toBe(true)
})
38 changes: 38 additions & 0 deletions services/image/src/utils/cloudflare-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,41 @@ export async function getImageByPath({

return ''
}

const transformationParams = [
'w',
'width',
'h',
'height',
'anim',
'background',
'blur',
'brightness',
'fit',
'fromat',
'q',
'quality',
'sharpen',
'trim.width',
'trim.height',
'trim.left',
'trim.top',
]

export function getCFIFlexibleVariant(
queryParams: { [param: string]: string },
publicUrl: string,
): string {
const transformations = Object.keys(queryParams)
.filter((param) => transformationParams.includes(param))
.map((param) => `${param}=${queryParams[param]}`)
.join(',')

if (!transformations) {
return publicUrl
}

publicUrl = publicUrl.split('/public')[0]

return `${publicUrl}/${transformations}`
}
Loading