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/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 (
- {IMAGE_FOLDERS.map(([folder, label]) => ( + {[...IMAGE_FOLDERS, ...extra].map(([folder, label]) => ( + + ))} + + + ); +} + /* ── Picker dialog ───────────────────────────────────────────── */ export function MediaPicker({ @@ -322,7 +405,8 @@ export function MediaPicker({ const { user } = useAuth(); const [query, setQuery] = useState(""); const [tab, setTab] = useState(folder); - const { items, error, setItems } = useMediaLibrary(query, "image", tab); + const fromRequests = tab === REQUESTS_TAB; + const { items, error, setItems } = useMediaLibrary(query, "image", fromRequests ? undefined : tab); const dialogRef = useRef(null); // showModal gives focus trapping, Escape and the backdrop for free. @@ -361,7 +445,7 @@ export function MediaPicker({
- +
{roleCanWrite(user) && ( @@ -380,7 +464,16 @@ export function MediaPicker({ )}
- {error ? ( + {fromRequests ? ( + { + setItems((current) => [item, ...(current ?? []).filter((m) => m.id !== item.id)]); + onPick(item); + dialogRef.current?.close(); + }} + /> + ) : error ? (

{error}

) : items === null ? (

Loading…