Skip to content

Commit 48e174b

Browse files
authored
fix(google-drive): add auto export format and validate against Drive API docs (#4161)
* fix(google-drive): add auto export format and Azure storage debug logging * chore: remove Azure storage debug logging * fix(google-drive): use status-based fallback instead of string matching for export errors * fix(google-drive): validate export formats against Drive API docs, remove fallback * fix(google-drive): use value function for dropdown default * fix(google-drive): add text/markdown to valid export formats for Google Docs * fix(google-drive): correct ODS MIME type for Sheets export format
1 parent 7529a75 commit 48e174b

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

apps/sim/app/api/tools/google_drive/download/route.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ALL_REVISION_FIELDS,
1414
DEFAULT_EXPORT_FORMATS,
1515
GOOGLE_WORKSPACE_MIME_TYPES,
16+
VALID_EXPORT_FORMATS,
1617
} from '@/tools/google_drive/utils'
1718

1819
export const dynamic = 'force-dynamic'
@@ -65,10 +66,12 @@ export async function POST(request: NextRequest) {
6566
const {
6667
accessToken,
6768
fileId,
68-
mimeType: exportMimeType,
69+
mimeType: rawExportMimeType,
6970
fileName,
7071
includeRevisions,
7172
} = validatedData
73+
const exportMimeType =
74+
rawExportMimeType && rawExportMimeType !== 'auto' ? rawExportMimeType : null
7275
const authHeader = `Bearer ${accessToken}`
7376

7477
logger.info(`[${requestId}] Getting file metadata from Google Drive`, { fileId })
@@ -112,6 +115,24 @@ export async function POST(request: NextRequest) {
112115

113116
if (GOOGLE_WORKSPACE_MIME_TYPES.includes(fileMimeType)) {
114117
const exportFormat = exportMimeType || DEFAULT_EXPORT_FORMATS[fileMimeType] || 'text/plain'
118+
119+
const validFormats = VALID_EXPORT_FORMATS[fileMimeType]
120+
if (validFormats && !validFormats.includes(exportFormat)) {
121+
logger.warn(`[${requestId}] Unsupported export format requested`, {
122+
fileId,
123+
fileMimeType,
124+
requestedFormat: exportFormat,
125+
validFormats,
126+
})
127+
return NextResponse.json(
128+
{
129+
success: false,
130+
error: `Export format "${exportFormat}" is not supported for this file type. Supported formats: ${validFormats.join(', ')}`,
131+
},
132+
{ status: 400 }
133+
)
134+
}
135+
115136
finalMimeType = exportFormat
116137

117138
logger.info(`[${requestId}] Exporting Google Workspace file`, {

apps/sim/blocks/blocks/google_drive.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ Return ONLY the query string - no explanations, no quotes around the whole thing
316316
title: 'Export Format',
317317
type: 'dropdown',
318318
options: [
319+
{ label: 'Auto (best format for file type)', id: 'auto' },
319320
{ label: 'Plain Text (text/plain)', id: 'text/plain' },
320321
{ label: 'HTML (text/html)', id: 'text/html' },
321322
{ label: 'PDF (application/pdf)', id: 'application/pdf' },
@@ -333,7 +334,8 @@ Return ONLY the query string - no explanations, no quotes around the whole thing
333334
},
334335
{ label: 'CSV (text/csv)', id: 'text/csv' },
335336
],
336-
placeholder: 'Optional: Choose export format for Google Docs/Sheets/Slides',
337+
value: () => 'auto',
338+
placeholder: 'Export format for Google Docs/Sheets/Slides',
337339
condition: { field: 'operation', value: 'download' },
338340
},
339341
{
@@ -867,7 +869,7 @@ Return ONLY the message text - no subject line, no greetings/signatures, no extr
867869
destinationFolderId: effectiveDestinationFolderId,
868870
file: normalizedFile,
869871
pageSize: rest.pageSize ? Number.parseInt(rest.pageSize as string, 10) : undefined,
870-
mimeType: mimeType,
872+
mimeType: mimeType === 'auto' ? undefined : mimeType,
871873
type: shareType, // Map shareType to type for share tool
872874
starred: starredValue,
873875
sendNotification: sendNotificationValue,

apps/sim/tools/google_drive/utils.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,50 @@ export const DEFAULT_EXPORT_FORMATS: Record<string, string> = {
111111
'application/vnd.google-apps.spreadsheet': 'text/csv',
112112
'application/vnd.google-apps.presentation': 'text/plain',
113113
'application/vnd.google-apps.drawing': 'image/png',
114-
'application/vnd.google-apps.form': 'application/pdf',
115-
'application/vnd.google-apps.script': 'application/json',
114+
'application/vnd.google-apps.form': 'application/zip',
115+
'application/vnd.google-apps.script': 'application/vnd.google-apps.script+json',
116+
}
117+
118+
/**
119+
* Valid export formats per Google Workspace file type.
120+
* See: https://developers.google.com/drive/api/guides/ref-export-formats
121+
*/
122+
export const VALID_EXPORT_FORMATS: Record<string, string[]> = {
123+
'application/vnd.google-apps.document': [
124+
'text/plain',
125+
'text/html',
126+
'application/pdf',
127+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
128+
'application/vnd.oasis.opendocument.text',
129+
'application/rtf',
130+
'application/epub+zip',
131+
'text/markdown',
132+
],
133+
'application/vnd.google-apps.spreadsheet': [
134+
'text/csv',
135+
'text/tab-separated-values',
136+
'application/pdf',
137+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
138+
'application/vnd.oasis.opendocument.spreadsheet',
139+
'application/zip',
140+
],
141+
'application/vnd.google-apps.presentation': [
142+
'text/plain',
143+
'application/pdf',
144+
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
145+
'application/vnd.oasis.opendocument.presentation',
146+
'image/jpeg',
147+
'image/png',
148+
'image/svg+xml',
149+
],
150+
'application/vnd.google-apps.drawing': [
151+
'application/pdf',
152+
'image/jpeg',
153+
'image/png',
154+
'image/svg+xml',
155+
],
156+
'application/vnd.google-apps.form': ['application/zip'],
157+
'application/vnd.google-apps.script': ['application/vnd.google-apps.script+json'],
116158
}
117159

118160
export const SOURCE_MIME_TYPES: Record<string, string> = {

0 commit comments

Comments
 (0)