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
|
|
@ -159,6 +159,8 @@ export const blocksChild = (ownerKind) => ({
|
|||
url("href"),
|
||||
text("alt"),
|
||||
text("cite"),
|
||||
// Video blocks only (066): an uploaded video's WebVTT file.
|
||||
text("captions"),
|
||||
// People blocks only (054): whose team's tiles to show.
|
||||
text("team_id"),
|
||||
],
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ export const frontPage = {
|
|||
text("livestream_title"),
|
||||
url("video_url"),
|
||||
text("video_title"),
|
||||
text("video_captions"),
|
||||
text("countdown_event_id"),
|
||||
text("countdown_scope_id"),
|
||||
enumeration("countdown_event_type", ["retreat", "class", "workshop", "meeting", "other"]),
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
13
server/src/migrations/066_video_captions.sql
Normal file
13
server/src/migrations/066_video_captions.sql
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
-- ═══════════════════════════════════════════════════════════════
|
||||
-- 066 VIDEO CAPTIONS
|
||||
--
|
||||
-- A WebVTT file for an uploaded video, drawn as its captions track
|
||||
-- (WCAG 1.2.2). YouTube, Vimeo and Facebook embeds bring their own.
|
||||
-- The file is an upload in /uploads/captions/ (media.js).
|
||||
--
|
||||
-- content_blocks.captions a video block's
|
||||
-- front_page.video_captions the hero's featured video's
|
||||
-- ═══════════════════════════════════════════════════════════════
|
||||
|
||||
ALTER TABLE content_blocks ADD COLUMN captions TEXT;
|
||||
ALTER TABLE front_page ADD COLUMN video_captions TEXT;
|
||||
|
|
@ -225,7 +225,7 @@ home.get("/front-page", (c) => {
|
|||
? { url: page.livestream_url, title: page.livestream_title }
|
||||
: null,
|
||||
video: page.video_url
|
||||
? { url: page.video_url, title: page.video_title }
|
||||
? { url: page.video_url, title: page.video_title, captions: page.video_captions }
|
||||
: null,
|
||||
event: shapeHeroEvent(db, heroEvent),
|
||||
},
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@
|
|||
|
||||
GET /media the library, newest first, ?q=name,
|
||||
?type=image|video|audio,
|
||||
?folder=logos|images|thumbnails.
|
||||
?folder=logos|images|thumbnails|captions.
|
||||
Picks up images copied into the
|
||||
uploads folder by hand first
|
||||
(registerLooseFiles, at most every
|
||||
SCAN_EVERY_MS)
|
||||
POST /media upload one image (multipart: file,
|
||||
POST /media upload one image or .vtt (multipart: file,
|
||||
keep_original, folder: FOLDERS in
|
||||
media.js, default images)
|
||||
GET /media/:id/uses which records name this file
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ export function loadBlocks(db, ownerKind, ids, slot = "body") {
|
|||
|
||||
const blockRows = db
|
||||
.prepare(
|
||||
`SELECT id, owner_id, type, text, media, href, alt, cite, team_id
|
||||
`SELECT id, owner_id, type, text, media, href, alt, cite, captions, team_id
|
||||
FROM content_blocks
|
||||
WHERE owner_kind = ? AND slot = ? AND owner_id IN (${placeholders(ids.length)})
|
||||
ORDER BY owner_id, sort_order`,
|
||||
|
|
@ -85,6 +85,7 @@ export function loadBlocks(db, ownerKind, ids, slot = "body") {
|
|||
href: row.href,
|
||||
alt: row.alt,
|
||||
cite: row.cite,
|
||||
captions: row.captions,
|
||||
team_id: row.team_id,
|
||||
items: [],
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue