Skip to content

Commit 9700598

Browse files
committed
Integrate Vercel image upload for featured images
Optimized image upload process by integrating Vercel's image upload functionality. Ensured the image buffer is fetched and converted before uploading. Updated the database to store the Vercel URL as the featured image path.
1 parent 3a6c6e2 commit 9700598

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

app/researcher/researcherActions.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
writeArticle
1111
} from "@/lib/agents/researcher/researcher";
1212
import OpenAI from "openai";
13+
import {uploadImageToVercel} from "@/lib/imageUploader";
1314
const openai = new OpenAI({
1415
apiKey: process.env.OPENAI_API_KEY,
1516
})
@@ -98,13 +99,20 @@ export async function generateImage(topic: string, articleId: string) {
9899
})
99100

100101
const imageUrl = response.data[0].url
102+
if (!imageUrl) {
103+
throw new Error("Failed to generate image")
104+
}
105+
const imgPath = 'articles/' + articleId + '/featured-image'
106+
// get the buffer from the imageUrl
107+
const buffer = await fetch(imageUrl).then(res => res.arrayBuffer())
108+
const vercelUrl = await uploadImageToVercel(Buffer.from(buffer), imgPath)
101109
if (imageUrl && articleId) {
102110
await prisma.article.update({
103111
where: {
104112
id: articleId,
105113
},
106114
data: {
107-
featuredImage: imageUrl,
115+
featuredImage: vercelUrl,
108116
},
109117
})
110118
}

0 commit comments

Comments
 (0)