Skip to content
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
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
Loading