Skip to content

Commit

Permalink
Integrate Vercel image upload for featured images
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mikepsinn committed Oct 3, 2024
1 parent 3a6c6e2 commit 9700598
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/researcher/researcherActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
writeArticle
} from "@/lib/agents/researcher/researcher";
import OpenAI from "openai";
import {uploadImageToVercel} from "@/lib/imageUploader";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
})
Expand Down Expand Up @@ -98,13 +99,20 @@ export async function generateImage(topic: string, articleId: string) {
})

const imageUrl = response.data[0].url
if (!imageUrl) {
throw new Error("Failed to generate image")
}
const imgPath = 'articles/' + articleId + '/featured-image'
// get the buffer from the imageUrl
const buffer = await fetch(imageUrl).then(res => res.arrayBuffer())
const vercelUrl = await uploadImageToVercel(Buffer.from(buffer), imgPath)
if (imageUrl && articleId) {
await prisma.article.update({
where: {
id: articleId,
},
data: {
featuredImage: imageUrl,
featuredImage: vercelUrl,
},
})
}
Expand Down

0 comments on commit 9700598

Please sign in to comment.