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.

isStored → isNotHostedImage

Steven Tey73110627f2a6
diff --git a/apps/web/lib/api/links/bulk-update-links.ts b/apps/web/lib/api/links/bulk-update-links.ts
index 460a2a1f9e..5c47b76fee 100644
--- a/apps/web/lib/api/links/bulk-update-links.ts
+++ b/apps/web/lib/api/links/bulk-update-links.ts
@@ -1,4 +1,4 @@
-import { isStored, storage } from "@/lib/storage";
+import { isNotHostedImage, storage } from "@/lib/storage";
import z from "@/lib/zod";
import { bulkUpdateLinksBodySchema } from "@/lib/zod/schemas/links";
import { prisma } from "@dub/prisma";
@@ -50,7 +50,7 @@ export async function bulkUpdateLinks(
title: truncate(title, 120),
description: truncate(description, 240),
image:
- proxy && image && !isStored(image)
+ proxy && image && !isNotHostedImage(image)
? `${R2_URL}/images/${linkIds[0]}_${imageUrlNonce}`
: image,
expiresAt: expiresAt ? new Date(expiresAt) : null,
@@ -129,7 +129,7 @@ export async function bulkUpdateLinks(
// if proxy is true and image is not stored in R2, upload image to R2
proxy &&
image &&
- !isStored(image) &&
+ !isNotHostedImage(image) &&
storage.upload(`images/${linkIds[0]}_${imageUrlNonce}`, image, {
width: 1200,
height: 630,
diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.ts
index 8807f8c0e3..555f945746 100644
--- a/apps/web/lib/api/links/create-link.ts
+++ b/apps/web/lib/api/links/create-link.ts
@@ -1,6 +1,6 @@
import { qstash } from "@/lib/cron";
import { getPartnerAndDiscount } from "@/lib/planetscale/get-partner-discount";
-import { isStored, storage } from "@/lib/storage";
+import { isNotHostedImage, storage } from "@/lib/storage";
import { recordLink } from "@/lib/tinybird";
import { ProcessedLinkProps } from "@/lib/types";
import { propagateWebhookTriggerChanges } from "@/lib/webhook/update-webhook";
@@ -60,7 +60,7 @@ export async function createLink(link: ProcessedLinkProps) {
title: truncate(title, 120),
description: truncate(description, 240),
// if it's an uploaded image, make this null first because we'll update it later
- image: proxy && image && !isStored(image) ? null : image,
+ image: proxy && image && !isNotHostedImage(image) ? null : image,
utm_source,
utm_medium,
utm_campaign,
@@ -150,8 +150,8 @@ export async function createLink(link: ProcessedLinkProps) {
// record link in Tinybird
recordLink(response),
// Upload image to R2 and update the link with the uploaded image URL when
- // proxy is enabled and image is set and not stored in R2
- ...(proxy && image && !isStored(image)
+ // proxy is enabled and image is set and is not a hosted image URL
+ ...(proxy && image && !isNotHostedImage(image)
? [
// upload image to R2
storage.upload(`images/${response.id}`, image, {
@@ -199,6 +199,8 @@ export async function createLink(link: ProcessedLinkProps) {
...transformLink(response),
// optimistically set the image URL to the uploaded image URL
image:
- proxy && image && !isStored(image) ? uploadedImageUrl : response.image,
+ proxy && image && !isNotHostedImage(image)
+ ? uploadedImageUrl
+ : response.image,
};
}
diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.ts
index 9c648896b7..87d5e306ae 100644
--- a/apps/web/lib/api/links/update-link.ts
+++ b/apps/web/lib/api/links/update-link.ts
@@ -1,5 +1,5 @@
import { getPartnerAndDiscount } from "@/lib/planetscale/get-partner-discount";
-import { isStored, storage } from "@/lib/storage";
+import { isNotHostedImage, storage } from "@/lib/storage";
import { recordLink } from "@/lib/tinybird";
import { LinkProps, ProcessedLinkProps } from "@/lib/types";
import { propagateWebhookTriggerChanges } from "@/lib/webhook/update-webhook";
@@ -93,7 +93,7 @@ export async function updateLink({
title: truncate(title, 120),
description: truncate(description, 240),
image:
- proxy && image && !isStored(image)
+ proxy && image && !isNotHostedImage(image)
? `${R2_URL}/images/${id}_${imageUrlNonce}`
: image,
utm_source: utm_source || null,
@@ -188,7 +188,7 @@ export async function updateLink({
// if proxy is true and image is not stored in R2, upload image to R2
proxy &&
image &&
- !isStored(image) &&
+ !isNotHostedImage(image) &&
storage.upload(`images/${id}_${imageUrlNonce}`, image, {
width: 1200,
height: 630,
diff --git a/apps/web/lib/storage.ts b/apps/web/lib/storage.ts
index ae39d9371f..d7917aa422 100644
--- a/apps/web/lib/storage.ts
+++ b/apps/web/lib/storage.ts
@@ -153,3 +153,7 @@ export const storage = new StorageClient();
export const isStored = (url: string) => {
return url.startsWith(R2_URL) || url.startsWith(OG_AVATAR_URL);
};
+
+export const isNotHostedImage = (imageString: string) => {
+ return !imageString.startsWith("https://");
+};