Skip to content

Commit 551c017

Browse files
authored
Merge pull request #346 from kodadot/fix--ignore-wsrv-cache
2 parents 65cbdcc + 2941fa4 commit 551c017

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

services/image/src/routes/type-endpoint.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Hono } from 'hono'
22
import { cors } from 'hono/cors'
33
import { allowedOrigin, encodeEndpoint } from '@kodadot/workers-utils'
44
import { CACHE_TTL_BY_STATUS, type Env } from '../utils/constants'
5-
import { getCFIFlexibleVariant, urlToCFI } from '../utils/cloudflare-images'
5+
import { deleteImageByPath, getCFIFlexibleVariant, urlToCFI } from '../utils/cloudflare-images'
66
import { ResponseType } from '../utils/types'
77

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

103103
try {
104-
await c.env.MY_BUCKET.delete(objectName)
104+
const bucket = await c.env.MY_BUCKET.delete(objectName)
105+
const image = await deleteImageByPath({
106+
token: c.env.IMAGE_API_TOKEN,
107+
imageAccount: c.env.CF_IMAGE_ACCOUNT,
108+
path,
109+
})
105110

106-
return c.json({ status: 'ok' })
111+
return c.json({ status: 'ok', bucket, image, path })
107112
} catch (error) {
108113
return c.json({ status: 'error', error }, 500)
109114
}

services/image/src/utils/cloudflare-images.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ async function resizeImage(url: string) {
1616
const wsrvnl = new URL('https://wsrv.nl')
1717
wsrvnl.searchParams.append('url', url)
1818
wsrvnl.searchParams.append('w', '1400')
19+
wsrvnl.searchParams.append('data', new Date().toISOString())
1920

2021
console.log(wsrvnl.toString())
2122

@@ -120,6 +121,25 @@ export async function getImageByPath({
120121
return ''
121122
}
122123

124+
export async function deleteImageByPath({ token, imageAccount, path }: CFImages & { path: string }) {
125+
const deleteImage = await fetch(
126+
`https://api.cloudflare.com/client/v4/accounts/${imageAccount}/images/v1/${path}`,
127+
{
128+
method: 'DELETE',
129+
headers: {
130+
'Content-Type': 'application/json',
131+
Authorization: `Bearer ${token}`,
132+
},
133+
},
134+
)
135+
136+
if (deleteImage.ok) {
137+
return true
138+
}
139+
140+
return false
141+
}
142+
123143
const transformationParams = [
124144
'w',
125145
'width',

0 commit comments

Comments
 (0)