Trim comments to the non-obvious why
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]>
This commit is contained in:
+11
-49
@@ -1,34 +1,11 @@
|
||||
/**
|
||||
* Uploaded files: the one module to use whenever the frontend stores a file
|
||||
* or shows one it stored.
|
||||
*
|
||||
* import { uploadFile, ACCEPT, kindOf, tagFor } from "@/uploads";
|
||||
*
|
||||
* <input type="file" :accept="ACCEPT.images" @change="…">
|
||||
* const url = await uploadFile(file); // "/uploads/3f9c….png"
|
||||
* kindOf(url) // "image"
|
||||
*
|
||||
* Files go to POST /api/uploads (backend/handlers/handle_upload.go). The
|
||||
* backend decides everything that matters: the allowed types and the 10MB
|
||||
* limit (backend/services/uploads.go, UploadTypes), the random file name, and
|
||||
* where it is stored (the Garage bucket, falling back to disk). The URL it
|
||||
* returns is permanent and public — anyone with it can load the file, no login
|
||||
* — so never upload something that shouldn't be public unless it is a private
|
||||
* chat attachment (`{ private: true }`, admin only).
|
||||
*
|
||||
* A refused file (wrong type, too large) rejects with an ApiError whose
|
||||
* `message` is the backend's sentence, ready to show the user.
|
||||
*
|
||||
* listUploads() lists what is already stored, for reusing a file instead of
|
||||
* uploading it again (components/util/UploadPicker.vue). It is admin-only:
|
||||
* the listing is what would turn every unguessable URL into a known one.
|
||||
* 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";
|
||||
|
||||
/**
|
||||
* `accept` values for file pickers, matching what the backend allows. They
|
||||
* only steer the picker; the backend enforces the real list.
|
||||
*/
|
||||
/** 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",
|
||||
@@ -36,15 +13,10 @@ export const ACCEPT = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Store one file; resolves to its URL.
|
||||
*
|
||||
* @param {File} file
|
||||
* @param {object} [options]
|
||||
* @param {boolean} [options.private] admin-only: store it under
|
||||
* /uploads/private/, readable only by admins. Never use it for anything
|
||||
* shown inside a canvas box — a box's sandboxed frame loads without the
|
||||
* login cookie, so the file would not load.
|
||||
* @returns {Promise<string>} "/uploads/<name>" or "/uploads/private/<name>"
|
||||
* @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();
|
||||
@@ -54,11 +26,7 @@ export async function uploadFile(file, { private: isPrivate = false } = {}) {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every public upload, newest first. Admin only.
|
||||
*
|
||||
* @returns {Promise<Array<{name: string, url: string, size: number, modified: string}>>}
|
||||
*/
|
||||
/** 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;
|
||||
@@ -76,11 +44,8 @@ export const isImage = (url) => kindOf(url) === "image";
|
||||
export const isVideo = (url) => kindOf(url) === "video";
|
||||
|
||||
/**
|
||||
* Whether a URL is one of our own uploads. Use it before rendering a URL that
|
||||
* came from somebody else (a chat message's fileUrl arrives over the socket
|
||||
* and is otherwise attacker-controlled). A strict prefix check, so absolute
|
||||
* ("https://evil/uploads/x") and protocol-relative ("//evil/uploads/x") URLs
|
||||
* fail, and ".." is refused outright.
|
||||
* 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/");
|
||||
@@ -100,10 +65,7 @@ export function tagFor(url) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Natural size of an image or video — a File, or the URL of one already
|
||||
* stored — or null if it can't be read.
|
||||
*/
|
||||
/** 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";
|
||||
|
||||
Reference in New Issue
Block a user