66 lines
1.7 KiB
JavaScript
66 lines
1.7 KiB
JavaScript
import { downloadFile } from "../../../actions/azurestorage";
|
|
|
|
import nextConnect from "next-connect";
|
|
import { hashAPIPath } from "../../../actions";
|
|
import middleware from "../middleware/middleware";
|
|
|
|
const WORDKEY = process.env.HASHKEY;
|
|
|
|
const ApiProxy = nextConnect();
|
|
|
|
ApiProxy.use(middleware);
|
|
|
|
ApiProxy.get(async (req, res) => {
|
|
var containerName = req.query.container;
|
|
var casefolderID = req.query.casefolderID;
|
|
var blobName = req.query.blobname;
|
|
var checkHash = req.query.hash;
|
|
|
|
var checkquerypath =
|
|
"/api/file/downloadblob?container=" +
|
|
containerName +
|
|
"&casefolderID=" +
|
|
encodeURIComponent(casefolderID) +
|
|
"&blobname=" +
|
|
encodeURIComponent(blobName.trim());
|
|
|
|
if (hashAPIPath(checkquerypath) == "&hash=" + checkHash) {
|
|
const bloblocation =
|
|
casefolderID + (blobName.indexOf(".json") > 0 ? "/" : "/files/");
|
|
|
|
const downloaded = await downloadFile(
|
|
containerName,
|
|
bloblocation + decodeURI(blobName)
|
|
);
|
|
|
|
res.setHeader(
|
|
"content-disposition",
|
|
"attachment; filename=" + decodeURI(blobName)
|
|
);
|
|
return res.status(200).send(downloaded);
|
|
} else {
|
|
return res.status(400).json();
|
|
}
|
|
});
|
|
|
|
async function streamToBuffer(readableStream) {
|
|
return new Promise((resolve, reject) => {
|
|
const chunks = [];
|
|
readableStream.on("data", (data) => {
|
|
chunks.push(data instanceof Buffer ? data : Buffer.from(data));
|
|
});
|
|
readableStream.on("end", () => {
|
|
resolve(Buffer.concat(chunks));
|
|
});
|
|
readableStream.on("error", reject);
|
|
});
|
|
}
|
|
|
|
export const config = {
|
|
api: {
|
|
bodyParser: false,
|
|
},
|
|
};
|
|
|
|
export default ApiProxy;
|