Skip to content

Commit

Permalink
Merge pull request #346 from kodadot/fix--ignore-wsrv-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiival authored Feb 16, 2025
2 parents 65cbdcc + 2941fa4 commit 551c017
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
11 changes: 8 additions & 3 deletions services/image/src/routes/type-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { allowedOrigin, encodeEndpoint } from '@kodadot/workers-utils'
import { CACHE_TTL_BY_STATUS, type Env } from '../utils/constants'
import { getCFIFlexibleVariant, urlToCFI } from '../utils/cloudflare-images'
import { deleteImageByPath, getCFIFlexibleVariant, urlToCFI } from '../utils/cloudflare-images'
import { ResponseType } from '../utils/types'

const app = new Hono<{ Bindings: Env }>()
Expand Down Expand Up @@ -101,9 +101,14 @@ app.delete('/*', async (c) => {
console.log({ objectName })

try {
await c.env.MY_BUCKET.delete(objectName)
const bucket = await c.env.MY_BUCKET.delete(objectName)
const image = await deleteImageByPath({
token: c.env.IMAGE_API_TOKEN,
imageAccount: c.env.CF_IMAGE_ACCOUNT,
path,
})

return c.json({ status: 'ok' })
return c.json({ status: 'ok', bucket, image, path })
} catch (error) {
return c.json({ status: 'error', error }, 500)
}
Expand Down
20 changes: 20 additions & 0 deletions services/image/src/utils/cloudflare-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async function resizeImage(url: string) {
const wsrvnl = new URL('https://wsrv.nl')
wsrvnl.searchParams.append('url', url)
wsrvnl.searchParams.append('w', '1400')
wsrvnl.searchParams.append('data', new Date().toISOString())

console.log(wsrvnl.toString())

Expand Down Expand Up @@ -120,6 +121,25 @@ export async function getImageByPath({
return ''
}

export async function deleteImageByPath({ token, imageAccount, path }: CFImages & { path: string }) {
const deleteImage = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${imageAccount}/images/v1/${path}`,
{
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
},
)

if (deleteImage.ok) {
return true
}

return false
}

const transformationParams = [
'w',
'width',
Expand Down

0 comments on commit 551c017

Please sign in to comment.