Captions for uploaded videos (migration 066)
Video blocks get a Captions field and the hero a Video captions field, each a WebVTT upload drawn as <track kind="captions" default> on a video file. The media library takes .vtt (sniffed by its WEBVTT header, kept as is, always in /uploads/captions/) with its own tab. Newsletter parts leave the field out. nginx needs text/vtt in mime.types: docs/media-uploads.md, step 5. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
aaab1a945c
commit
33be618331
18 changed files with 123 additions and 43 deletions
|
|
@ -15,8 +15,9 @@
|
|||
── What ──
|
||||
The type is read from the file's first bytes, never from its
|
||||
name or the browser's Content-Type, both of which the uploader
|
||||
controls. JPEG, PNG, GIF, WebP and SVG are accepted; anything
|
||||
else is refused.
|
||||
controls. JPEG, PNG, GIF, WebP and SVG are accepted, and WebVTT
|
||||
captions (a text file starting "WEBVTT"); anything else is
|
||||
refused.
|
||||
|
||||
── Processing ──
|
||||
By default a raster image is turned upright (EXIF orientation),
|
||||
|
|
@ -25,7 +26,7 @@
|
|||
`keepOriginal` stores the bytes exactly as uploaded instead,
|
||||
under the tighter KEEP_MAX_BYTES. An SVG is always kept as it
|
||||
is: it's already small, and rasterizing it would lose the point
|
||||
of it.
|
||||
of it. So is a WebVTT file, always into captions/.
|
||||
|
||||
Names are random, so two uploads of "logo.png" can't collide
|
||||
and a filename says nothing about what's in it.
|
||||
|
|
@ -33,8 +34,8 @@
|
|||
── Folders ──
|
||||
Each upload goes in a folder by what it's for (FOLDERS): logos/
|
||||
for logo and icon fields, thumbnails/ for pictures the site saves
|
||||
by itself (link previews, Drive files' first pages), images/ for
|
||||
the rest. A stored name includes it: 'logos/3f9c….webp', served at
|
||||
by itself (link previews, Drive files' first pages), captions/
|
||||
for videos' WebVTT files, images/ for the rest. A stored name includes it: 'logos/3f9c….webp', served at
|
||||
/uploads/logos/3f9c….webp. The small copy sits beside it. Clips
|
||||
keep a bare name in the root.
|
||||
|
||||
|
|
@ -66,7 +67,7 @@ const MB = 1024 * 1024;
|
|||
/* The largest file accepted at all, before processing: room for a
|
||||
full-size phone photo. */
|
||||
/* Where an upload goes, by what it's for (see Folders above). */
|
||||
export const FOLDERS = ["logos", "images", "thumbnails"];
|
||||
export const FOLDERS = ["logos", "images", "thumbnails", "captions"];
|
||||
export const folderOf = (value) => (FOLDERS.includes(value) ? value : "images");
|
||||
|
||||
/* A name this library stores: a bare name in the root, or one inside
|
||||
|
|
@ -131,8 +132,12 @@ const TYPES = {
|
|||
gif: { mime: "image/gif", ext: ".gif" },
|
||||
webp: { mime: "image/webp", ext: ".webp" },
|
||||
svg: { mime: "image/svg+xml", ext: ".svg" },
|
||||
vtt: { mime: "text/vtt", ext: ".vtt" },
|
||||
};
|
||||
|
||||
/* Kinds stored exactly as uploaded, with no small copy: text, not pixels. */
|
||||
const AS_IS = new Set(["svg", "vtt"]);
|
||||
|
||||
function startsWith(buf, bytes, offset = 0) {
|
||||
return bytes.every((byte, i) => buf[offset + i] === byte);
|
||||
}
|
||||
|
|
@ -151,6 +156,7 @@ function sniff(buf) {
|
|||
if (/^(<\?xml[\s\S]*?\?>\s*)?(<!--[\s\S]*?-->\s*|<!DOCTYPE[^>]*>\s*)*<svg[\s>]/i.test(head)) {
|
||||
return "svg";
|
||||
}
|
||||
if (/^WEBVTT(\s|$)/.test(head)) return "vtt";
|
||||
|
||||
return null;
|
||||
}
|
||||
|
|
@ -169,10 +175,10 @@ export async function store(buf, { keepOriginal = false, folder } = {}) {
|
|||
|
||||
const kind = sniff(buf);
|
||||
if (!kind) {
|
||||
throw new MediaError(415, "Only JPEG, PNG, GIF, WebP and SVG images can be uploaded.");
|
||||
throw new MediaError(415, "Only JPEG, PNG, GIF, WebP and SVG images, and WebVTT (.vtt) captions, can be uploaded.");
|
||||
}
|
||||
|
||||
const convert = kind !== "svg" && !keepOriginal;
|
||||
const convert = !AS_IS.has(kind) && !keepOriginal;
|
||||
|
||||
if (!convert && buf.length > KEEP_MAX_BYTES) {
|
||||
throw new MediaError(
|
||||
|
|
@ -200,7 +206,7 @@ export async function store(buf, { keepOriginal = false, folder } = {}) {
|
|||
width = info.width;
|
||||
// An animated image reports the whole strip's height.
|
||||
height = info.pageHeight ?? info.height;
|
||||
} else if (kind !== "svg") {
|
||||
} else if (!AS_IS.has(kind)) {
|
||||
const meta = await sharp(buf, { limitInputPixels: MAX_INPUT_PIXELS }).metadata();
|
||||
width = meta.width ?? null;
|
||||
height = meta.pageHeight ?? meta.height ?? null;
|
||||
|
|
@ -209,7 +215,7 @@ export async function store(buf, { keepOriginal = false, folder } = {}) {
|
|||
throw new MediaError(415, "That image couldn't be read. It may be damaged.");
|
||||
}
|
||||
|
||||
const dir = folderOf(folder);
|
||||
const dir = kind === "vtt" ? "captions" : folderOf(folder);
|
||||
const base = `${randomBytes(12).toString("hex")}${type.ext}`;
|
||||
const filename = `${dir}/${base}`;
|
||||
|
||||
|
|
@ -219,7 +225,7 @@ export async function store(buf, { keepOriginal = false, folder } = {}) {
|
|||
const tmp = path.join(UPLOADS_DIR, dir, `.${base}.tmp`);
|
||||
await writeFile(tmp, out);
|
||||
await rename(tmp, path.join(UPLOADS_DIR, filename));
|
||||
if (kind !== "svg") await writeThumb(filename, out);
|
||||
if (!AS_IS.has(kind)) await writeThumb(filename, out);
|
||||
|
||||
return {
|
||||
filename,
|
||||
|
|
@ -269,10 +275,10 @@ export async function writeMissingThumbs() {
|
|||
const present = new Set(names);
|
||||
let made = 0;
|
||||
for (const name of names) {
|
||||
if (THUMB.test(name) || /\.svg$/i.test(name)) continue;
|
||||
if (THUMB.test(name) || /\.(svg|vtt)$/i.test(name)) continue;
|
||||
if (present.has(thumbName(name))) continue;
|
||||
const buf = await readFile(path.join(UPLOADS_DIR, name)).catch(() => null);
|
||||
if (!buf || !sniff(buf) || sniff(buf) === "svg") continue;
|
||||
if (!buf || !sniff(buf) || AS_IS.has(sniff(buf))) continue;
|
||||
await writeThumb(name, buf);
|
||||
made += 1;
|
||||
}
|
||||
|
|
@ -309,7 +315,7 @@ async function describe(filename) {
|
|||
|
||||
let width = null;
|
||||
let height = null;
|
||||
if (kind !== "svg") {
|
||||
if (!AS_IS.has(kind)) {
|
||||
try {
|
||||
const meta = await sharp(buf, { limitInputPixels: MAX_INPUT_PIXELS }).metadata();
|
||||
width = meta.width ?? null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue