push to maple-street
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Upload storage helpers. Files are written under `UPLOAD_DIR`
|
||||
* (default `/tmp/uploads`, a named Docker volume in the deployment) and only
|
||||
* metadata is stored in Postgres.
|
||||
*/
|
||||
|
||||
import path from "node:path";
|
||||
import { mkdir } from "node:fs/promises";
|
||||
|
||||
export const UPLOAD_DIR = process.env.UPLOAD_DIR ?? "/tmp/uploads";
|
||||
|
||||
export const MAX_UPLOAD_BYTES = 5 * 1024 * 1024;
|
||||
|
||||
const ALLOWED_MIME_PREFIXES = ["image/"];
|
||||
const ALLOWED_MIME_TYPES = [
|
||||
"application/pdf",
|
||||
"text/plain",
|
||||
"text/markdown",
|
||||
"application/msword",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
];
|
||||
|
||||
export function isAllowedUpload(mimeType: string): boolean {
|
||||
return (
|
||||
ALLOWED_MIME_PREFIXES.some((prefix) => mimeType.startsWith(prefix)) ||
|
||||
ALLOWED_MIME_TYPES.includes(mimeType)
|
||||
);
|
||||
}
|
||||
|
||||
function startsWith(bytes: Uint8Array, signature: number[]): boolean {
|
||||
return signature.every((value, index) => bytes[index] === value);
|
||||
}
|
||||
|
||||
function ascii(bytes: Uint8Array, start: number, length: number): string {
|
||||
return String.fromCharCode(...bytes.slice(start, start + length));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that the bytes plausibly match the declared MIME type. Client-provided
|
||||
* `File.type` is advisory; without this an upload could claim `image/png` and
|
||||
* deliver an HTML/SVG payload. Text formats have no magic number, so they are
|
||||
* accepted when the content is NUL-free (a binary masquerading as text fails).
|
||||
*/
|
||||
export function uploadSignatureMatches(
|
||||
mimeType: string,
|
||||
bytes: Uint8Array,
|
||||
): boolean {
|
||||
const head = bytes.slice(0, 16);
|
||||
const hasNul = head.includes(0);
|
||||
|
||||
if (mimeType === "application/pdf") return ascii(head, 0, 5) === "%PDF-";
|
||||
if (mimeType === "image/png") {
|
||||
return startsWith(head, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
||||
}
|
||||
if (mimeType === "image/jpeg") return startsWith(head, [0xff, 0xd8, 0xff]);
|
||||
if (mimeType === "image/gif") {
|
||||
return ascii(head, 0, 4) === "GIF8";
|
||||
}
|
||||
if (mimeType === "image/webp") {
|
||||
return ascii(head, 0, 4) === "RIFF" && ascii(head, 8, 4) === "WEBP";
|
||||
}
|
||||
if (mimeType.startsWith("image/")) return !hasNul;
|
||||
if (
|
||||
mimeType ===
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
) {
|
||||
return startsWith(head, [0x50, 0x4b, 0x03, 0x04]);
|
||||
}
|
||||
if (mimeType === "application/msword") {
|
||||
return startsWith(head, [0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1]);
|
||||
}
|
||||
return !hasNul;
|
||||
}
|
||||
|
||||
/** A random on-disk name: UUIDv7 + a sanitized extension from the original. */
|
||||
export function storedNameFor(originalName: string, id: string): string {
|
||||
const ext = path.extname(originalName).toLowerCase().replace(/[^.a-z0-9]/g, "");
|
||||
return `${id}${ext}`;
|
||||
}
|
||||
|
||||
export async function ensureUploadDir(): Promise<void> {
|
||||
await mkdir(UPLOAD_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
/** Resolve a stored name inside UPLOAD_DIR, refusing path traversal. */
|
||||
export function uploadPath(storedName: string): string {
|
||||
return path.join(UPLOAD_DIR, path.basename(storedName));
|
||||
}
|
||||
Reference in New Issue
Block a user