// A LIBRARY'S CAPTURED MEDIA, FOUND FOR THE ONE ROUTE THAT SERVES IT (lane // posts-library): GET /api/library/media///. // // A trending library is PRIVATE. Every other reader of the corpus in umtool // refuses a library by its marker; this is the one that REQUIRES it, and it // can name exactly one kind of file: // // //trending/posts-media// // // lib a slug whose config.json carries the library marker // id digits only // name one path segment of [A-Za-z0-9._-], not starting with a dot, with a // picture's or a video's extension — so never capture.json, an // article's text, the raw reads, the judgements or the index // and the file's REAL path must sit under the real trending/posts-media of // that library: a name that is a symlink out of it is refused. (The media // files are hardlinks into the library's store, which are plain files.) // // It lists nothing: no directory, no index of ids. Plain ESM. import { realpath, stat } from "node:fs/promises"; import path from "node:path"; import { isTrendingLibrary } from "../report/xTrendingMarker.mjs"; const SLUG = /^[A-Za-z0-9][A-Za-z0-9._-]*$/; const ID = /^\d{1,32}$/; const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/; export const LIBRARY_MEDIA_TYPES = Object.freeze({ ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png", ".webp": "image/webp", ".gif": "image/gif", ".mp4": "video/mp4", ".m4v": "video/mp4", ".mov": "video/quicktime", ".webm": "video/webm", }); export { libraryMediaSrc } from "./mediaSrc.mjs"; /** * The file a media request names, or why not. * @param {{ channelsDir: string, lib: string, id: string, name: string }} o * @returns {Promise<{ ok: true, abs: string, size: number, type: string } | { ok: false, status: 400 | 404 }>} */ export async function resolveLibraryMedia({ channelsDir, lib, id, name }) { if (typeof lib !== "string" || typeof id !== "string" || typeof name !== "string") return { ok: false, status: 400 }; if (!SLUG.test(lib) || !ID.test(id) || !NAME.test(name) || name.includes("..")) return { ok: false, status: 400 }; const type = LIBRARY_MEDIA_TYPES[path.extname(name).toLowerCase()]; if (!type) return { ok: false, status: 404 }; if (!(await isTrendingLibrary(channelsDir, lib))) return { ok: false, status: 404 }; const root = path.join(/* turbopackIgnore: true */ channelsDir, lib, "trending", "posts-media"); const abs = path.join(/* turbopackIgnore: true */ root, id, name); const [real, realRoot] = await Promise.all([realpath(/* turbopackIgnore: true */ abs).catch(() => null), realpath(/* turbopackIgnore: true */ root).catch(() => null)]); if (!real || !realRoot || !real.startsWith(realRoot + path.sep)) return { ok: false, status: 404 }; const st = await stat(/* turbopackIgnore: true */ real).catch(() => null); if (!st?.isFile()) return { ok: false, status: 404 }; return { ok: true, abs: real, size: st.size, type }; }