Files
pedwfrontend/pages/api/file/upload.js
T
Robert Bond 1a2c3ede3b Merged PR 2295: resolved lint warnings
resolved lint warnings

Related work items: #22873
2026-05-05 17:13:53 +00:00

81 lines
2.3 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 getFieldValue = (field) => (Array.isArray(field) ? field[0] : field);
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 = getFieldValue(req.body?.appealData);
const containerID = getFieldValue(req.body?.containerID);
const casefolderID = getFieldValue(req.body?.casefolderID);
const repOrAppealRaw = getFieldValue(req.body?.repOrAppeal);
const repOrAppeal =
repOrAppealRaw === true ||
repOrAppealRaw === "true" ||
repOrAppealRaw === "1";
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;