Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

only delete if stored in R2

Steven Tey2c8a20aac2b2
diff --git a/apps/web/app/api/admin/ban/route.ts b/apps/web/app/api/admin/ban/route.ts
index 93a9ac80ef..4b6704e1f5 100644
--- a/apps/web/app/api/admin/ban/route.ts
+++ b/apps/web/app/api/admin/ban/route.ts
@@ -17,6 +17,7 @@ export const POST = withAdmin(async ({ req }) => {
select: {
id: true,
email: true,
+ image: true,
projects: {
where: {
role: "owner",
@@ -58,7 +59,9 @@ export const POST = withAdmin(async ({ req }) => {
id: user.id,
},
}),
- storage.delete(`avatars/${user.id}`),
+ // if the user has a custom avatar, delete it
+ user.image?.startsWith(`https://${process.env.STORAGE_DOMAIN}`) &&
+ storage.delete(`avatars/${user.id}`),
unsubscribe(user.email),
fetch(
`https://api.vercel.com/v1/edge-config/${process.env.EDGE_CONFIG_ID}/items?teamId=${process.env.TEAM_ID_VERCEL}`,
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.ts
index 2d8779a0cb..646fb9fd46 100644
--- a/apps/web/app/api/user/route.ts
+++ b/apps/web/app/api/user/route.ts
@@ -74,13 +74,15 @@ export const DELETE = withSession(async ({ session }) => {
{ status: 422 },
);
} else {
+ const user = await prisma.user.delete({
+ where: {
+ id: session.user.id,
+ },
+ });
const response = await Promise.allSettled([
- prisma.user.delete({
- where: {
- id: session.user.id,
- },
- }),
- storage.delete(`avatars/${session.user.id}`),
+ // if the user has a custom avatar, delete it
+ user.image?.startsWith(`https://${process.env.STORAGE_DOMAIN}`) &&
+ storage.delete(`avatars/${session.user.id}`),
unsubscribe(session.user.email),
]);
return NextResponse.json(response);
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 876b08e479..8fa485208b 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -556,7 +556,7 @@ export async function addLink(link: LinkWithTagIdsProps) {
if (proxy && image && uploadedImage) {
await Promise.all([
- // upload image to storage
+ // upload image to R2
storage.upload(`images/${response.id}`, image),
// update the null image we set earlier to the uploaded image URL
prisma.link.update({
@@ -751,7 +751,7 @@ export async function editLink({
if (uploadedImage) {
await storage.upload(`images/${id}`, image);
}
- // if there's no proxy enabled or no image, delete the image
+ // if there's an image in R2, delete it
} else if (oldImage?.startsWith(`https://${process.env.STORAGE_DOMAIN}`)) {
await storage.delete(`images/${id}`);
}
@@ -833,7 +833,10 @@ export async function deleteLink(linkId: string) {
},
});
return await Promise.allSettled([
- link.proxy && link.image && storage.delete(`images/${link.id}`),
+ // if the image is stored in Cloudflare R2, delete it
+ link.proxy &&
+ link.image?.startsWith(`https://${process.env.STORAGE_DOMAIN}`) &&
+ storage.delete(`images/${link.id}`),
redis.hdel(link.domain, link.key.toLowerCase()),
recordLink({ link, deleted: true }),
link.projectId &&
diff --git a/apps/web/lib/api/projects.ts b/apps/web/lib/api/projects.ts
index 530c8d72cb..51726dde12 100644
--- a/apps/web/lib/api/projects.ts
+++ b/apps/web/lib/api/projects.ts
@@ -1,8 +1,8 @@
import { deleteDomainAndLinks } from "@/lib/api/domains";
import prisma from "@/lib/prisma";
+import { storage } from "@/lib/storage";
import { cancelSubscription } from "@/lib/stripe";
import { DUB_DOMAINS_ARRAY, LEGAL_PROJECT_ID, LEGAL_USER_ID } from "@dub/utils";
-import { storage } from "@/lib/storage";
import { ProjectProps } from "../types";
import { redis } from "../upstash";
@@ -30,6 +30,7 @@ export async function deleteProject(
domain: true,
key: true,
proxy: true,
+ image: true,
},
}),
]);
@@ -60,15 +61,18 @@ export async function deleteProject(
),
// delete all default domain links from redis
pipeline.exec(),
- // remove all images
- ...defaultDomainLinks.map(({ id, proxy }) =>
- proxy ? storage.delete(`images/${id}`) : Promise.resolve(),
+ // remove all images from R2
+ ...defaultDomainLinks.map(({ id, proxy, image }) =>
+ proxy && image?.startsWith(`https://${process.env.STORAGE_DOMAIN}`)
+ ? storage.delete(`images/${id}`)
+ : Promise.resolve(),
),
]);
const deleteProjectResponse = await Promise.all([
- // delete project logo
- project.logo && storage.delete(`logos/${project.id}`),
+ // delete project logo if it's a custom logo stored in R2
+ project.logo?.startsWith(`https://${process.env.STORAGE_DOMAIN}`) &&
+ storage.delete(`logos/${project.id}`),
// if they have a Stripe subscription, cancel it
project.stripeId && cancelSubscription(project.stripeId),
// delete the project
@@ -122,8 +126,9 @@ export async function deleteProjectAdmin(
]);
const deleteProjectResponse = await Promise.all([
- // delete project logo
- project.logo && storage.delete(`logos/${project.id}`),
+ // delete project logo if it's a custom logo stored in R2
+ project.logo?.startsWith(`https://${process.env.STORAGE_DOMAIN}`) &&
+ storage.delete(`logos/${project.id}`),
// if they have a Stripe subscription, cancel it
project.stripeId && cancelSubscription(project.stripeId),
// delete the project