diff --git a/docs/video-clips.md b/docs/video-clips.md index 90a558a..3faf4d2 100644 --- a/docs/video-clips.md +++ b/docs/video-clips.md @@ -9,6 +9,11 @@ Facebook links; the server downloads it, then you cut clips from it: the crop; the white box over the video shows what's kept. - **Format**: video (MP4, H.264 and AAC) or audio only (MP3). +A downloaded video can be kept whole too, before or without cutting +it: **Save to library** and **Save to Drive** on the download itself. +It stays on the server to cut clips from either way (until the day's +sweep). + Clips queue and run one at a time. A finished clip is deleted from the server once it's downloaded, unless **Save to library** is pressed first, which puts it in the media library as an upload (usable as a featured diff --git a/server/src/clips.js b/server/src/clips.js index 28f05e3..acc5b2d 100644 --- a/server/src/clips.js +++ b/server/src/clips.js @@ -490,9 +490,10 @@ export function kick(db = dbRef) { export const fileOf = (job) => (job?.file ? filePath(job.file) : null); -/* A finished clip into the media library: copied into the uploads - directory under a random name, given a media row, and its working - file deleted. Returns the media row's id. */ +/* A finished clip, or a whole downloaded video, into the media + library: copied into the uploads directory under a random name and + given a media row. A clip's working file is deleted; a video's is + kept for cutting. Returns the media row's id. */ export async function saveToLibrary(db, job, userId) { const from = fileOf(job); if (!from) throw new ClipError(410, "That clip's file is gone. Make it again."); @@ -528,7 +529,11 @@ export async function saveToLibrary(db, job, userId) { const { size } = await stat(from); const params = JSON.parse(job.params ?? "{}"); const stamp = (s) => `${Math.floor(s / 60)}m${String(Math.floor(s % 60)).padStart(2, "0")}s`; - const label = `${job.title ?? "Clip"} (${stamp(params.start ?? 0)} to ${stamp(params.end ?? 0)})${ext}`; + // A whole download is named for itself; a clip for its span. + const label = + job.kind === "source" + ? `${job.title ?? "Video"}${ext}` + : `${job.title ?? "Clip"} (${stamp(params.start ?? 0)} to ${stamp(params.end ?? 0)})${ext}`; id = await step("adding it to the media table", () => Number( @@ -546,8 +551,14 @@ export async function saveToLibrary(db, job, userId) { throw err; } - await rm(from, { force: true }); - update(db, job.id, { saved_media_id: id, file: null }); + // A clip's working file goes; a downloaded video's stays, to cut + // clips from (the day's sweep clears it). + if (job.kind === "source") { + update(db, job.id, { saved_media_id: id }); + } else { + await rm(from, { force: true }); + update(db, job.id, { saved_media_id: id, file: null }); + } return { id, url: `${PUBLIC_PREFIX}${filename}` }; } diff --git a/server/src/routes/admin.js b/server/src/routes/admin.js index 47cc1a1..8962f5f 100644 --- a/server/src/routes/admin.js +++ b/server/src/routes/admin.js @@ -13,6 +13,7 @@ import { requireAuth, requireRole } from "../auth.js"; import { NO_STORE } from "../shape.js"; import { driveDelete, driveDownload, driveReady, driveTree } from "../googleService.js"; import { DRIVE_FOLDER, TYPE_FOLDERS } from "./requests.js"; +import { addToLibrary, shape as mediaShape } from "./media.js"; import { HttpError } from "../admin-crud.js"; import { importDocx } from "./import.js"; @@ -246,6 +247,9 @@ admin.delete("/feedback/:id", requireRole("admin"), async (c) => { GET /feedback/:id/files/:file download, from Drive POST /feedback/:id/files/:file/blog-post a .docx as a new unpublished blog post (import.js), editors and up + POST /feedback/:id/files/:file/to-library { folder }: a photo + into the media library, for an image field (the picker's + From requests tab), editors and up DELETE /feedback/:id/files/:file into Drive's trash and off the request, admins and up GET /request-files every request's files, @@ -284,6 +288,29 @@ admin.get("/feedback/:id/files/:file", async (c) => { } }); +/* A request's photo as an upload: records' images live in the media + library, not Drive. Once per file: the copy is found again by name. */ +admin.post("/feedback/:id/files/:file/to-library", requireRole("editor"), async (c) => { + const file = fileOf(c); + if (!file) return c.json({ error: "No such file." }, 404); + if (!file.mime.startsWith("image/")) return c.json({ error: "Only a photo (JPEG or PNG) can go in the library." }, 400); + const db = c.get("db"); + const name = `${file.name} (request ${c.req.param("id")})`.slice(0, 200); + const known = db.prepare(`SELECT m.* FROM media m WHERE m.original_name = ?`).get(name); + if (known) return c.json({ media: mediaShape(known) }, 200, NO_STORE); + try { + const bytes = await fromDrive(file); + const body = (await c.req.json().catch(() => null)) ?? {}; + const row = await addToLibrary(db, bytes, { originalName: name, folder: body.folder, userId: c.get("user").id }); + console.log(`feedback #${c.req.param("id")}: ${file.name} into the media library by ${c.get("user").email}`); + return c.json({ media: mediaShape(row) }, 201, NO_STORE); + } catch (err) { + if (err instanceof HttpError) return c.json({ error: err.message }, err.status); + if (err?.status && err?.message) return c.json({ error: err.message }, err.status); // MediaError + throw err; + } +}); + admin.post("/feedback/:id/files/:file/blog-post", requireRole("editor"), async (c) => { const file = fileOf(c); if (!file) return c.json({ error: "No such file." }, 404); diff --git a/server/src/routes/clips.js b/server/src/routes/clips.js index c899217..6f0663a 100644 --- a/server/src/routes/clips.js +++ b/server/src/routes/clips.js @@ -11,9 +11,11 @@ GET /clips/:id/download a finished clip, as an attachment; its file is deleted once it has gone out in full, unless saved - POST /clips/:id/save a finished clip into the media + POST /clips/:id/save a finished clip, or a whole + downloaded video, into the media library - POST /clips/:id/drive { folder }: a finished clip into a + POST /clips/:id/drive { folder }: a finished clip (or a + whole downloaded video) into a folder under Media in Drive, as a Files entry (routes/files.js) DELETE /clips/:id a job and its files (a source @@ -214,8 +216,9 @@ clips.get("/clips/:id/download", async (c) => { clips.post("/clips/:id/save", async (c) => { const db = c.get("db"); const job = findOr404(c); - if (job.kind !== "clip" || job.status !== "done") throw new ClipError(400, "Only a finished clip can be saved."); + if (job.status !== "done") throw new ClipError(400, "Only a finished download or clip can be saved."); if (job.saved_media_id) throw new ClipError(409, "It's already in the media library."); + if (!job.file) throw new ClipError(410, "Its file is gone from the server."); const media = await saveToLibrary(db, job, c.get("user").id); console.log(`clips: clip ${job.id} saved to the library by ${c.get("user").email}`); return c.json({ media, job: shape(db.prepare(`${SELECT} WHERE j.id = ?`).get(job.id)) }, 200, NO_STORE); @@ -227,7 +230,7 @@ clips.post("/clips/:id/save", async (c) => { clips.post("/clips/:id/drive", async (c) => { const db = c.get("db"); const job = findOr404(c); - if (job.kind !== "clip" || job.status !== "done") throw new ClipError(400, "Only a finished clip can be saved."); + if (job.status !== "done") throw new ClipError(400, "Only a finished download or clip can be saved."); if (job.saved_drive_file_id) throw new ClipError(409, "It's already in Drive."); if (!driveReady()) throw new ClipError(400, "Google Drive isn't set up on the server (docs/google-drive-and-mail.md)."); const folder = cleanFolder((await body(c)).folder); @@ -239,7 +242,11 @@ clips.post("/clips/:id/drive", async (c) => { const mime = audio ? "audio/mpeg" : "video/mp4"; const params = job.params ? JSON.parse(job.params) : {}; const stamp = (s) => `${Math.floor(s / 60)}m${String(Math.floor(s % 60)).padStart(2, "0")}s`; - const name = `${(job.title ?? "Clip").replace(/[\u0000-\u001f"\\/]+/g, " ").trim().slice(0, 120) || "Clip"} (${stamp(params.start ?? 0)} to ${stamp(params.end ?? 0)}).${audio ? "mp3" : "mp4"}`; + const title = (job.title ?? "").replace(/[\u0000-\u001f"\\/]+/g, " ").trim().slice(0, 120); + const name = + job.kind === "source" + ? `${title || "Video"}.mp4` + : `${title || "Clip"} (${stamp(params.start ?? 0)} to ${stamp(params.end ?? 0)}).${audio ? "mp3" : "mp4"}`; let uploaded; try { diff --git a/server/src/routes/media.js b/server/src/routes/media.js index c845079..907c924 100644 --- a/server/src/routes/media.js +++ b/server/src/routes/media.js @@ -58,7 +58,7 @@ let lastScan = 0; /* Tables that can't hold an image path, or mustn't be searched. */ const NOT_CONTENT = new Set(["media", "drive_files", "admin_users", "sessions", "feedback", "feedback_files", "sqlite_sequence"]); -function shape(row) { +export function shape(row) { return { id: row.id, // A hand-copied file keeps its own name, which may need escaping diff --git a/src/components/admin/MediaLibrary.tsx b/src/components/admin/MediaLibrary.tsx index 86c12b2..311348b 100644 --- a/src/components/admin/MediaLibrary.tsx +++ b/src/components/admin/MediaLibrary.tsx @@ -6,7 +6,10 @@ ImageInput the `widget: "image"` form field. A preview, the stored value as editable text, and buttons to upload a new file or pick an earlier one. - MediaPicker the dialog behind "Choose from library". + MediaPicker the dialog behind "Choose from library". Its From + requests tab lists photos sent with the Request + form (in Drive); picking one copies it into the + library first (RequestImages). MediaGrid the thumbnails both the picker and the Media page (pages/admin/AdminMedia.tsx) draw. UploadButton a file input dressed as a button, with the @@ -37,6 +40,8 @@ import { FileAudio, X } from "@phosphor-icons/react"; import { useCallback, useEffect, useId, useRef, useState, type ReactNode } from "react"; +import { useLocation } from "react-router-dom"; + import { ApiError, get, post, upload } from "../../lib/api.ts"; import { useAuth } from "../../lib/auth.tsx"; import { canWrite as roleCanWrite } from "../../lib/roles.ts"; @@ -65,11 +70,20 @@ export const IMAGE_FOLDERS = [ ] as const; export type ImageFolder = (typeof IMAGE_FOLDERS)[number][0]; -/* A tab strip over the folders; `value` is the one showing. */ -export function FolderTabs({ value, onChange }: { value: string; onChange: (folder: ImageFolder) => void }) { +/* A tab strip over the folders; `value` is the one showing. `extra` + adds tabs after them (the picker's From requests). */ +export function FolderTabs({ + value, + onChange, + extra = [], +}: { + value: string; + onChange: (folder: string) => void; + extra?: [string, string][]; +}) { return (
{error}
) : items === null ? (Loading…
diff --git a/src/lib/version.ts b/src/lib/version.ts index 334a4b6..00e54dc 100644 --- a/src/lib/version.ts +++ b/src/lib/version.ts @@ -7,4 +7,4 @@ box or a build banner can read the same value later. ═══════════════════════════════════════════════════════════════ */ -export const SITE_VERSION = "NGU-Web.v1.9.13-content-display"; +export const SITE_VERSION = "NGU-Web.v1.9.14-content-display"; diff --git a/src/pages/admin/AdminClips.tsx b/src/pages/admin/AdminClips.tsx index 8a37d43..47ed2cb 100644 --- a/src/pages/admin/AdminClips.tsx +++ b/src/pages/admin/AdminClips.tsx @@ -491,6 +491,26 @@ export default function AdminClips() { {openId === s.id ? "Close" : "Make clips"} )} + {/* The whole video, before (or without) cutting: it + stays here to cut from either way. */} + {s.status === "done" && s.has_file && !s.saved_url && ( +{request @@ -381,6 +402,7 @@ export default function FeedbackForm({ fixedType }: { fixedType?: string } = {}) />
- Choose what it's about, give it a name, write at least a sentence and give an email address to send. + Choose what it's about, give it a name, write at least {MIN_MESSAGE} characters and give an email address to send.
)}