Let the image picker take photos sent with requests
A From requests tab in every image picker lists photos from the Request form (they live in Drive), the request being edited from first. Picking one copies it into the media library, in the field's folder, once per file, and the field gets that copy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
110b655d5e
commit
6ccbc03b52
3 changed files with 128 additions and 8 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue