@@ -11,11 +11,10 @@ const logger = createLogger('GoogleDriveFileAPI')
1111 * Get a single file from Google Drive
1212 */
1313export async function GET ( request : NextRequest ) {
14- const requestId = generateRequestId ( ) // Generate a short request ID for correlation
14+ const requestId = generateRequestId ( )
1515 logger . info ( `[${ requestId } ] Google Drive file request received` )
1616
1717 try {
18- // Get the credential ID and file ID from the query params
1918 const { searchParams } = new URL ( request . url )
2019 const credentialId = searchParams . get ( 'credentialId' )
2120 const fileId = searchParams . get ( 'fileId' )
@@ -31,7 +30,6 @@ export async function GET(request: NextRequest) {
3130 return NextResponse . json ( { error : authz . error || 'Unauthorized' } , { status : 403 } )
3231 }
3332
34- // Refresh access token if needed using the utility function
3533 const accessToken = await refreshAccessTokenIfNeeded (
3634 credentialId ,
3735 authz . credentialOwnerUserId ,
@@ -42,7 +40,6 @@ export async function GET(request: NextRequest) {
4240 return NextResponse . json ( { error : 'Failed to obtain valid access token' } , { status : 401 } )
4341 }
4442
45- // Fetch the file from Google Drive API
4643 logger . info ( `[${ requestId } ] Fetching file ${ fileId } from Google Drive API` )
4744 const response = await fetch (
4845 `https://www.googleapis.com/drive/v3/files/${ fileId } ?fields=id,name,mimeType,iconLink,webViewLink,thumbnailLink,createdTime,modifiedTime,size,owners,exportLinks,shortcutDetails&supportsAllDrives=true` ,
@@ -69,15 +66,13 @@ export async function GET(request: NextRequest) {
6966
7067 const file = await response . json ( )
7168
72- // In case of Google Docs, Sheets, etc., provide the export links
7369 const exportFormats : { [ key : string ] : string } = {
7470 'application/vnd.google-apps.document' : 'application/pdf' , // Google Docs to PDF
7571 'application/vnd.google-apps.spreadsheet' :
7672 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' , // Google Sheets to XLSX
7773 'application/vnd.google-apps.presentation' : 'application/pdf' , // Google Slides to PDF
7874 }
7975
80- // Resolve shortcuts transparently for UI stability
8176 if (
8277 file . mimeType === 'application/vnd.google-apps.shortcut' &&
8378 file . shortcutDetails ?. targetId
@@ -105,20 +100,16 @@ export async function GET(request: NextRequest) {
105100 }
106101 }
107102
108- // If the file is a Google Docs, Sheets, or Slides file, we need to provide the export link
109103 if ( file . mimeType . startsWith ( 'application/vnd.google-apps.' ) ) {
110104 const format = exportFormats [ file . mimeType ] || 'application/pdf'
111105 if ( ! file . exportLinks ) {
112- // If export links are not available in the response, try to construct one
113106 file . downloadUrl = `https://www.googleapis.com/drive/v3/files/${ file . id } /export?mimeType=${ encodeURIComponent (
114107 format
115108 ) } `
116109 } else {
117- // Use the export link from the response if available
118110 file . downloadUrl = file . exportLinks [ format ]
119111 }
120112 } else {
121- // For regular files, use the download link
122113 file . downloadUrl = `https://www.googleapis.com/drive/v3/files/${ file . id } ?alt=media`
123114 }
124115
0 commit comments