Add a Request form, with files in Google Drive and email notices
One form at /request for blog posts, events, and new or updated chapters, regions (updates only), partners, teams, awards and people. ?type= opens it on one; buttons on the Community sections, blog posts, Retreats and the front page calendar do. Files (.docx, PDF, JPEG, PNG) go to a Google Drive folder through a service account. Requests are feedback rows of type 'content' (migration 046), in a third Forms queue: files download from Drive, a .docx can become a draft blog post, and deleting a request deletes its files. New content and data requests email the addresses in Site settings through Gmail. Setup: docs/google-drive-and-mail.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0e582c236b
commit
dc53af238f
31 changed files with 1318 additions and 113 deletions
|
|
@ -46,6 +46,52 @@ function freeId(db, base) {
|
|||
return id;
|
||||
}
|
||||
|
||||
/* A Word document as a new unpublished blog post, through the CRUD
|
||||
engine. Returns { id, summary }; an unreadable document is a 400
|
||||
HttpError. Also used by Forms → Content requests, on a file someone
|
||||
sent with the Request form. */
|
||||
export function importDocx(db, buffer, fileName, byEmail) {
|
||||
let doc;
|
||||
try {
|
||||
doc = readDocx(buffer);
|
||||
} catch (err) {
|
||||
throw new HttpError(400, err instanceof DocxError ? err.message : "That document couldn't be read. Save it again as .docx and retry.");
|
||||
}
|
||||
if (doc.blocks.length === 0) throw new HttpError(400, "That document has no text to import.");
|
||||
|
||||
const title = doc.title || String(fileName ?? "").replace(/\.docx$/i, "") || "Imported post";
|
||||
const headings = doc.blocks.filter((b) => b.type === "heading" || b.type === "subheading").length;
|
||||
|
||||
const row = createRow(db, ENTITIES.blog_posts, {
|
||||
id: freeId(db, slug(title)),
|
||||
title: title.slice(0, 200),
|
||||
published_on: doc.date,
|
||||
is_published: 0,
|
||||
is_listed: 1,
|
||||
// A long document with sections reads better with its contents.
|
||||
show_contents: headings >= 3 ? 1 : 0,
|
||||
content_blocks: doc.blocks.map((block) => ({
|
||||
slot: "body",
|
||||
type: block.type,
|
||||
text: block.text ?? null,
|
||||
items: block.items ?? [],
|
||||
})),
|
||||
});
|
||||
const count = (type) => doc.blocks.filter((b) => b.type === type).length;
|
||||
console.log(`blog_posts ${row.id} imported from Word by ${byEmail}`);
|
||||
return {
|
||||
id: row.id,
|
||||
summary: {
|
||||
blocks: doc.blocks.length,
|
||||
headings,
|
||||
paragraphs: count("paragraph"),
|
||||
lists: count("list"),
|
||||
tables: count("table"),
|
||||
skipped: doc.skipped,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
imports.post(
|
||||
"/import/docx",
|
||||
requireRole("editor"),
|
||||
|
|
@ -64,53 +110,9 @@ imports.post(
|
|||
const file = body.file;
|
||||
if (!(file instanceof File)) return c.json({ error: "No file was sent." }, 400);
|
||||
|
||||
let doc;
|
||||
try {
|
||||
doc = readDocx(Buffer.from(await file.arrayBuffer()));
|
||||
} catch (err) {
|
||||
if (err instanceof DocxError) return c.json({ error: err.message }, 400);
|
||||
return c.json({ error: "That document couldn't be read. Save it again as .docx and retry." }, 400);
|
||||
}
|
||||
if (doc.blocks.length === 0) return c.json({ error: "That document has no text to import." }, 400);
|
||||
|
||||
const title = doc.title || String(file.name ?? "").replace(/\.docx$/i, "") || "Imported post";
|
||||
const headings = doc.blocks.filter((b) => b.type === "heading" || b.type === "subheading").length;
|
||||
|
||||
const payload = {
|
||||
id: freeId(db, slug(title)),
|
||||
title: title.slice(0, 200),
|
||||
published_on: doc.date,
|
||||
is_published: 0,
|
||||
is_listed: 1,
|
||||
// A long document with sections reads better with its contents.
|
||||
show_contents: headings >= 3 ? 1 : 0,
|
||||
content_blocks: doc.blocks.map((block) => ({
|
||||
slot: "body",
|
||||
type: block.type,
|
||||
text: block.text ?? null,
|
||||
items: block.items ?? [],
|
||||
})),
|
||||
};
|
||||
|
||||
try {
|
||||
const row = createRow(db, ENTITIES.blog_posts, payload);
|
||||
const count = (type) => doc.blocks.filter((b) => b.type === type).length;
|
||||
console.log(`blog_posts ${row.id} imported from Word by ${c.get("user").email}`);
|
||||
return c.json(
|
||||
{
|
||||
id: row.id,
|
||||
summary: {
|
||||
blocks: doc.blocks.length,
|
||||
headings,
|
||||
paragraphs: count("paragraph"),
|
||||
lists: count("list"),
|
||||
tables: count("table"),
|
||||
skipped: doc.skipped,
|
||||
},
|
||||
},
|
||||
201,
|
||||
NO_STORE,
|
||||
);
|
||||
const result = importDocx(db, Buffer.from(await file.arrayBuffer()), file.name, c.get("user").email);
|
||||
return c.json(result, 201, NO_STORE);
|
||||
} catch (err) {
|
||||
if (err instanceof HttpError) return c.json({ error: err.message, fields: err.fields }, err.status);
|
||||
throw err;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue