add cors for raw file link

This commit is contained in:
myl7
2022-01-26 20:42:57 +08:00
parent 79fd82b71e
commit 39022be478
3 changed files with 61 additions and 0 deletions
+11
View File
@@ -2,6 +2,7 @@ import { posix as pathPosix } from 'path'
import type { NextApiRequest, NextApiResponse } from 'next'
import axios from 'axios'
import Cors from 'cors'
import apiConfig from '../../config/api.json'
import siteConfig from '../../config/site.json'
@@ -170,6 +171,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// Go for file raw download link and query with only temporary link parameter
if (raw) {
const cors = Cors({ methods: ['GET', 'HEAD'] })
await new Promise((resolve, reject) => {
cors(req, res, result => {
if (result instanceof Error) {
return reject(result)
}
return resolve(result)
})
})
const { data } = await axios.get(requestUrl, {
headers: { Authorization: `Bearer ${accessToken}` },
params: {