import s from"path";import{REDOCLY_ROUTE_RBAC as C}from"@redocly/config";import{PUBLIC_STATIC_FOLDER as r}from"../../constants/common.js";import{fileExistsAsync as g}from"../../utils/index.js";import{isPathInFolder as h}from"../../../shared/utils.js";import{handleUnauthorizedAsset as w}from"../utils.js";import{MIME_TYPES as E}from"../mime-types.js";import{canAccessResource as R}from"../../utils/rbac.js";const O=async(t,n,o,a)=>{const c=n.req,{isAuthenticated:m,teams:l,claims:{email:u}}=n.get("auth"),i=s.join(r,r);t.startsWith(i)&&(t=t.replace(i,r));const e=s.resolve(o.outdir,t);if(!h(e,o.outdir))return null;if(await g(e)){const p={[C]:{slug:t,fsPath:e},slug:t};if(!R(p,{isAuthenticated:m,email:u,teams:l},o.config.rbac,o.config.requiresLogin))return w(n);const f=c.query("download")!=null,d=E[s.extname(e)]||"text/plain",A=await a(e);return n.newResponse(A,200,{"Content-Type":d,"Access-Control-Allow-Origin":"*",...f&&{"Content-Disposition":`attachment; filename="${s.basename(e)}"`}})}else return null};export{O as staticContentHandler};
