-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(platform): delete old images on upload of new ones
- Loading branch information
1 parent
498aa96
commit 3ce1cb1
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { z } from 'zod' | ||
import { Context } from '../../context' | ||
|
||
export const DeleteMethodInputSchema = z.string() | ||
export type DeleteMethodInputParams = z.infer<typeof DeleteMethodInputSchema> | ||
|
||
export const DeleteMethodOutputSchema = z.boolean() | ||
export type DeleteMethodOutputParams = z.infer<typeof DeleteMethodOutputSchema> | ||
|
||
export const deleteMethod = async ({ | ||
input, | ||
ctx, | ||
}: { | ||
input: DeleteMethodInputParams | ||
ctx: Context | ||
}): Promise<DeleteMethodOutputParams> => { | ||
// A typical image delivery URL looks like this: | ||
// https://imagedelivery.net/<ACCOUNT_HASH>/<IMAGE_ID>/<VARIANT_NAME> | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters