Let the clip tool save a clip to a Drive folder
Save to Drive, beside Save to library, puts a finished clip in a folder under Media (Videos or Audio chosen first to match it) as a Files entry, shared by link, so pages can link to it (migration 051). The clip streams from disk through a resumable upload, so a long one isn't read into memory. A clip can be in both the library and Drive. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e1542d8d87
commit
42d1536ee6
6 changed files with 219 additions and 17 deletions
|
|
@ -32,7 +32,9 @@
|
|||
Setup: docs/google-drive-and-mail.md.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { readFileSync } from "node:fs";
|
||||
import { createReadStream, readFileSync } from "node:fs";
|
||||
import { stat } from "node:fs/promises";
|
||||
import { Readable } from "node:stream";
|
||||
import { createSign } from "node:crypto";
|
||||
|
||||
const DRIVE_SCOPE = "https://www.googleapis.com/auth/drive";
|
||||
|
|
@ -99,11 +101,11 @@ async function accessToken(scope, subject) {
|
|||
return data.access_token;
|
||||
}
|
||||
|
||||
async function call(url, init, scope, subject) {
|
||||
async function call(url, init, scope, subject, timeoutMs = 60_000) {
|
||||
const res = await fetch(url, {
|
||||
...init,
|
||||
headers: { ...init.headers, Authorization: `Bearer ${await accessToken(scope, subject)}` },
|
||||
signal: AbortSignal.timeout(60_000),
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const detail = await res.json().catch(() => null);
|
||||
|
|
@ -270,6 +272,41 @@ export async function driveRename(id, name) {
|
|||
);
|
||||
}
|
||||
|
||||
/* A file on disk into a folder of the shared drive, streamed through
|
||||
a resumable upload rather than read into memory (a video clip can
|
||||
be large). Returns { id, webViewLink }, as driveUpload does. */
|
||||
export async function driveUploadFile(folder, name, mime, filePath) {
|
||||
const { size } = await stat(filePath);
|
||||
const start = await call(
|
||||
`${DRIVE_UPLOAD}?uploadType=resumable&supportsAllDrives=true&fields=id,webViewLink`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json; charset=UTF-8",
|
||||
"X-Upload-Content-Type": mime,
|
||||
"X-Upload-Content-Length": String(size),
|
||||
},
|
||||
body: JSON.stringify({ name, parents: [await driveFolder(folder)] }),
|
||||
},
|
||||
DRIVE_SCOPE,
|
||||
);
|
||||
const session = start.headers.get("location");
|
||||
if (!session) throw new GoogleError("Drive didn't start the upload.");
|
||||
const res = await call(
|
||||
session,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": mime, "Content-Length": String(size) },
|
||||
body: Readable.toWeb(createReadStream(filePath)),
|
||||
duplex: "half",
|
||||
},
|
||||
DRIVE_SCOPE,
|
||||
undefined,
|
||||
30 * 60_000,
|
||||
);
|
||||
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. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue