function fileToGenerativePart(path, mimeType) {
return {
inlineData: {
data: Buffer.from(fs.readFileSync(path)).toString('base64'),
mimeType,
},
}
}
let imageParts = []
if (Array.isArray(visionImgList) && visionImgList.length > 0) {
visionImgList = visionImgList
.filter((item) => item.url)
.map((item) => item.url)
let list = visionImgList
let promiseListForDownload = list.map((item) => {
return new Promise((resolve) => {
let sdUrl = item
let urlObj = new URL(sdUrl)
const client = urlObj.protocol == 'https:' ? https : http
client.get(sdUrl, async (httpRes) => {
let fileName = Date.now()
let endIndex =
sdUrl.indexOf('?') > 0 ? sdUrl.indexOf('?') : sdUrl.length
sdUrl = sdUrl.slice(0, endIndex)
let pointIndex = sdUrl.lastIndexOf('.')
let mimeType = sdUrl.slice(pointIndex + 1) //PNG - 图片/png JPEG - image/jpeg WEBP - image/webp HEIC - 图片/heic HEIF - image/heif
let urlCdnArr = sdUrl.split('/')
let filePath = `/temp/ai/geminiImages/${fileName}-${
urlCdnArr[urlCdnArr.length - 1]
}`
let stream = fs.createWriteStream(filePath)
httpRes.pipe(stream)
stream.on('finish', async () => {
resolve({ filePath, mimeType: `image/${mimeType}` })
})
})
})
})
let localFileList = await Promise.all(promiseListForDownload)
imageParts = localFileList.map((item) => {
return fileToGenerativePart(item.filePath, item.mimeType)
})
我开发的chatgpt网站: