Masters-four-Tab-OpenAI / frontend /src /utils /protectedFiles.ts
Pete Dunn
Sync canonical Router KB corpus, Router RAG module, eval harness, and app updates
6231209
Raw
History Blame Contribute Delete
1.09 kB
import { apiFetch } from "../api/client";
function withLeadingSlash(path: string): string {
const p = String(path || "").trim();
if (!p) return "/";
return p.startsWith("/") ? p : `/${p}`;
}
export function isProtectedDocPath(path: string): boolean {
const p = withLeadingSlash(path);
return p.startsWith("/masters_files/") || p.startsWith("/pots_files/") || p.startsWith("/router_rag_files/");
}
export async function openProtectedFile(path: string): Promise<void> {
const target = withLeadingSlash(path);
const response = await apiFetch(target, { method: "GET" });
if (!response.ok) {
throw new Error(`File request failed (${response.status}).`);
}
const blob = await response.blob();
const objectUrl = URL.createObjectURL(blob);
const popup = window.open(objectUrl, "_blank", "noopener,noreferrer");
if (!popup) {
const anchor = document.createElement("a");
anchor.href = objectUrl;
anchor.target = "_blank";
anchor.rel = "noopener noreferrer";
anchor.click();
}
window.setTimeout(() => URL.revokeObjectURL(objectUrl), 60_000);
}