File size: 1,092 Bytes
1b2baa8
 
 
 
 
 
 
 
 
 
6231209
1b2baa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
}