fix: Improve Google Drive file download error handling and export formats

This commit is contained in:
Taylor Wilsdon (aider) 2024-12-16 10:32:50 -05:00
parent cc9b7a1f1a
commit ec26296dc3

View File

@ -136,12 +136,20 @@ export const createPicker = () => {
console.log('File MIME type:', mimeType); console.log('File MIME type:', mimeType);
let downloadUrl; let downloadUrl;
let exportFormat;
if (mimeType.includes('google-apps')) { if (mimeType.includes('google-apps')) {
// Google Docs/Sheets/etc need export URL // Handle Google Workspace files
const exportFormat = mimeType.includes('document') ? 'docx' : if (mimeType.includes('document')) {
mimeType.includes('spreadsheet') ? 'xlsx' : exportFormat = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
mimeType.includes('presentation') ? 'pptx' : 'pdf'; } else if (mimeType.includes('spreadsheet')) {
downloadUrl = `https://www.googleapis.com/drive/v3/files/${fileId}/export?mimeType=application/${exportFormat}`; exportFormat = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
} else if (mimeType.includes('presentation')) {
exportFormat = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
} else {
exportFormat = 'application/pdf';
}
downloadUrl = `https://www.googleapis.com/drive/v3/files/${fileId}/export?mimeType=${encodeURIComponent(exportFormat)}`;
} else { } else {
// Regular files use direct download URL // Regular files use direct download URL
downloadUrl = `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`; downloadUrl = `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`;
@ -159,7 +167,13 @@ export const createPicker = () => {
}); });
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to download file: ${response.statusText}`); const errorText = await response.text();
console.error('Download failed:', {
status: response.status,
statusText: response.statusText,
error: errorText
});
throw new Error(`Failed to download file (${response.status}): ${errorText}`);
} }
const blob = await response.blob(); const blob = await response.blob();