Add a file library in Google Drive for pages to link to

Tools → Media → Files uploads PDF, Office (.docx, .xlsx, .pptx), MP3,
M4A and zip files, up to 50 MB, to the shared drive's Blog folder and
shares each as anyone with the link can view (migration 048). A file's
name and contents must agree.

Content block links get "Choose a file" (paragraph, button and image
links, list and link items, link cards). A link card also takes the
file's name and Drive's picture of its first page, fetched after
upload and saved into the image library; cards to Drive say "Google
Drive". Deleting a file says first what still links to it.

Setup and the nginx block: docs/google-drive-and-mail.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-29 18:37:15 -05:00
parent 36c9226bd9
commit 934598f954
13 changed files with 772 additions and 33 deletions

View file

@ -167,7 +167,7 @@ async function findOrMakeFolder(path) {
}
/* One file into a folder of the shared drive (driveFolder). Returns
Drive's id for it. */
{ id, webViewLink }: Drive's id, and the page that shows it. */
export async function driveUpload(folder, name, mime, buffer) {
const boundary = `ngu${Date.now().toString(36)}`;
const meta = JSON.stringify({ name, parents: [await driveFolder(folder)] });
@ -180,11 +180,38 @@ export async function driveUpload(folder, name, mime, buffer) {
Buffer.from(`\r\n--${boundary}--`),
]);
const res = await call(
`${DRIVE_UPLOAD}?uploadType=multipart&supportsAllDrives=true&fields=id`,
`${DRIVE_UPLOAD}?uploadType=multipart&supportsAllDrives=true&fields=id,webViewLink`,
{ method: "POST", headers: { "Content-Type": `multipart/related; boundary=${boundary}` }, body },
DRIVE_SCOPE,
);
return (await res.json()).id;
return await res.json();
}
/* Anyone with the link can view: for files the public site links to.
Refused when the shared drive (or the Workspace) doesn't allow
sharing outside the organization. */
export async function driveShareWithAnyone(id) {
await call(
`${DRIVE}/${encodeURIComponent(id)}/permissions?supportsAllDrives=true`,
{ method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ type: "anyone", role: "reader" }) },
DRIVE_SCOPE,
);
}
/* Drive's picture of a file's first page (PDFs, Office files), as
image bytes about `edge` pixels on the long side, or null while
Drive hasn't drawn one (it does so a little after upload) or can't. */
export async function driveThumbnail(id, edge = 1280) {
const res = await call(
`${DRIVE}/${encodeURIComponent(id)}?supportsAllDrives=true&fields=hasThumbnail,thumbnailLink`,
{},
DRIVE_SCOPE,
);
const { hasThumbnail, thumbnailLink } = await res.json();
if (!hasThumbnail || !thumbnailLink) return null;
// The link ends in its size, eg. =s220; ask for a bigger one.
const image = await call(thumbnailLink.replace(/=s\d+$/, `=s${edge}`), {}, DRIVE_SCOPE);
return Buffer.from(await image.arrayBuffer());
}
export async function driveDownload(id) {