- Replace the static nginx site with a small Node server: serves public/, stores plays on a /data volume and exposes a JSON API. Writes need a shared UPLOAD_PASSWORD; unpublished plays are hidden without it. - Page photos (shrunk to <=1.15 MP JPEG in the browser) are transcribed one request per page by a Claude vision model (claude-haiku-5-5 by default, CLAUDE_MODEL to change) into the play markup, then stitched together: speaker spellings unified, gaps in page numbers marked as missing pages, failed pages flagged. DAILY_PAGE_LIMIT caps spend. - New pages: library (play menu), add (upload + progress), editor (text next to the photos, checks for [?] readings and one-off names, publish, delete). The reader loads plays from the API and remembers the chosen role per play; old /?postava= links still work. - PDF export: text now flows across original pages and prints at the on-screen size, so A4 sheets are full instead of half empty; page numbers in the footer. - docker-compose.yml + .env.example for one-click deploys; the 1931 play moves to seed/ and is copied into an empty volume on first start. - Tests: parser leniency, transcription assembly, API end to end with a stubbed Claude client. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HA82gVRDXpoukzgMFumoJt
95 lines
3.9 KiB
JavaScript
95 lines
3.9 KiB
JavaScript
// Small helpers shared by the pages: API calls and the upload password.
|
|
const PASS_KEY = "theater-reader:password";
|
|
|
|
export function getPassword() {
|
|
try { return sessionStorage.getItem(PASS_KEY) || localStorage.getItem(PASS_KEY) || ""; } catch { return ""; }
|
|
}
|
|
export function setPassword(pw, remember) {
|
|
try {
|
|
sessionStorage.setItem(PASS_KEY, pw);
|
|
if (remember) localStorage.setItem(PASS_KEY, pw); else localStorage.removeItem(PASS_KEY);
|
|
} catch { /* storage blocked: the password just won't be remembered */ }
|
|
}
|
|
export function clearPassword() {
|
|
try { sessionStorage.removeItem(PASS_KEY); localStorage.removeItem(PASS_KEY); } catch { /* ignore */ }
|
|
}
|
|
|
|
export class ApiError extends Error {
|
|
constructor(status, message) { super(message); this.status = status; }
|
|
}
|
|
|
|
// fetch wrapper: sends the password when we have one, throws ApiError with the server's message.
|
|
export async function api(path, { method = "GET", body, type, raw = false } = {}) {
|
|
const headers = {};
|
|
const pw = getPassword();
|
|
if (pw) headers["x-upload-password"] = pw;
|
|
if (body !== undefined && !(body instanceof Blob) && typeof body !== "string") {
|
|
body = JSON.stringify(body);
|
|
headers["content-type"] = "application/json";
|
|
} else if (type) headers["content-type"] = type;
|
|
const res = await fetch(`api/${path}`, { method, headers, body });
|
|
if (!res.ok) {
|
|
let msg = `HTTP ${res.status}`;
|
|
try { msg = (await res.json()).error || msg; } catch { /* not JSON */ }
|
|
throw new ApiError(res.status, msg);
|
|
}
|
|
if (raw) return res;
|
|
return (res.headers.get("content-type") || "").includes("json") ? res.json() : res.text();
|
|
}
|
|
|
|
export async function login(pw, remember) {
|
|
const headers = { "x-upload-password": pw };
|
|
const res = await fetch("api/login", { method: "POST", headers });
|
|
if (!res.ok) {
|
|
let msg = "Přihlášení se nepovedlo.";
|
|
try { msg = (await res.json()).error || msg; } catch { /* ignore */ }
|
|
throw new ApiError(res.status, msg);
|
|
}
|
|
setPassword(pw, remember);
|
|
}
|
|
|
|
export const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
|
|
export const $ = (id) => document.getElementById(id);
|
|
|
|
// Ask for the upload password in a modal dialog; resolves true once logged in, false if cancelled.
|
|
export function askPassword(reason = "") {
|
|
return new Promise((resolve) => {
|
|
const dlg = document.createElement("dialog");
|
|
dlg.className = "login";
|
|
dlg.innerHTML = `<form method="dialog">
|
|
<h2>Heslo pro úpravy</h2>
|
|
${reason ? `<p class="muted">${esc(reason)}</p>` : ""}
|
|
<label class="field"><span>Heslo</span><input type="password" name="pw" autocomplete="current-password" required></label>
|
|
<label class="radio"><input type="checkbox" name="remember"> Zapamatovat na tomto zařízení</label>
|
|
<p class="error" hidden></p>
|
|
<div class="actions"><button value="cancel" formnovalidate>Zrušit</button><button class="primary" value="ok">Přihlásit</button></div>
|
|
</form>`;
|
|
document.body.append(dlg);
|
|
const form = dlg.querySelector("form");
|
|
const err = dlg.querySelector(".error");
|
|
form.addEventListener("submit", async (e) => {
|
|
if (e.submitter?.value !== "ok") return;
|
|
e.preventDefault();
|
|
try {
|
|
await login(form.pw.value, form.remember.checked);
|
|
dlg.close("ok");
|
|
} catch (ex) {
|
|
err.textContent = ex.message;
|
|
err.hidden = false;
|
|
}
|
|
});
|
|
dlg.addEventListener("close", () => { dlg.remove(); resolve(dlg.returnValue === "ok"); });
|
|
dlg.showModal();
|
|
});
|
|
}
|
|
|
|
// Run fn; on 401 ask for the password and retry once.
|
|
export async function withLogin(fn, reason) {
|
|
try { return await fn(); }
|
|
catch (err) {
|
|
if (!(err instanceof ApiError) || err.status !== 401) throw err;
|
|
clearPassword();
|
|
if (!(await askPassword(reason || "Pro tuto akci je potřeba heslo."))) throw err;
|
|
return fn();
|
|
}
|
|
}
|