import { hashAPIPath } from "../../../actions/core/hash"; import { deleteBlob } from "../../../actions/azurestorage"; import { respondError, respondSuccess } from "../middleware/apiResponse"; import nextConnect from "next-connect"; import middleware from "../middleware/middleware"; const ApiProxy = nextConnect(); ApiProxy.use(middleware); ApiProxy.get(async (req, res) => { const containerName = req.query.container; const casefolderID = req.query.casefolderID; const blobName = req.query.blobname; const checkHash = req.query.hash; if ( typeof containerName === "undefined" || containerName.length === 0 || typeof casefolderID === "undefined" || casefolderID.length === 0 || typeof blobName === "undefined" || blobName.length === 0 || typeof checkHash === "undefined" || checkHash.length === 0 ) { return respondError(res, { status: 400, code: "MISSING_REQUIRED_QUERY", message: "container, casefolderID, blobname and hash are required" }); } const casefolderIDTrimmed = casefolderID.trim(); const blobNameTrimmed = blobName.trim(); const hashCandidatePaths = [ "/api/file/deleteawaitingsubmissionfromblob?container=" + containerName + "&casefolderID=" + casefolderIDTrimmed + "&blobname=" + blobNameTrimmed, "/api/file/deleteawaitingsubmissionfromblob?container=" + containerName + "&casefolderID=" + encodeURIComponent(casefolderIDTrimmed) + "&blobname=" + encodeURIComponent(blobNameTrimmed), // legacy compatibility with callers that hash against deleteblob route "/api/file/deleteblob?container=" + containerName + "&casefolderID=" + casefolderIDTrimmed + "&blobname=" + blobNameTrimmed, "/api/file/deleteblob?container=" + containerName + "&casefolderID=" + encodeURIComponent(casefolderIDTrimmed) + "&blobname=" + encodeURIComponent(blobNameTrimmed) ]; const isHashValid = hashCandidatePaths.some( (candidatePath) => hashAPIPath(candidatePath) == "&hash=" + checkHash ); if (!isHashValid) { return respondError(res, { status: 400, code: "INVALID_HASH", message: "Invalid hash" }); } try { const normalizedBlobName = blobNameTrimmed.startsWith( casefolderIDTrimmed + "/" ) ? blobNameTrimmed : casefolderIDTrimmed + "/files/" + blobNameTrimmed; const data = await deleteBlob(containerName, normalizedBlobName); return respondSuccess(res, { data: data }); } catch (error) { return respondError(res, { status: 400, code: "DELETE_AWAITING_SUBMISSION_BLOB_FAILED", message: "Unable to delete awaiting submission blob" }); } }); export const config = { api: { bodyParser: false } }; export default ApiProxy;