The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Update migrate-images.ts
Steven Teyc8238302f8aa
diff --git a/apps/web/scripts/migrate-images.ts b/apps/web/scripts/migrate-images.tsindex cce834ec78..7aecf81bf4 100644--- a/apps/web/scripts/migrate-images.ts+++ b/apps/web/scripts/migrate-images.ts@@ -1,5 +1,6 @@import prisma from "@/lib/prisma";import { storage } from "@/lib/storage";+import { truncate } from "@dub/utils";import "dotenv-flow/config";async function main() {@@ -14,30 +15,63 @@ async function main() {id: true,image: true,},- take: 10,+ take: 30,});- const res = await Promise.all(+ const res = await Promise.allSettled(imagesToMigrate.map(async (link) => {- const uploadedImage = await storage.upload(- `images/${link.id}`,- link.image!, // this exists since we're filtering above- {- width: 1200,- height: 630,- },- );+ try {+ const { url } = await storage.upload(+ `images/${link.id}`,+ link.image!, // this exists since we're filtering above+ {+ width: 1200,+ height: 630,+ },+ );- await prisma.link.update({- where: {- id: link.id,- },- data: {- image: uploadedImage.url,- },- });+ await prisma.link.update({+ where: {+ id: link.id,+ },+ data: {+ image: url,+ },+ });- return uploadedImage;+ return {+ url,+ original: truncate(link.image, 80),+ };+ } catch (e) {+ if (e.message === "Failed to fetch URL: Not Found") {+ // double check if it's actually 404+ const res = await fetch(link.image!);+ if (res.status === 404) {+ console.log(`Image deleted: ${link.image}`);+ await prisma.link.update({+ where: {+ id: link.id,+ },+ data: {+ image: null,+ },+ });+ }+ }+ return {+ url: "deleted: " + link.id,+ original: truncate(link.image, 80),+ };+ }+ }),+ ).then((res) =>+ res.map((r) => {+ if (r.status === "fulfilled") {+ return r.value;+ } else {+ return r.reason;+ }}),);