113 lines
3.1 KiB
JavaScript
113 lines
3.1 KiB
JavaScript
import { createBlob, createRepBlob } from "../../../actions/azurestorage";
|
|
import { hashAPIPath } from "../../../actions/core/hash";
|
|
import { respondError, respondSuccess } from "../middleware/apiResponse";
|
|
import { getServerSession } from "next-auth/next";
|
|
import { authOptions } from "../auth/[...nextauth]";
|
|
|
|
import nextConnect from "next-connect";
|
|
import middleware from "../middleware/middleware";
|
|
|
|
const ApiProxy = nextConnect();
|
|
ApiProxy.use(middleware);
|
|
|
|
const unauthenticated = (res) =>
|
|
respondError(res, {
|
|
status: 401,
|
|
code: "UNAUTHENTICATED",
|
|
message: "Authentication required"
|
|
});
|
|
|
|
const forbidden = (res) =>
|
|
respondError(res, {
|
|
status: 403,
|
|
code: "FORBIDDEN",
|
|
message: "Request not permitted"
|
|
});
|
|
|
|
ApiProxy.post(async (req, res) => {
|
|
const getFieldValue = (field) => (Array.isArray(field) ? field[0] : field);
|
|
|
|
const session = await getServerSession(req, res, authOptions(req, res));
|
|
if (
|
|
typeof session?.user?.id !== "string" ||
|
|
session.user.id.length === 0
|
|
) {
|
|
return unauthenticated(res);
|
|
}
|
|
|
|
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 &&
|
|
session.user.id !== containerID
|
|
) {
|
|
return forbidden(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"
|
|
});
|
|
}
|
|
|
|
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;
|