Add Drive folders: per request type, and managed from the Media page
Request files go in Requests/<type> (Chapters, Events...). Media → Files shows a tab per folder under Media, up to three deep, and can make, rename (files follow) and delete empty folders, move a file with its Folder dropdown, and upload into the folder showing; Choose a file has the same tabs. A folder made, or a file moved, in Drive by hand is followed on the next look (migration 050 records each file's folder). Deletes now move to Drive's trash: a shared drive's Content manager, the service account's role, can't delete outright, so request and file deletes would have failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
9854da965e
commit
7f05076a86
7 changed files with 479 additions and 38 deletions
|
|
@ -124,6 +124,7 @@ const folders = new Map();
|
|||
"Media/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) {
|
||||
path = path.split("/").filter(Boolean).join("/");
|
||||
let pending = folders.get(path);
|
||||
if (!pending) {
|
||||
pending = findOrMakeFolder(path);
|
||||
|
|
@ -133,6 +134,12 @@ export function driveFolder(path) {
|
|||
return pending;
|
||||
}
|
||||
|
||||
/* After a folder is renamed or deleted: its path, and the paths
|
||||
under it, are looked up afresh next time. */
|
||||
export function forgetFolders(path) {
|
||||
for (const key of folders.keys()) if (key === path || key.startsWith(`${path}/`)) folders.delete(key);
|
||||
}
|
||||
|
||||
async function findOrMakeFolder(path) {
|
||||
const drive = process.env.GOOGLE_SHARED_DRIVE;
|
||||
let parent = drive;
|
||||
|
|
@ -187,16 +194,15 @@ export async function driveUpload(folder, name, mime, buffer) {
|
|||
return await res.json();
|
||||
}
|
||||
|
||||
/* The files (not folders) directly in a folder of the shared drive:
|
||||
[{ id, name, mimeType, size, webViewLink }]. size is missing for
|
||||
Google's own Docs, Sheets and Slides. */
|
||||
export async function driveList(folder) {
|
||||
const parent = await driveFolder(folder);
|
||||
/* Everything directly in a folder: [{ id, name, mimeType, size,
|
||||
webViewLink }]. size is missing for Google's own Docs, Sheets and
|
||||
Slides. */
|
||||
async function driveChildren(parent) {
|
||||
const found = [];
|
||||
let pageToken = "";
|
||||
do {
|
||||
const query = new URLSearchParams({
|
||||
q: `'${parent}' in parents and trashed = false and mimeType != '${FOLDER}'`,
|
||||
q: `'${parent}' in parents and trashed = false`,
|
||||
corpora: "drive",
|
||||
driveId: process.env.GOOGLE_SHARED_DRIVE,
|
||||
includeItemsFromAllDrives: "true",
|
||||
|
|
@ -212,6 +218,53 @@ export async function driveList(folder) {
|
|||
return found;
|
||||
}
|
||||
|
||||
/* A folder and the folders under it, down to `depth` levels: the
|
||||
folders' paths (relative to it, "" for itself) and ids, and every
|
||||
file with the path of the folder it's in. */
|
||||
export async function driveTree(path, depth = 3) {
|
||||
const folderList = [{ path: "", id: await driveFolder(path) }];
|
||||
const files = [];
|
||||
for (let i = 0; i < folderList.length; i++) {
|
||||
const here = folderList[i];
|
||||
for (const item of await driveChildren(here.id)) {
|
||||
if (item.mimeType === FOLDER) {
|
||||
const sub = here.path ? `${here.path}/${item.name}` : item.name;
|
||||
if (sub.split("/").length <= depth) {
|
||||
folderList.push({ path: sub, id: item.id });
|
||||
// Known now: no lookup needed to upload into it.
|
||||
const full = `${path}/${sub}`;
|
||||
if (!folders.has(full)) folders.set(full, Promise.resolve(item.id));
|
||||
}
|
||||
} else {
|
||||
files.push({ ...item, folder: here.path });
|
||||
}
|
||||
}
|
||||
}
|
||||
return { folders: folderList, files };
|
||||
}
|
||||
|
||||
/* Whether a folder has anything in it (files or folders). */
|
||||
export async function driveFolderEmpty(id) {
|
||||
return (await driveChildren(id)).length === 0;
|
||||
}
|
||||
|
||||
/* A file or folder from one folder into another. */
|
||||
export async function driveMove(id, fromParent, toParent) {
|
||||
await call(
|
||||
`${DRIVE}/${encodeURIComponent(id)}?supportsAllDrives=true&addParents=${encodeURIComponent(toParent)}&removeParents=${encodeURIComponent(fromParent)}`,
|
||||
{ method: "PATCH", headers: { "Content-Type": "application/json" }, body: "{}" },
|
||||
DRIVE_SCOPE,
|
||||
);
|
||||
}
|
||||
|
||||
export async function driveRename(id, name) {
|
||||
await call(
|
||||
`${DRIVE}/${encodeURIComponent(id)}?supportsAllDrives=true`,
|
||||
{ method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name }) },
|
||||
DRIVE_SCOPE,
|
||||
);
|
||||
}
|
||||
|
||||
/* 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. */
|
||||
|
|
@ -244,8 +297,15 @@ export async function driveDownload(id) {
|
|||
return Buffer.from(await res.arrayBuffer());
|
||||
}
|
||||
|
||||
/* Into Drive's trash (a shared drive keeps it 30 days), not deleted
|
||||
outright: a shared drive's Content manager, the service account's
|
||||
role, may trash but not permanently delete. */
|
||||
export async function driveDelete(id) {
|
||||
await call(`${DRIVE}/${encodeURIComponent(id)}?supportsAllDrives=true`, { method: "DELETE" }, DRIVE_SCOPE);
|
||||
await call(
|
||||
`${DRIVE}/${encodeURIComponent(id)}?supportsAllDrives=true`,
|
||||
{ method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ trashed: true }) },
|
||||
DRIVE_SCOPE,
|
||||
);
|
||||
}
|
||||
|
||||
/* ── Gmail ───────────────────────────────────────────────────── */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue