/** * @swagger * /api/file/getrepsblob: * get: * tags: [Azure Storage] * summary: Phase 2 * description: get reps blobs * parameters: * - name: container * in: query * required: true * example: cl761h97h0008h11ycmu2qyfo * schema: * type: string * - name: casefolderID * in: query * required: true * example: TMP-AYJHX-SNW1H5 * schema: * type: string * - name: hash * in: query * required: true * example: 372c59f86d62b57cc2a7981eeee7737c944d407c6a7f2ea9d85797bbfe9852c5 * schema: * type: string * responses: * 200: * description: Success */ import { downloadAllRepsFiles, getRepsBlobs } from "../../../actions/azurestorage"; import nextConnect from "next-connect"; import middleware from "../middleware/middleware"; import { hashAPIPath } from "../../../actions/core/hash"; import { consoleLogger } from "../../../actions/core/logger"; import { respondError, respondSuccess } from "../middleware/apiResponse"; import { getServerSession } from "next-auth/next"; import { authOptions } from "../auth/[...nextauth]"; import { readAdminSessionCookie, validateAdminSession } from "../../../lib/admin/adminAuth"; const ApiProxy = nextConnect(); ApiProxy.use(middleware); const forbidden = (res) => respondError(res, { status: 403, code: "FORBIDDEN", message: "Request not permitted" }); ApiProxy.get(async (req, res) => { const containerName = req.query.container; const checkHash = req.query.hash; if ( typeof containerName === "undefined" || containerName.length === 0 || typeof checkHash === "undefined" || checkHash.length === 0 ) { return respondError(res, { status: 400, code: "MISSING_REQUIRED_QUERY", message: "container and hash are required" }); } const session = await getServerSession(req, res, authOptions(req, res)); const isPortalOwner = typeof session?.user?.id === "string" && session.user.id.length > 0 && session.user.id === containerName; const isAdministrator = validateAdminSession(readAdminSessionCookie(req)); if (!isPortalOwner && !isAdministrator) { return forbidden(res); } const hashCandidatePaths = [ "/api/file/getrepsblob?container=" + containerName, "/api/file/getrepsblob?container=" + encodeURIComponent(containerName) ]; const isHashValid = hashCandidatePaths.some( (candidatePath) => hashAPIPath(candidatePath) == "&hash=" + checkHash ); if (!isHashValid) { return respondError(res, { status: 400, code: "INVALID_HASH", message: "Invalid hash" }); } try { const repsBlobs = await getRepsBlobs(containerName); const result = await downloadAllRepsFiles(containerName, repsBlobs); res.setHeader("Cache-Control", "no-store"); return respondSuccess(res, result); } catch (error) { consoleLogger(error); return respondError(res, { status: 400, code: "GET_REPS_BLOB_FAILED", message: "Failed to retrieve representation blobs" }); } }); export default ApiProxy;