Compare commits
No commits in common. "c63478a72ae2be79d984be557f6d21c025968f89" and "e7743829bea5aa3125b3872ba32abdac487cf3be" have entirely different histories.
c63478a72a
...
e7743829be
5 changed files with 31 additions and 114 deletions
|
|
@ -37,7 +37,7 @@ In dev, Vite proxies `/api` to the target set in `vite.config.ts`, so the API mu
|
|||
- Word import: Blog posts → Import from Word turns a .docx into an unpublished post (headings, paragraphs, lists, tables, links); nginx needs a block for `/api/admin/import/docx` with an 11m body limit: `docs/importing.md`.
|
||||
- Speed: nginx must gzip JS/CSS/JSON/SVG and speak HTTP/2; page HTML is `no-cache`; uploads get a `-640.webp` small copy made at upload and at boot: `docs/performance.md`.
|
||||
- Security headers: the API sends them (Hono `secureHeaders()`, plus a CSRF check on `/api` writes); nginx sends HSTS on everything and must pass `Host` and `X-Forwarded-For` to the API: `docs/security-headers.md`.
|
||||
- Request form (`/request`): files go to the "Requests" folder of a Google shared drive and new-request emails go out through Gmail, both via a service account (`server/src/googleService.js`). `driveUpload(folder, ...)` finds or makes the named folder in the shared drive, so other uses get their own folder. Off until `GOOGLE_SERVICE_ACCOUNT` plus `GOOGLE_SHARED_DRIVE` (the bare id, not a URL) / `GOOGLE_MAIL_FROM` are set. Needs its own nginx block. Setup: `docs/google-drive-and-mail.md`.
|
||||
- Request form (`/request`): files go to a Google Drive folder (a shared drive) and new-request emails go out through Gmail, both via a service account (`server/src/googleService.js`); off until `GOOGLE_SERVICE_ACCOUNT` plus `GOOGLE_DRIVE_FOLDER` / `GOOGLE_MAIL_FROM` are set. Needs its own nginx block. Setup: `docs/google-drive-and-mail.md`.
|
||||
- Google sign-in for @nextgenerationofunity.org admin accounts: on when `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` are set on the service. Only existing accounts (made by admin-cli) get in; passwords still work. Setup: `docs/google-sign-in.md`.
|
||||
|
||||
## Git workflow
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
# Google Drive and Gmail (the Request form)
|
||||
|
||||
The site keeps files in one Google **shared drive**: the Request form
|
||||
(`/request`) puts what people send in its "Requests" folder, and later
|
||||
uses (eg. files linked from blog posts) get folders of their own. New
|
||||
requests can be emailed to whoever is listed in
|
||||
The Request form (`/request`) keeps the files people send in a Google
|
||||
Drive folder, and new requests can be emailed to whoever is listed in
|
||||
Panel → Site settings → Notices. Both go through one Google **service
|
||||
account** (`server/src/googleService.js`). Nothing here is needed for
|
||||
the rest of the site; until it's set up, the form takes no files and no
|
||||
|
|
@ -12,7 +10,7 @@ mail goes out.
|
|||
| Setting on the `ngu-api` service | What it is |
|
||||
| --- | --- |
|
||||
| `GOOGLE_SERVICE_ACCOUNT` | Path to the service account's key file, eg. `/etc/ngu/google-service-account.json` |
|
||||
| `GOOGLE_SHARED_DRIVE` | The shared drive's **id only**, eg. `0ACUt32ze-CVGUk9PVA`: never the whole address (see step 2) |
|
||||
| `GOOGLE_DRIVE_FOLDER` | The Drive folder's id (the last part of its URL) |
|
||||
| `GOOGLE_MAIL_FROM` | The Workspace address notices are sent from, eg. `web@nextgenerationofunity.org` |
|
||||
|
||||
Files need the first two; mail needs the first and third.
|
||||
|
|
@ -37,43 +35,21 @@ Files need the first two; mail needs the first and third.
|
|||
Note the account's email (`ngu-site@<project>.iam.gserviceaccount.com`)
|
||||
and, from its Details page, its **Client ID** (a long number).
|
||||
|
||||
## 2. The shared drive
|
||||
## 2. The Drive folder
|
||||
|
||||
A service account has no Drive storage of its own, so everything goes in
|
||||
a **shared drive**.
|
||||
A service account has no Drive storage of its own, so the folder must
|
||||
be in a **shared drive**.
|
||||
|
||||
1. In Google Drive, make (or pick) a shared drive, eg. "NGU Website".
|
||||
1. In Google Drive, make (or pick) a shared drive, eg. "NGU Website",
|
||||
and a folder in it, eg. "Requests".
|
||||
2. Shared drive → Manage members → add the service account's email as
|
||||
**Content manager** (it makes folders and deletes files).
|
||||
3. Open the shared drive itself (not a folder in it). Its address is
|
||||
`https://drive.google.com/drive/u/2/folders/0ACUt32ze-CVGUk9PVA`;
|
||||
the id is only the part after `folders/`, here `0ACUt32ze-CVGUk9PVA`.
|
||||
A shared drive's id starts with `0A`. Leave out the address, the
|
||||
`/u/2/` part and anything after a `?`: pasting the whole URL gives
|
||||
`404 File not found` on every upload.
|
||||
**Content manager**.
|
||||
3. Open the folder; its id is the end of the address:
|
||||
`https://drive.google.com/drive/folders/<this part>`.
|
||||
|
||||
### Folders in it
|
||||
|
||||
The site finds each folder it uses by name, at the top of the shared
|
||||
drive, and makes it the first time it's needed. Nothing needs making by
|
||||
hand.
|
||||
|
||||
| Folder | Used by | Set in |
|
||||
| --- | --- | --- |
|
||||
| `Requests` | Files sent with the Request form | `DRIVE_FOLDER` in `server/src/routes/requests.js` |
|
||||
|
||||
A new use calls `driveUpload("<Folder>", name, mime, bytes)` from
|
||||
`server/src/googleService.js`; a path such as `"Blog/2026"` makes each
|
||||
level. Add it to this table.
|
||||
|
||||
Folders are matched by name, so renaming or moving one in Drive makes
|
||||
the site start a new one under the old name (files already stored keep
|
||||
working: they're found by their own id). People can add their own
|
||||
folders to the drive; the site ignores them.
|
||||
|
||||
Request files are named `Request 12 - original name.docx`, so the number
|
||||
matches the one in Forms → Content requests. Deleting a request in the
|
||||
admin deletes its files from Drive too.
|
||||
Files are named `Request 12 - original name.docx`, so the number matches
|
||||
the one in Forms → Content requests. Deleting a request in the admin
|
||||
deletes its files from Drive too.
|
||||
|
||||
## 3. Gmail (domain-wide delegation)
|
||||
|
||||
|
|
@ -101,7 +77,7 @@ sudo systemctl edit ngu-api
|
|||
```ini
|
||||
[Service]
|
||||
Environment=GOOGLE_SERVICE_ACCOUNT=/etc/ngu/google-service-account.json
|
||||
Environment=GOOGLE_SHARED_DRIVE=<shared drive id, eg. 0ACUt32ze-CVGUk9PVA>
|
||||
Environment=GOOGLE_DRIVE_FOLDER=<folder id>
|
||||
Environment=GOOGLE_MAIL_FROM=web@nextgenerationofunity.org
|
||||
```
|
||||
|
||||
|
|
@ -125,13 +101,12 @@ location = /api/requests {
|
|||
- Send a request with a small file at `/request`. The journal shows
|
||||
`feedback #N (content: new blog, 1 files)`, and with mail on,
|
||||
`feedback #N: notice sent to 1`.
|
||||
- The file is in the shared drive's Requests folder (made on the first upload), and downloads from Forms → Content
|
||||
- The file is in the Drive folder, and downloads from Forms → Content
|
||||
requests.
|
||||
- `token refused: unauthorized_client` on mail means the delegation
|
||||
(step 3) is missing or has the wrong Client ID or scope. `404 File not
|
||||
found` on upload means `GOOGLE_SHARED_DRIVE` isn't the bare id (a
|
||||
whole URL, say) or the service account isn't a member of the shared
|
||||
drive.
|
||||
found` on upload means the folder id is wrong or the service account
|
||||
isn't a member of the shared drive.
|
||||
|
||||
## What the form takes
|
||||
|
||||
|
|
|
|||
|
|
@ -6,11 +6,9 @@
|
|||
Google swaps for an access token (RS256 with node:crypto, no
|
||||
library). Two uses:
|
||||
|
||||
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.
|
||||
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.
|
||||
Gmail new-request notices, sent as a Workspace user through
|
||||
domain-wide delegation.
|
||||
|
||||
|
|
@ -18,12 +16,11 @@
|
|||
|
||||
GOOGLE_SERVICE_ACCOUNT path to the key's .json file (never in
|
||||
the repo)
|
||||
GOOGLE_SHARED_DRIVE the shared drive's id: only the last part
|
||||
of its address, not the whole URL
|
||||
GOOGLE_DRIVE_FOLDER the folder's id, from its URL
|
||||
GOOGLE_MAIL_FROM the Workspace address notices come from
|
||||
|
||||
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.
|
||||
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.
|
||||
Setup: docs/google-drive-and-mail.md.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
|
|
@ -49,7 +46,7 @@ function serviceKey() {
|
|||
return key;
|
||||
}
|
||||
|
||||
export const driveReady = () => Boolean(process.env.GOOGLE_SERVICE_ACCOUNT && process.env.GOOGLE_SHARED_DRIVE);
|
||||
export const driveReady = () => Boolean(process.env.GOOGLE_SERVICE_ACCOUNT && process.env.GOOGLE_DRIVE_FOLDER);
|
||||
export const mailReady = () => Boolean(process.env.GOOGLE_SERVICE_ACCOUNT && process.env.GOOGLE_MAIL_FROM);
|
||||
|
||||
/* ── Access tokens, kept until a minute before they run out ──── */
|
||||
|
|
@ -107,63 +104,10 @@ async function call(url, init, scope, subject) {
|
|||
|
||||
/* ── Drive ───────────────────────────────────────────────────── */
|
||||
|
||||
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) {
|
||||
/* One file into the folder. Returns Drive's id for it. */
|
||||
export async function driveUpload(name, mime, buffer) {
|
||||
const boundary = `ngu${Date.now().toString(36)}`;
|
||||
const meta = JSON.stringify({ name, parents: [await driveFolder(folder)] });
|
||||
const meta = JSON.stringify({ name, parents: [process.env.GOOGLE_DRIVE_FOLDER] });
|
||||
const body = Buffer.concat([
|
||||
Buffer.from(
|
||||
`--${boundary}\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n${meta}\r\n` +
|
||||
|
|
|
|||
|
|
@ -40,8 +40,6 @@ const CATEGORIES = {
|
|||
|
||||
const LIMITS = { name: 120, email: 254, subject: 200, message: 5000 };
|
||||
const MIN_MESSAGE = 10;
|
||||
// Where in the shared drive the files go (googleService.js).
|
||||
const DRIVE_FOLDER = "Requests";
|
||||
export const MAX_FILES = 3;
|
||||
export const MAX_FILE_BYTES = 10 * 1024 * 1024;
|
||||
|
||||
|
|
@ -134,7 +132,7 @@ requests.post(
|
|||
const stored = [];
|
||||
try {
|
||||
for (const file of uploads) {
|
||||
stored.push({ ...file, driveId: await driveUpload(DRIVE_FOLDER, `Request ${id} - ${file.name}`, file.mime, file.buffer) });
|
||||
stored.push({ ...file, driveId: await driveUpload(`Request ${id} - ${file.name}`, file.mime, file.buffer) });
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn(`feedback #${id}: Drive upload failed: ${err.message}`);
|
||||
|
|
|
|||
|
|
@ -7,4 +7,4 @@
|
|||
box or a build banner can read the same value later.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
export const SITE_VERSION = "NGU-Web.v1.9.6-content-display";
|
||||
export const SITE_VERSION = "NGU-Web.v1.9.5-content-display";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue