From cc9b7a1f1af880f7b39022d7ae7390ca6a99ae44 Mon Sep 17 00:00:00 2001 From: "Taylor Wilsdon (aider)" Date: Mon, 16 Dec 2024 10:32:01 -0500 Subject: [PATCH] fix: Update Google Drive file download URL generation to handle different file types --- src/lib/utils/google-drive-picker.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib/utils/google-drive-picker.ts b/src/lib/utils/google-drive-picker.ts index 1fa09a73a..789c8f02f 100644 --- a/src/lib/utils/google-drive-picker.ts +++ b/src/lib/utils/google-drive-picker.ts @@ -130,9 +130,22 @@ export const createPicker = () => { throw new Error('Required file details missing'); } - // Construct download URL using usercontent format + // Construct download URL based on MIME type console.log('Constructing download URL for fileId:', fileId); - const downloadUrl = `https://drive.usercontent.google.com/u/0/uc?id=${fileId}&export=download`; + const mimeType = doc[google.picker.Document.MIME_TYPE]; + console.log('File MIME type:', mimeType); + + let downloadUrl; + if (mimeType.includes('google-apps')) { + // Google Docs/Sheets/etc need export URL + const exportFormat = mimeType.includes('document') ? 'docx' : + mimeType.includes('spreadsheet') ? 'xlsx' : + mimeType.includes('presentation') ? 'pptx' : 'pdf'; + downloadUrl = `https://www.googleapis.com/drive/v3/files/${fileId}/export?mimeType=application/${exportFormat}`; + } else { + // Regular files use direct download URL + downloadUrl = `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`; + } console.log('Download URL constructed:', downloadUrl); console.log('Current token value:', token ? 'Token exists' : 'No token');