diff --git a/services/image/src/routes/type-endpoint.ts b/services/image/src/routes/type-endpoint.ts index c741610..17c822c 100644 --- a/services/image/src/routes/type-endpoint.ts +++ b/services/image/src/routes/type-endpoint.ts @@ -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 }>() @@ -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) } diff --git a/services/image/src/utils/cloudflare-images.ts b/services/image/src/utils/cloudflare-images.ts index 2a49a87..8d78a8a 100644 --- a/services/image/src/utils/cloudflare-images.ts +++ b/services/image/src/utils/cloudflare-images.ts @@ -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()) @@ -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',