Scans and photos of a person's papers, and a company's documents for the extension packs, uploaded on a dashed strip that takes dropped files: up to 10 files of 20 MB each, 64 MB in all; PDF, JPEG, PNG and WebP, told by their first bytes. - src/files.rs: one file per document as <id>.vdoc, AES-256-GCM under a key per file (HKDF-SHA256 from the file key and a random salt in the header); the document's id is authenticated with it. The key comes from VISA_FILES_KEY_FILE or the credential files-key; with the login on, the app does not start without it and VISA_FILES_DIR. - migrations/0003: documents (a person's or a company's, the record it belongs to, kind, date on it, note, file name, type, size, SHA-256); audit_log gets company_id and the action download. - A Documents tab per person, the documents on the company's page, and each record's documents next to its Edit link with "+ Document". - An upload's form token is its first field and is checked before any file is read; the guard lets multipart through only to the two upload addresses. - Files open under their own CSP and are never cached; a download is named after the person or company, the kind and the date. Every upload, view, download, change and deletion is in the audit log. - visa files-check; demo documents in seed-demo; scripts/dev.sh keeps the local documents and their key in .dev/; /favicon.ico for image and PDF pages. Tests: 57 unit and 33 integration (8 new for documents). Edge check of the strip (choose, drop, too large), an upload, the PDF and image viewers under the file policy: no problems, no console errors.
143 lines
5.0 KiB
JavaScript
143 lines
5.0 KiB
JavaScript
// Theme switch, the list filter, delete confirmations and the upload strip.
|
|
// Event delegation and no inline handlers: the Content Security Policy
|
|
// allows no inline script.
|
|
(function () {
|
|
"use strict";
|
|
|
|
// ---- theme: <html data-theme="light|dark">, kept in localStorage
|
|
document.addEventListener("click", function (ev) {
|
|
var b = ev.target.closest && ev.target.closest("#themeBtn");
|
|
if (!b) return;
|
|
var root = document.documentElement;
|
|
var next = root.getAttribute("data-theme") === "light" ? "dark" : "light";
|
|
root.setAttribute("data-theme", next);
|
|
try {
|
|
localStorage.setItem("visa-theme", next);
|
|
} catch (e) {
|
|
/* storage blocked */
|
|
}
|
|
});
|
|
|
|
// ---- forms with data-confirm ask first (archive, delete)
|
|
document.addEventListener("submit", function (ev) {
|
|
var f = ev.target;
|
|
var msg = f && f.getAttribute && f.getAttribute("data-confirm");
|
|
if (msg && !window.confirm(msg)) ev.preventDefault();
|
|
});
|
|
|
|
// ---- a selection with data-autosubmit filters the page at once
|
|
document.addEventListener("change", function (ev) {
|
|
var t = ev.target;
|
|
if (t && t.hasAttribute && t.hasAttribute("data-autosubmit") && t.form) {
|
|
t.form.submit();
|
|
}
|
|
});
|
|
|
|
// ---- filter: rows carry data-q (lower case); "/" focuses the box
|
|
function applyFilter() {
|
|
var box = document.getElementById("q");
|
|
if (!box) return;
|
|
var q = box.value.trim().toLowerCase();
|
|
var rows = document.querySelectorAll("[data-q]");
|
|
var shown = 0;
|
|
rows.forEach(function (el) {
|
|
var hit = q === "" || el.getAttribute("data-q").indexOf(q) !== -1;
|
|
el.hidden = !hit;
|
|
if (hit) shown++;
|
|
});
|
|
document.querySelectorAll("[data-filter-empty]").forEach(function (el) {
|
|
el.hidden = q === "" || rows.length === 0 || shown > 0;
|
|
});
|
|
}
|
|
document.addEventListener("input", function (ev) {
|
|
if (ev.target && ev.target.id === "q") applyFilter();
|
|
});
|
|
document.addEventListener("keydown", function (ev) {
|
|
var box = document.getElementById("q");
|
|
if (!box) return;
|
|
var tag = document.activeElement ? document.activeElement.tagName : "";
|
|
if (ev.key === "/" && !/^(INPUT|TEXTAREA|SELECT)$/.test(tag)) {
|
|
ev.preventDefault();
|
|
box.focus();
|
|
} else if (ev.key === "Escape" && document.activeElement === box) {
|
|
box.value = "";
|
|
applyFilter();
|
|
box.blur();
|
|
}
|
|
});
|
|
document.addEventListener("DOMContentLoaded", applyFilter);
|
|
|
|
// ---- uploads: the file input covers the dashed strip, so a click or a
|
|
// drop lands on it; this shows the chosen names (and any too large), lights
|
|
// the strip while files are dragged over it, and keeps a file dropped
|
|
// beside it from replacing the page.
|
|
function showFiles(input) {
|
|
var strip = input.closest(".drop");
|
|
var out = strip && strip.querySelector("[data-drop-names]");
|
|
if (!out) return;
|
|
var max = parseInt(strip.getAttribute("data-max"), 10) || 0;
|
|
var names = [];
|
|
var big = [];
|
|
Array.prototype.forEach.call(input.files || [], function (f) {
|
|
names.push(f.name);
|
|
if (max && f.size > max) big.push(f.name);
|
|
});
|
|
out.textContent = big.length
|
|
? big.join(", ") + (big.length === 1 ? " is" : " are") + " too large"
|
|
: names.join(", ");
|
|
out.classList.toggle("bad", big.length > 0);
|
|
}
|
|
document.addEventListener("change", function (ev) {
|
|
var t = ev.target;
|
|
if (t && t.matches && t.matches(".drop input[type=file]")) showFiles(t);
|
|
});
|
|
function stripOf(ev) {
|
|
return ev.target && ev.target.closest ? ev.target.closest(".drop") : null;
|
|
}
|
|
document.addEventListener("dragover", function (ev) {
|
|
var s = stripOf(ev);
|
|
if (s) {
|
|
s.classList.add("over");
|
|
} else if (document.querySelector(".drop")) {
|
|
ev.preventDefault();
|
|
ev.dataTransfer.dropEffect = "none";
|
|
}
|
|
});
|
|
document.addEventListener("dragleave", function (ev) {
|
|
var s = stripOf(ev);
|
|
if (s) s.classList.remove("over");
|
|
});
|
|
document.addEventListener("drop", function (ev) {
|
|
var s = stripOf(ev);
|
|
if (s) {
|
|
s.classList.remove("over");
|
|
// the files go to the input here, the same in every browser
|
|
var input = s.querySelector("input[type=file]");
|
|
var dropped = ev.dataTransfer && ev.dataTransfer.files;
|
|
if (input && dropped && dropped.length) {
|
|
ev.preventDefault();
|
|
input.files = dropped;
|
|
showFiles(input);
|
|
}
|
|
} else if (document.querySelector(".drop")) {
|
|
ev.preventDefault();
|
|
}
|
|
});
|
|
// one upload at a time: the button waits for the answer
|
|
document.addEventListener("submit", function (ev) {
|
|
var f = ev.target;
|
|
if (ev.defaultPrevented || !f.classList || !f.classList.contains("upload")) return;
|
|
var b = f.querySelector("button[type=submit]");
|
|
if (b) {
|
|
b.disabled = true;
|
|
b.textContent = "Uploading...";
|
|
}
|
|
});
|
|
window.addEventListener("pageshow", function () {
|
|
document.querySelectorAll(".upload button[type=submit]").forEach(function (b) {
|
|
b.disabled = false;
|
|
b.textContent = "Upload";
|
|
});
|
|
});
|
|
})();
|