@@ -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
1819export const dynamic = 'force-dynamic'
@@ -114,6 +115,24 @@ export async function POST(request: NextRequest) {
114115
115116 if ( GOOGLE_WORKSPACE_MIME_TYPES . includes ( fileMimeType ) ) {
116117 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+
117136 finalMimeType = exportFormat
118137
119138 logger . info ( `[${ requestId } ] Exporting Google Workspace file` , {
@@ -131,7 +150,7 @@ export async function POST(request: NextRequest) {
131150 )
132151 }
133152
134- let exportResponse = await secureFetchWithPinnedIP (
153+ const exportResponse = await secureFetchWithPinnedIP (
135154 exportUrl ,
136155 exportUrlValidation . resolvedIP ! ,
137156 { headers : { Authorization : authHeader } }
@@ -141,64 +160,17 @@ export async function POST(request: NextRequest) {
141160 const exportError = ( await exportResponse
142161 . json ( )
143162 . catch ( ( ) => ( { } ) ) ) as GoogleApiErrorResponse
144- const errorMessage = exportError . error ?. message || ''
145-
146- const defaultFormat = DEFAULT_EXPORT_FORMATS [ fileMimeType ]
147- const canFallback = ! ! exportMimeType && ! ! defaultFormat && exportMimeType !== defaultFormat
148-
149- if ( canFallback ) {
150- logger . warn ( `[${ requestId } ] Export failed with custom format, falling back to default` , {
151- requestedFormat : exportFormat ,
152- fallbackFormat : defaultFormat ,
153- fileMimeType,
154- status : exportResponse . status ,
155- } )
156-
157- finalMimeType = defaultFormat
158- const fallbackUrl = `https://www.googleapis.com/drive/v3/files/${ fileId } /export?mimeType=${ encodeURIComponent ( defaultFormat ) } &supportsAllDrives=true`
159- const fallbackUrlValidation = await validateUrlWithDNS ( fallbackUrl , 'fallbackExportUrl' )
160- if ( ! fallbackUrlValidation . isValid ) {
161- return NextResponse . json (
162- { success : false , error : fallbackUrlValidation . error } ,
163- { status : 400 }
164- )
165- }
166-
167- exportResponse = await secureFetchWithPinnedIP (
168- fallbackUrl ,
169- fallbackUrlValidation . resolvedIP ! ,
170- { headers : { Authorization : authHeader } }
171- )
172-
173- if ( ! exportResponse . ok ) {
174- const fallbackError = ( await exportResponse
175- . json ( )
176- . catch ( ( ) => ( { } ) ) ) as GoogleApiErrorResponse
177- logger . error ( `[${ requestId } ] Fallback export also failed` , {
178- status : exportResponse . status ,
179- error : fallbackError ,
180- } )
181- return NextResponse . json (
182- {
183- success : false ,
184- error : fallbackError . error ?. message || 'Failed to export Google Workspace file' ,
185- } ,
186- { status : 400 }
187- )
188- }
189- } else {
190- logger . error ( `[${ requestId } ] Failed to export file` , {
191- status : exportResponse . status ,
192- error : exportError ,
193- } )
194- return NextResponse . json (
195- {
196- success : false ,
197- error : errorMessage || 'Failed to export Google Workspace file' ,
198- } ,
199- { status : 400 }
200- )
201- }
163+ logger . error ( `[${ requestId } ] Failed to export file` , {
164+ status : exportResponse . status ,
165+ error : exportError ,
166+ } )
167+ return NextResponse . json (
168+ {
169+ success : false ,
170+ error : exportError . error ?. message || 'Failed to export Google Workspace file' ,
171+ } ,
172+ { status : 400 }
173+ )
202174 }
203175
204176 const arrayBuffer = await exportResponse . arrayBuffer ( )
0 commit comments