Skip to content

Commit d0e54b1

Browse files
authored
Merge pull request dubinc#2385 from dubinc/is-not-hosted-image
2 parents ca0306a + 913660d commit d0e54b1

4 files changed

Lines changed: 17 additions & 11 deletions

File tree

apps/web/lib/api/links/bulk-update-links.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isStored, storage } from "@/lib/storage";
1+
import { isNotHostedImage, storage } from "@/lib/storage";
22
import z from "@/lib/zod";
33
import { bulkUpdateLinksBodySchema } from "@/lib/zod/schemas/links";
44
import { prisma } from "@dub/prisma";
@@ -50,7 +50,7 @@ export async function bulkUpdateLinks(
5050
title: truncate(title, 120),
5151
description: truncate(description, 240),
5252
image:
53-
proxy && image && !isStored(image)
53+
proxy && image && isNotHostedImage(image)
5454
? `${R2_URL}/images/${linkIds[0]}_${imageUrlNonce}`
5555
: image,
5656
expiresAt: expiresAt ? new Date(expiresAt) : null,
@@ -129,7 +129,7 @@ export async function bulkUpdateLinks(
129129
// if proxy is true and image is not stored in R2, upload image to R2
130130
proxy &&
131131
image &&
132-
!isStored(image) &&
132+
isNotHostedImage(image) &&
133133
storage.upload(`images/${linkIds[0]}_${imageUrlNonce}`, image, {
134134
width: 1200,
135135
height: 630,

apps/web/lib/api/links/create-link.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { qstash } from "@/lib/cron";
22
import { getPartnerAndDiscount } from "@/lib/planetscale/get-partner-discount";
3-
import { isStored, storage } from "@/lib/storage";
3+
import { isNotHostedImage, storage } from "@/lib/storage";
44
import { recordLink } from "@/lib/tinybird";
55
import { ProcessedLinkProps } from "@/lib/types";
66
import { propagateWebhookTriggerChanges } from "@/lib/webhook/update-webhook";
@@ -60,7 +60,7 @@ export async function createLink(link: ProcessedLinkProps) {
6060
title: truncate(title, 120),
6161
description: truncate(description, 240),
6262
// if it's an uploaded image, make this null first because we'll update it later
63-
image: proxy && image && !isStored(image) ? null : image,
63+
image: proxy && image && isNotHostedImage(image) ? null : image,
6464
utm_source,
6565
utm_medium,
6666
utm_campaign,
@@ -150,8 +150,8 @@ export async function createLink(link: ProcessedLinkProps) {
150150
// record link in Tinybird
151151
recordLink(response),
152152
// Upload image to R2 and update the link with the uploaded image URL when
153-
// proxy is enabled and image is set and not stored in R2
154-
...(proxy && image && !isStored(image)
153+
// proxy is enabled and image is set and is not a hosted image URL
154+
...(proxy && image && isNotHostedImage(image)
155155
? [
156156
// upload image to R2
157157
storage.upload(`images/${response.id}`, image, {
@@ -199,6 +199,8 @@ export async function createLink(link: ProcessedLinkProps) {
199199
...transformLink(response),
200200
// optimistically set the image URL to the uploaded image URL
201201
image:
202-
proxy && image && !isStored(image) ? uploadedImageUrl : response.image,
202+
proxy && image && isNotHostedImage(image)
203+
? uploadedImageUrl
204+
: response.image,
203205
};
204206
}

apps/web/lib/api/links/update-link.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getPartnerAndDiscount } from "@/lib/planetscale/get-partner-discount";
2-
import { isStored, storage } from "@/lib/storage";
2+
import { isNotHostedImage, storage } from "@/lib/storage";
33
import { recordLink } from "@/lib/tinybird";
44
import { LinkProps, ProcessedLinkProps } from "@/lib/types";
55
import { propagateWebhookTriggerChanges } from "@/lib/webhook/update-webhook";
@@ -93,7 +93,7 @@ export async function updateLink({
9393
title: truncate(title, 120),
9494
description: truncate(description, 240),
9595
image:
96-
proxy && image && !isStored(image)
96+
proxy && image && isNotHostedImage(image)
9797
? `${R2_URL}/images/${id}_${imageUrlNonce}`
9898
: image,
9999
utm_source: utm_source || null,
@@ -188,7 +188,7 @@ export async function updateLink({
188188
// if proxy is true and image is not stored in R2, upload image to R2
189189
proxy &&
190190
image &&
191-
!isStored(image) &&
191+
isNotHostedImage(image) &&
192192
storage.upload(`images/${id}_${imageUrlNonce}`, image, {
193193
width: 1200,
194194
height: 630,

apps/web/lib/storage.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,7 @@ export const storage = new StorageClient();
153153
export const isStored = (url: string) => {
154154
return url.startsWith(R2_URL) || url.startsWith(OG_AVATAR_URL);
155155
};
156+
157+
export const isNotHostedImage = (imageString: string) => {
158+
return !imageString.startsWith("https://");
159+
};

0 commit comments

Comments
 (0)