in vision/use-cases/hey_llm/src/main.ts [374:420]
function uploadImageToDrive_(
oauth: GoogleAppsScriptOAuth2.OAuth2Service,
base64image: string,
filename: string,
mimeType: string,
parentFolderID: string,
) {
const metadata = {
name: filename,
mimeType: mimeType,
parents: [parentFolderID],
};
const boundary = 'UploadImageRequestBoundary';
const payload = `--${boundary}
Content-Type: application/json; charset=UTF-8
${JSON.stringify(metadata)}
--${boundary}
Content-Type: ${mimeType}
Content-Transfer-Encoding: base64
${base64image}
--${boundary}--`;
const res = UrlFetchApp.fetch(
'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=thumbnailLink',
{
method: 'post',
headers: {
Authorization: 'Bearer ' + oauth.getAccessToken(),
'Content-Type': `multipart/related; boundary=${boundary}`,
},
payload: payload,
muteHttpExceptions: true,
},
);
const result: {thumbnailLink?: string} = JSON.parse(res.getContentText());
if (!result.thumbnailLink) {
Logger.log(JSON.stringify(metadata), result);
throw new Error(
'Uploading image to Drive failed. Is Drive API enabled? ' +
res.getContentText(),
);
}
return result.thumbnailLink;
}