Login button with show-password, skip identical photos on upload
A Přihlásit/Odhlásit button in the top bar replaces logging in via Přidat hru. The dialog has an eye toggle, says when it is checking, and keeps the remember box as it was after a wrong password; nothing is stored until the server accepts it. A remembered password that no longer works is dropped on load. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HA82gVRDXpoukzgMFumoJt
This commit is contained in:
parent
ab842a1d73
commit
6240907809
6 changed files with 138 additions and 44 deletions
|
|
@ -1,22 +1,30 @@
|
|||
import { api, withLogin, getPassword, askPassword, esc, $ } from "./api.js";
|
||||
import { api, withLogin, getPassword, askPassword, authButton, esc, $ } from "./api.js";
|
||||
import { toJpeg, fingerprint } from "./photos.js";
|
||||
|
||||
// The API reads images at most ~1.15 megapixels, so a bigger upload only costs bandwidth.
|
||||
const MAX_PIXELS = 1.15e6;
|
||||
const MAX_SIDE = 1568;
|
||||
|
||||
let files = []; // [{ file, url }]
|
||||
let files = []; // [{ file, url, hash }]
|
||||
let cfg;
|
||||
|
||||
const collator = new Intl.Collator("cs", { numeric: true, sensitivity: "base" });
|
||||
|
||||
function addFiles(list) {
|
||||
// Add picked photos, skipping any that are byte-for-byte the same as one already
|
||||
// picked. (Two different photos of the same page are sorted out after transcription.)
|
||||
async function addFiles(list) {
|
||||
const imgs = [...list].filter((f) => f.type.startsWith("image/") || /\.(heic|heif)$/i.test(f.name));
|
||||
files.push(...imgs.map((file) => ({ file, url: URL.createObjectURL(file) })));
|
||||
const skipped = [];
|
||||
for (const file of imgs) {
|
||||
const hash = await fingerprint(file);
|
||||
const same = files.find((f) => f.hash === hash);
|
||||
if (same) { skipped.push(`„${file.name}“ je stejná fotka jako „${same.file.name}“`); continue; }
|
||||
files.push({ file, hash, url: URL.createObjectURL(file) });
|
||||
}
|
||||
files.sort((a, b) => collator.compare(a.file.name, b.file.name));
|
||||
const notes = skipped.length ? [`Přeskočeno ${skipped.length === 1 ? "1 opakování" : `${skipped.length} opakování`}: ${skipped.join("; ")}.`] : [];
|
||||
if (files.length > cfg.maxPages) {
|
||||
showError(`Najednou lze nahrát nejvýše ${cfg.maxPages} stran.`);
|
||||
notes.push(`Najednou lze nahrát nejvýše ${cfg.maxPages} stran.`);
|
||||
files.splice(cfg.maxPages).forEach((f) => URL.revokeObjectURL(f.url));
|
||||
}
|
||||
$("notice").textContent = notes.join(" ");
|
||||
$("notice").hidden = !notes.length;
|
||||
renderThumbs();
|
||||
}
|
||||
|
||||
|
|
@ -35,20 +43,6 @@ function renderThumbs() {
|
|||
$("go").disabled = !files.length;
|
||||
}
|
||||
|
||||
// Decode, shrink and re-encode as JPEG (also strips EXIF, e.g. location).
|
||||
async function toJpeg(file) {
|
||||
let bitmap;
|
||||
try { bitmap = await createImageBitmap(file, { imageOrientation: "from-image" }); }
|
||||
catch { throw new Error(`Obrázek „${file.name}“ se nepodařilo otevřít${/\.hei[cf]$/i.test(file.name) ? " (formát HEIC – zkuste fotku uložit jako JPEG)" : ""}.`); }
|
||||
const scale = Math.min(1, MAX_SIDE / Math.max(bitmap.width, bitmap.height), Math.sqrt(MAX_PIXELS / (bitmap.width * bitmap.height)));
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = Math.round(bitmap.width * scale);
|
||||
canvas.height = Math.round(bitmap.height * scale);
|
||||
canvas.getContext("2d").drawImage(bitmap, 0, 0, canvas.width, canvas.height);
|
||||
bitmap.close();
|
||||
return new Promise((resolve, reject) => canvas.toBlob((b) => (b ? resolve(b) : reject(new Error("Převod obrázku selhal."))), "image/jpeg", 0.88));
|
||||
}
|
||||
|
||||
function showError(msg) { $("error").textContent = msg; $("error").hidden = !msg; }
|
||||
function setStatus(text, value) {
|
||||
$("progress").hidden = false;
|
||||
|
|
@ -108,7 +102,8 @@ async function main() {
|
|||
$("ai-note").textContent = "Automatický přepis není na serveru zapnutý (chybí klíč OPENROUTER_API_KEY nebo ANTHROPIC_API_KEY). Fotky se nahrají a text pak napíšete v editoru ručně.";
|
||||
$("go").textContent = "Nahrát";
|
||||
}
|
||||
if (!getPassword()) askPassword("Přidávat hry může jen ten, kdo zná heslo.");
|
||||
await authButton($("auth"), { uploads: true, onChange: (inn) => { if (!inn) location.replace("./"); } });
|
||||
if (!getPassword() && !(await askPassword("Přidávat hry může jen ten, kdo zná heslo."))) { location.replace("./"); return; }
|
||||
|
||||
$("files").addEventListener("change", (e) => { addFiles(e.target.files); e.target.value = ""; });
|
||||
const drop = $("drop");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue