From 87cce46592d06e0d9e9584ae31f8f2f7d2447e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cosmin=20P=C3=A2rvulescu?= Date: Mon, 23 Oct 2023 11:10:23 +0300 Subject: [PATCH] chore(platform): delete old images on upload of new ones --- .../app/routes/apps/$clientId/designer.tsx | 11 ++++++ platform/images/src/jsonrpc/methods/delete.ts | 36 +++++++++++++++++++ platform/images/src/jsonrpc/router.ts | 11 ++++++ 3 files changed, 58 insertions(+) create mode 100644 platform/images/src/jsonrpc/methods/delete.ts diff --git a/apps/console/app/routes/apps/$clientId/designer.tsx b/apps/console/app/routes/apps/$clientId/designer.tsx index 6986f9dd2f..a87f087838 100644 --- a/apps/console/app/routes/apps/$clientId/designer.tsx +++ b/apps/console/app/routes/apps/$clientId/designer.tsx @@ -97,6 +97,7 @@ import designerSVG from '~/assets/early/designer.webp' import EarlyAccessPanel from '~/components/EarlyAccess/EarlyAccessPanel' import { IdentityURN } from '@proofzero/urns/identity' import { GetOgThemeResult } from '@proofzero/platform.starbase/src/jsonrpc/methods/getOgTheme' +import createImageClient from '@proofzero/platform-clients/image' const LazyAuth = lazy(() => // @ts-ignore :( @@ -1556,6 +1557,8 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper( let colorDark = fd.get('colordark') as string | undefined if (!colorDark || colorDark === '') colorDark = undefined + const ogGraphicURL = theme.graphicURL + let graphicURL = fd.get('image') as string | undefined if (!graphicURL || graphicURL === '') graphicURL = undefined @@ -1596,6 +1599,14 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper( clientId, theme, }) + + if (ogGraphicURL && ogGraphicURL !== theme.graphicURL) { + const imageClient = createImageClient(context.env.Images, { + headers: generateTraceContextHeaders(context.traceSpan), + }) + + await imageClient.delete.mutate(ogGraphicURL) + } } return json({ diff --git a/platform/images/src/jsonrpc/methods/delete.ts b/platform/images/src/jsonrpc/methods/delete.ts new file mode 100644 index 0000000000..44d298757f --- /dev/null +++ b/platform/images/src/jsonrpc/methods/delete.ts @@ -0,0 +1,36 @@ +import { z } from 'zod' +import { Context } from '../../context' + +export const DeleteMethodInputSchema = z.string() +export type DeleteMethodInputParams = z.infer + +export const DeleteMethodOutputSchema = z.boolean() +export type DeleteMethodOutputParams = z.infer + +export const deleteMethod = async ({ + input, + ctx, +}: { + input: DeleteMethodInputParams + ctx: Context +}): Promise => { + // A typical image delivery URL looks like this: + // https://imagedelivery.net/// + const imageComponents = input.split('/') + const imageID = imageComponents[imageComponents.length - 2] + + const url = `https://api.cloudflare.com/client/v4/accounts/${ctx.INTERNAL_CLOUDFLARE_ACCOUNT_ID}/images/v1/${imageID}` + const deleteRequest = new Request(url, { + method: 'DELETE', + headers: { + Authorization: `Bearer ${ctx.TOKEN_CLOUDFLARE_API}`, + }, + }) + + const response = await fetch(deleteRequest) + const res = (await response.json()) as { + success: boolean + } + + return res.success +} diff --git a/platform/images/src/jsonrpc/router.ts b/platform/images/src/jsonrpc/router.ts index 3224c7f44d..1b20731c20 100644 --- a/platform/images/src/jsonrpc/router.ts +++ b/platform/images/src/jsonrpc/router.ts @@ -21,6 +21,11 @@ import { } from './methods/getGradient' import { Context } from '../context' +import { + DeleteMethodInputSchema, + DeleteMethodOutputSchema, + deleteMethod, +} from './methods/delete' const t = initTRPC.context().create({ errorFormatter }) @@ -31,6 +36,12 @@ export const appRouter = t.router({ .input(uploadMethodInput) .output(uploadMethodOutput) .mutation(uploadMethod), + delete: t.procedure + .use(LogUsage) + .use(Analytics) + .input(DeleteMethodInputSchema) + .output(DeleteMethodOutputSchema) + .mutation(deleteMethod), getOgImage: t.procedure .use(LogUsage) .use(Analytics)