SPB Git

Repository Public

index.ts 3.1 KB 74 lines typescript

1/**2 * Media storage boundary. The site only ever asks a `MediaStorage` for bytes by key; today the only3 * implementation is the local filesystem under ATLAS_DATA_DIR. An S3-compatible implementation can be4 * added later without touching the routes.5 */6import { createReadStream } from "node:fs";7import { realpath, stat } from "node:fs/promises";8import { resolve, sep } from "node:path";9import type { Readable } from "node:stream";10import { isSafeRelPath } from "../content-schema/schema";1112export interface StoredObject { size: number; mtimeMs: number }13export interface ByteRange { start: number; end: number }1415export interface MediaStorage {16  /** null when the key is unsafe or the object does not exist. */17  stat(key: string): Promise<StoredObject | null>;18  read(key: string, range?: ByteRange): Promise<Readable | null>;19}2021export class FsStorage implements MediaStorage {22  constructor(private readonly root: string) {}2324  /** Resolves a key to a real path inside the root, following symlinks; null if it escapes. */25  private async locate(key: string): Promise<string | null> {26    if (!isSafeRelPath(key)) return null;27    const root = resolve(/* turbopackIgnore: true */ this.root);28    const target = resolve(/* turbopackIgnore: true */ root, key);29    if (!target.startsWith(root + sep)) return null;30    try {31      const [realRoot, real] = await Promise.all([realpath(root), realpath(target)]);32      return real.startsWith(realRoot + sep) ? real : null;33    } catch { return null; }34  }3536  async stat(key: string): Promise<StoredObject | null> {37    const path = await this.locate(key);38    if (!path) return null;39    const s = await stat(path).catch(() => null);40    return s?.isFile() ? { size: s.size, mtimeMs: s.mtimeMs } : null;41  }4243  async read(key: string, range?: ByteRange): Promise<Readable | null> {44    const path = await this.locate(key);45    return path ? createReadStream(path, range) : null;46  }47}4849/** Parses a single-range `Range` header. Returns "invalid" for unsatisfiable or multi-range requests. */50export function parseRange(header: string | null, size: number): ByteRange | null | "invalid" {51  if (!header) return null;52  const m = /^bytes=(\d*)-(\d*)$/.exec(header.trim());53  if (!m || (m[1] === "" && m[2] === "")) return "invalid";54  if (m[1] === "") {55    const n = Number(m[2]);56    if (n <= 0) return "invalid";57    return { start: Math.max(0, size - n), end: size - 1 };58  }59  const start = Number(m[1]), end = m[2] === "" ? size - 1 : Math.min(Number(m[2]), size - 1);60  if (start >= size || start > end) return "invalid";61  return { start, end };62}6364const MIME: Record<string, string> = {65  m3u8: "application/vnd.apple.mpegurl", ts: "video/mp2t", m4s: "video/iso.segment", mp4: "video/mp4",66  vtt: "text/vtt; charset=utf-8", json: "application/json; charset=utf-8", jpg: "image/jpeg", jpeg: "image/jpeg",67  png: "image/png", webp: "image/webp", svg: "image/svg+xml", pdf: "application/pdf",68};6970/** Only known media types are ever served; anything else is refused by the routes. */71export function mimeOf(key: string): string | null {72  return MIME[key.split(".").pop()?.toLowerCase() ?? ""] ?? null;73}74
Files main
Menu

Theme