88 lines
2.6 KiB
JavaScript
88 lines
2.6 KiB
JavaScript
import axios from "axios";
|
|
import https from "https";
|
|
import CryptoJS from "crypto-js";
|
|
import { getToken } from "../../../../actions";
|
|
|
|
const WORDKEY = process.env.HASHKEY;
|
|
const accessTokenEndpoint = process.env.ACCESS_TOKEN_ENDPOINT;
|
|
const tenantId = process.env.TENANT;
|
|
|
|
const WEBAPI_URL =
|
|
process.env.RELAY_ROOT ||
|
|
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
|
|
|
|
const hashAPIPath = (queryPath) => {
|
|
var hashlink = CryptoJS.HmacSHA256(
|
|
queryPath,
|
|
CryptoJS.enc.Hex.parse(WORDKEY)
|
|
);
|
|
hashlink = hashlink.toString(CryptoJS.enc.Hex);
|
|
|
|
//return "&hash=" + hashlink;
|
|
|
|
return (queryPath.indexOf("?") > -1 ? "&hash=" : "?hash=") + hashlink;
|
|
};
|
|
|
|
const azureHeadersPaged = (access_token) => {
|
|
return {
|
|
headers: {
|
|
"OData-MaxVersion": "4.0",
|
|
"OData-Version": "4.0",
|
|
"Accept": "application/json;odata.metadata=none",
|
|
"Prefer":
|
|
'odata.include-annotations="*",return=representation, odata.maxpagesize=10',
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Bearer " + access_token,
|
|
},
|
|
};
|
|
};
|
|
|
|
export default async function ApiProxy(req, res) {
|
|
var token = await getToken();
|
|
|
|
var docRef = req.query;
|
|
|
|
var queryUrl = "documents/download/" + docRef.id + "?hash=" + docRef.hash;
|
|
|
|
const configDocument = (access_token) => {
|
|
return {
|
|
headers: {
|
|
"OData-MaxVersion": "4.0",
|
|
"OData-Version": "4.0",
|
|
"Accept": "application/json",
|
|
"Prefer": 'odata.include-annotations="*",return=representation',
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Bearer " + access_token,
|
|
},
|
|
responseType: "arraybuffer",
|
|
};
|
|
};
|
|
|
|
return axios
|
|
.get(
|
|
WEBAPI_URL + queryUrl, // + hashAPIPath(queryUrl),
|
|
configDocument(token.access_token)
|
|
)
|
|
.then((response) => {
|
|
const bytes = response.data.byteLength;
|
|
console.log(bytes);
|
|
|
|
res.setHeader(
|
|
"content-disposition",
|
|
"attachment; filename=" +
|
|
response.headers["content-disposition"].split(
|
|
"filename="
|
|
)[1]
|
|
);
|
|
return res.status(200).send(response.data);
|
|
})
|
|
.then(() => {
|
|
console.log("has downloaded");
|
|
|
|
return "downloadComplete";
|
|
})
|
|
.catch(({ err }) => {
|
|
res.status(400).json(err);
|
|
});
|
|
}
|