Keep Drive files in named folders of one shared drive

GOOGLE_SHARED_DRIVE (the shared drive's bare id) replaces
GOOGLE_DRIVE_FOLDER. driveUpload takes a folder path, found by name in
the shared drive or made the first time, so each use gets its own
folder: "Requests" for the Request form now, others later. The docs
say plainly that the setting is the id only, not the address.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-29 16:53:40 -05:00
parent e7743829be
commit 0f6a796021
4 changed files with 113 additions and 30 deletions

View file

@ -6,9 +6,11 @@
Google swaps for an access token (RS256 with node:crypto, no
library). Two uses:
Drive files sent with the Request form are kept in one folder,
in a shared drive (a service account has no storage of
its own), and read back for the admin.
Drive one shared drive (a service account has no storage of
its own), with a folder per use: "Requests" for files sent
with the Request form, and so on. Each folder is found by
name, or made, the first time it's needed; a path such as
"Blog/2026" makes each level.
Gmail new-request notices, sent as a Workspace user through
domain-wide delegation.
@ -16,11 +18,12 @@
GOOGLE_SERVICE_ACCOUNT path to the key's .json file (never in
the repo)
GOOGLE_DRIVE_FOLDER the folder's id, from its URL
GOOGLE_SHARED_DRIVE the shared drive's id: only the last part
of its address, not the whole URL
GOOGLE_MAIL_FROM the Workspace address notices come from
Each part is off until its settings are there: without a folder the
form offers no uploads, and without a sender no mail goes out.
Each part is off until its settings are there: without a shared drive
the form offers no uploads, and without a sender no mail goes out.
Setup: docs/google-drive-and-mail.md.
═══════════════════════════════════════════════════════════════ */
@ -46,7 +49,7 @@ function serviceKey() {
return key;
}
export const driveReady = () => Boolean(process.env.GOOGLE_SERVICE_ACCOUNT && process.env.GOOGLE_DRIVE_FOLDER);
export const driveReady = () => Boolean(process.env.GOOGLE_SERVICE_ACCOUNT && process.env.GOOGLE_SHARED_DRIVE);
export const mailReady = () => Boolean(process.env.GOOGLE_SERVICE_ACCOUNT && process.env.GOOGLE_MAIL_FROM);
/* ── Access tokens, kept until a minute before they run out ──── */
@ -104,10 +107,63 @@ async function call(url, init, scope, subject) {
/* ── Drive ───────────────────────────────────────────────────── */
/* One file into the folder. Returns Drive's id for it. */
export async function driveUpload(name, mime, buffer) {
const FOLDER = "application/vnd.google-apps.folder";
// Folder path → a promise of its id. A promise, so two uploads at
// once don't both make the folder. A failed lookup is forgotten.
const folders = new Map();
/* The id of a folder in the shared drive, eg. "Requests" or
"Blog/2026", made if it isn't there. Found by name, so a folder
renamed in Drive is made again under the old name. */
export function driveFolder(path) {
let pending = folders.get(path);
if (!pending) {
pending = findOrMakeFolder(path);
folders.set(path, pending);
pending.catch(() => folders.delete(path));
}
return pending;
}
async function findOrMakeFolder(path) {
const drive = process.env.GOOGLE_SHARED_DRIVE;
let parent = drive;
for (const name of path.split("/").filter(Boolean)) {
const quoted = name.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
const query = new URLSearchParams({
q: `name = '${quoted}' and mimeType = '${FOLDER}' and '${parent}' in parents and trashed = false`,
corpora: "drive",
driveId: drive,
includeItemsFromAllDrives: "true",
supportsAllDrives: "true",
fields: "files(id)",
});
const found = (await (await call(`${DRIVE}?${query}`, {}, DRIVE_SCOPE)).json()).files?.[0];
parent = found
? found.id
: (
await (
await call(
`${DRIVE}?supportsAllDrives=true&fields=id`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, mimeType: FOLDER, parents: [parent] }),
},
DRIVE_SCOPE,
)
).json()
).id;
}
return parent;
}
/* One file into a folder of the shared drive (driveFolder). Returns
Drive's id for it. */
export async function driveUpload(folder, name, mime, buffer) {
const boundary = `ngu${Date.now().toString(36)}`;
const meta = JSON.stringify({ name, parents: [process.env.GOOGLE_DRIVE_FOLDER] });
const meta = JSON.stringify({ name, parents: [await driveFolder(folder)] });
const body = Buffer.concat([
Buffer.from(
`--${boundary}\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n${meta}\r\n` +