Files
pedwfrontend/pages/api/file/upload.js

75 lines
2.1 KiB
JavaScript

import { createBlob, createRepBlob } from "../../../actions/azurestorage";
import { hashAPIPath } from "../../../actions/core/hash";
import { respondError, respondSuccess } from "../middleware/apiResponse";
import nextConnect from "next-connect";
import middleware from "../middleware/middleware";
const ApiProxy = nextConnect();
ApiProxy.use(middleware);
ApiProxy.post(async (req, res) => {
const checkHash = req.query.hash;
if (typeof checkHash === "undefined" || checkHash.length === 0) {
return respondError(res, {
status: 400,
code: "HASH_REQUIRED",
message: "hash is required"
});
}
const hashCandidatePaths = ["/api/file/upload", "/api/file/upload?"];
const isHashValid = hashCandidatePaths.some(
(candidatePath) => hashAPIPath(candidatePath) == "?hash=" + checkHash
);
if (!isHashValid) {
return respondError(res, {
status: 400,
code: "INVALID_HASH",
message: "Invalid hash"
});
}
const appealData = req.body.appealData;
const containerID = req.body?.containerID?.[0];
const casefolderID = req.body?.casefolderID?.[0];
const repOrAppeal = req.body.repOrAppeal || false;
if (
typeof containerID === "undefined" ||
containerID.length === 0 ||
typeof casefolderID === "undefined" ||
casefolderID.length === 0
) {
return respondError(res, {
status: 400,
code: "MISSING_REQUIRED_BODY",
message: "containerID and casefolderID are required"
});
}
try {
const data = await (repOrAppeal
? createRepBlob(appealData, containerID, casefolderID)
: createBlob(appealData, containerID, casefolderID));
return respondSuccess(res, { data });
} catch {
return respondError(res, {
status: 400,
code: "UPLOAD_FAILED",
message: "Failed to upload blob"
});
}
});
export const config = {
api: {
bodyParser: false
}
};
export default ApiProxy;