Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
improve raw file link query performance
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwooo committed Sep 4, 2021
1 parent 9a3599b commit 97f4250
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions pages/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

// Handle response from OneDrive API
const requestUrl = `${apiConfig.driveApi}/root${encodePath(path)}`
const { data } = await axios.get(requestUrl, {
headers: { Authorization: `Bearer ${accessToken}` },
params: {
select: '@microsoft.graph.downloadUrl,name,size,id,lastModifiedDateTime,folder,file',
expand: 'children(select=@content.downloadUrl,name,lastModifiedDateTime,eTag,size,id,folder,file)',
},
})

// Go for file raw download link and query with only temporary link parameter
if (raw) {
const { data } = await axios.get(requestUrl, {
headers: { Authorization: `Bearer ${accessToken}` },
params: {
select: '@microsoft.graph.downloadUrl,folder,file',
},
})

if ('folder' in data) {
res.status(400).json({ error: "Folders doesn't have raw download urls." })
return
Expand All @@ -111,6 +112,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
}

// Normal query selecting and expanding every children in current directory
const { data } = await axios.get(requestUrl, {
headers: { Authorization: `Bearer ${accessToken}` },
params: {
select: '@microsoft.graph.downloadUrl,name,size,id,lastModifiedDateTime,folder,file',
expand: 'children(select=@content.downloadUrl,name,lastModifiedDateTime,eTag,size,id,folder,file)',
},
})
res.status(200).json({ path, data })
return
}
Expand Down

1 comment on commit 97f4250

@vercel
Copy link

@vercel vercel bot commented on 97f4250 Sep 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.