Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
90 lines
3.2 KiB
JavaScript
90 lines
3.2 KiB
JavaScript
/**
|
|
* Every frontend upload goes through here. The backend enforces types, size
|
|
* and naming (backend/services/uploads.go). Returned URLs are public to anyone
|
|
* who has them. A refusal rejects with an ApiError carrying a showable message.
|
|
*/
|
|
import { api } from "@/api";
|
|
|
|
/** Picker hints only; the backend enforces the real list. */
|
|
export const ACCEPT = {
|
|
images: "image/jpeg,image/png,image/gif,image/webp",
|
|
media: "image/jpeg,image/png,image/gif,image/webp,video/mp4,video/webm,audio/mpeg,audio/ogg",
|
|
any: "image/jpeg,image/png,image/gif,image/webp,video/mp4,video/webm,audio/mpeg,audio/ogg,application/pdf,text/plain",
|
|
};
|
|
|
|
/**
|
|
* @param {object} [options]
|
|
* @param {boolean} [options.private] admin-only. Never for canvas boxes: their
|
|
* sandboxed frames load without the login cookie.
|
|
* @returns {Promise<string>} the file's URL
|
|
*/
|
|
export async function uploadFile(file, { private: isPrivate = false } = {}) {
|
|
const body = new FormData();
|
|
body.append("file", file);
|
|
if (isPrivate) body.append("private", "true");
|
|
const { url } = await api.post("/uploads", body, { fallback: `Upload of ${file.name} failed` });
|
|
return url;
|
|
}
|
|
|
|
/** Admin only: the listing would reveal every unguessable URL. */
|
|
export async function listUploads() {
|
|
const { files } = await api.get("/uploads", { fallback: "Couldn't list uploads" });
|
|
return files;
|
|
}
|
|
|
|
/** What a stored file is, from its URL: "image", "video", "audio" or "file". */
|
|
export function kindOf(url) {
|
|
if (/\.(jpe?g|png|gif|webp)$/i.test(url)) return "image";
|
|
if (/\.(mp4|webm)$/i.test(url)) return "video";
|
|
if (/\.(mp3|ogg)$/i.test(url)) return "audio";
|
|
return "file";
|
|
}
|
|
|
|
export const isImage = (url) => kindOf(url) === "image";
|
|
export const isVideo = (url) => kindOf(url) === "video";
|
|
|
|
/**
|
|
* Use before rendering an untrusted URL (e.g. a chat fileUrl). Absolute,
|
|
* protocol-relative and ".." URLs fail.
|
|
*/
|
|
export function isUploadUrl(url) {
|
|
return typeof url === "string" && !url.includes("..") && url.startsWith("/uploads/");
|
|
}
|
|
|
|
/** The markup a stored file is shown with in a box's HTML, by kind. */
|
|
export function tagFor(url) {
|
|
switch (kindOf(url)) {
|
|
case "image":
|
|
return `<img src="${url}" alt="">`;
|
|
case "video":
|
|
return `<video src="${url}" controls loop muted></video>`;
|
|
case "audio":
|
|
return `<audio src="${url}" controls></audio>`;
|
|
default:
|
|
return `<a href="${url}" target="_blank">${url.split("/").pop()}</a>`;
|
|
}
|
|
}
|
|
|
|
/** Natural size of an image or video (File or URL), or null. */
|
|
export function mediaSize(file) {
|
|
return new Promise((resolve) => {
|
|
const isFile = typeof file !== "string";
|
|
const src = isFile ? URL.createObjectURL(file) : file;
|
|
const done = (size) => {
|
|
if (isFile) URL.revokeObjectURL(src);
|
|
resolve(size);
|
|
};
|
|
if (isFile ? file.type.startsWith("video/") : isVideo(file)) {
|
|
const v = document.createElement("video");
|
|
v.onloadedmetadata = () => done({ w: v.videoWidth, h: v.videoHeight });
|
|
v.onerror = () => done(null);
|
|
v.src = src;
|
|
} else {
|
|
const img = new Image();
|
|
img.onload = () => done({ w: img.naturalWidth, h: img.naturalHeight });
|
|
img.onerror = () => done(null);
|
|
img.src = src;
|
|
}
|
|
});
|
|
}
|