botinabox 2.8.0 → 2.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -284,4 +284,86 @@ declare class GoogleDriveConnector implements Connector<DriveFileRecord> {
|
|
|
284
284
|
private mapFile;
|
|
285
285
|
}
|
|
286
286
|
|
|
287
|
-
|
|
287
|
+
/**
|
|
288
|
+
* Google Drive file read primitives — download or export files to raw bytes.
|
|
289
|
+
*
|
|
290
|
+
* Signature-agnostic primitives that produce `{ buffer, mimeType, filename }`
|
|
291
|
+
* tuples for use in attachment enrichment pipelines and other file consumers.
|
|
292
|
+
*/
|
|
293
|
+
/**
|
|
294
|
+
* Raw file bytes fetched from Google Drive, plus the metadata needed to
|
|
295
|
+
* decide what to do with them (MIME type → extractor, filename → display).
|
|
296
|
+
*/
|
|
297
|
+
interface DriveFileBytes {
|
|
298
|
+
buffer: Buffer;
|
|
299
|
+
mimeType: string;
|
|
300
|
+
filename: string;
|
|
301
|
+
/** Size in bytes as reported by the download response. */
|
|
302
|
+
size: number;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Supported export target formats for Google-native file types
|
|
306
|
+
* (Docs, Sheets, Slides, Drawings).
|
|
307
|
+
*/
|
|
308
|
+
type GoogleDocExportAs = 'docx' | 'xlsx' | 'pptx' | 'png' | 'pdf' | 'txt' | 'csv' | 'html';
|
|
309
|
+
/**
|
|
310
|
+
* Download a binary Drive file as raw bytes.
|
|
311
|
+
*
|
|
312
|
+
* Use this for files uploaded to Drive in their native format (pdf, docx,
|
|
313
|
+
* xlsx, pptx, jpg, png, mp4, etc.) — anything where the Drive `mimeType`
|
|
314
|
+
* is NOT `application/vnd.google-apps.*`.
|
|
315
|
+
*
|
|
316
|
+
* For Google-native formats (Docs, Sheets, Slides, Drawings) use
|
|
317
|
+
* `exportGoogleDoc` instead — `files.get` with `alt: 'media'` fails on
|
|
318
|
+
* those with `fileNotDownloadable`.
|
|
319
|
+
*
|
|
320
|
+
* @param drive - googleapis `drive_v3.Drive` instance (kept as `any` to avoid hard type import)
|
|
321
|
+
* @param fileId - Drive file ID
|
|
322
|
+
* @returns Promise resolving to `DriveFileBytes` with buffer, mimeType, filename, and size
|
|
323
|
+
*/
|
|
324
|
+
declare function downloadDriveFile(drive: any, fileId: string): Promise<DriveFileBytes>;
|
|
325
|
+
/**
|
|
326
|
+
* Export a Google-native document to a downloadable format.
|
|
327
|
+
*
|
|
328
|
+
* The `exportAs` argument picks the target MIME type:
|
|
329
|
+
* - docx → application/vnd.openxmlformats-officedocument.wordprocessingml.document
|
|
330
|
+
* - xlsx → application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
|
|
331
|
+
* - pptx → application/vnd.openxmlformats-officedocument.presentationml.presentation
|
|
332
|
+
* - png → image/png
|
|
333
|
+
* - pdf → application/pdf
|
|
334
|
+
* - txt → text/plain
|
|
335
|
+
* - csv → text/csv (only valid for spreadsheets)
|
|
336
|
+
* - html → text/html
|
|
337
|
+
*
|
|
338
|
+
* Throws if the source file is not a Google-native type, or if the
|
|
339
|
+
* requested export is not supported for that type (e.g., csv on a Doc).
|
|
340
|
+
*
|
|
341
|
+
* @param drive - googleapis `drive_v3.Drive` instance
|
|
342
|
+
* @param fileId - Drive file ID
|
|
343
|
+
* @param exportAs - Target export format
|
|
344
|
+
* @returns Promise resolving to `DriveFileBytes` with exported content
|
|
345
|
+
*/
|
|
346
|
+
declare function exportGoogleDoc(drive: any, fileId: string, exportAs: GoogleDocExportAs): Promise<DriveFileBytes>;
|
|
347
|
+
/**
|
|
348
|
+
* High-level dispatcher. Fetches file metadata, inspects the mimeType,
|
|
349
|
+
* and picks `downloadDriveFile` (binary) or `exportGoogleDoc` (native)
|
|
350
|
+
* automatically.
|
|
351
|
+
*
|
|
352
|
+
* Google-native → exported format mapping:
|
|
353
|
+
* - Docs → docx
|
|
354
|
+
* - Sheets → xlsx
|
|
355
|
+
* - Slides → pptx
|
|
356
|
+
* - Drawings → png
|
|
357
|
+
* - Scripts → throws (not supported)
|
|
358
|
+
* - Forms/Site → throws (not supported)
|
|
359
|
+
*
|
|
360
|
+
* Consumers that want a different export target (e.g., gdoc → pdf for
|
|
361
|
+
* Claude's native PDF reading) should call `exportGoogleDoc` directly.
|
|
362
|
+
*
|
|
363
|
+
* @param drive - googleapis `drive_v3.Drive` instance
|
|
364
|
+
* @param fileId - Drive file ID
|
|
365
|
+
* @returns Promise resolving to `DriveFileBytes` with automatically chosen format
|
|
366
|
+
*/
|
|
367
|
+
declare function readDriveFile(drive: any, fileId: string): Promise<DriveFileBytes>;
|
|
368
|
+
|
|
369
|
+
export { type CalendarAttendee, type CalendarConnectorOpts, type CalendarEventRecord, type DriveConnectorOpts, type DriveFileBytes, type DriveFileRecord, type DriveOwner, type EmailAddress, type EmailAttachment, type EmailRecord, type GmailConnectorOpts, GoogleCalendarConnector, type GoogleConnectorConfig, type GoogleDocExportAs, GoogleDriveConnector, GoogleGmailConnector, type GoogleOAuthConfig, type GoogleServiceAccountConfig, type GoogleTokens, createOAuth2Client, createServiceAccountClient, downloadDriveFile, exchangeCode, exportGoogleDoc, getAuthUrl, loadTokens, readDriveFile, refreshIfNeeded, saveTokens };
|
|
@@ -474,15 +474,115 @@ var GoogleDriveConnector = class {
|
|
|
474
474
|
function errorMessage2(err) {
|
|
475
475
|
return err instanceof Error ? err.message : String(err);
|
|
476
476
|
}
|
|
477
|
+
|
|
478
|
+
// src/connectors/google/drive-read.ts
|
|
479
|
+
var GOOGLE_NATIVE_PREFIX = "application/vnd.google-apps.";
|
|
480
|
+
var GOOGLE_DOC = "application/vnd.google-apps.document";
|
|
481
|
+
var GOOGLE_SHEET = "application/vnd.google-apps.spreadsheet";
|
|
482
|
+
var GOOGLE_SLIDES = "application/vnd.google-apps.presentation";
|
|
483
|
+
var GOOGLE_DRAWING = "application/vnd.google-apps.drawing";
|
|
484
|
+
var EXPORT_MIME = {
|
|
485
|
+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
486
|
+
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
487
|
+
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
488
|
+
png: "image/png",
|
|
489
|
+
pdf: "application/pdf",
|
|
490
|
+
txt: "text/plain",
|
|
491
|
+
csv: "text/csv",
|
|
492
|
+
html: "text/html"
|
|
493
|
+
};
|
|
494
|
+
var EXPORT_EXTENSION = {
|
|
495
|
+
docx: "docx",
|
|
496
|
+
xlsx: "xlsx",
|
|
497
|
+
pptx: "pptx",
|
|
498
|
+
png: "png",
|
|
499
|
+
pdf: "pdf",
|
|
500
|
+
txt: "txt",
|
|
501
|
+
csv: "csv",
|
|
502
|
+
html: "html"
|
|
503
|
+
};
|
|
504
|
+
async function downloadDriveFile(drive, fileId) {
|
|
505
|
+
const metaRes = await drive.files.get({
|
|
506
|
+
fileId,
|
|
507
|
+
fields: "name,mimeType,size"
|
|
508
|
+
});
|
|
509
|
+
const { name: filename, mimeType, size } = metaRes.data;
|
|
510
|
+
const response = await drive.files.get(
|
|
511
|
+
{ fileId, alt: "media" },
|
|
512
|
+
{ responseType: "arraybuffer" }
|
|
513
|
+
);
|
|
514
|
+
const buffer = Buffer.from(response.data);
|
|
515
|
+
return {
|
|
516
|
+
buffer,
|
|
517
|
+
mimeType: mimeType ?? "application/octet-stream",
|
|
518
|
+
filename: filename ?? "document",
|
|
519
|
+
size: size ? parseInt(String(size), 10) : buffer.length
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
async function exportGoogleDoc(drive, fileId, exportAs) {
|
|
523
|
+
const metaRes = await drive.files.get({
|
|
524
|
+
fileId,
|
|
525
|
+
fields: "name,mimeType"
|
|
526
|
+
});
|
|
527
|
+
const { name: filename, mimeType } = metaRes.data;
|
|
528
|
+
if (!mimeType?.startsWith(GOOGLE_NATIVE_PREFIX)) {
|
|
529
|
+
throw new Error(
|
|
530
|
+
`Cannot export non-Google-native file. mimeType: ${mimeType}. Use downloadDriveFile instead.`
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
const targetMimeType = EXPORT_MIME[exportAs];
|
|
534
|
+
const response = await drive.files.export(
|
|
535
|
+
{ fileId, mimeType: targetMimeType },
|
|
536
|
+
{ responseType: "arraybuffer" }
|
|
537
|
+
);
|
|
538
|
+
const buffer = Buffer.from(response.data);
|
|
539
|
+
const extension = EXPORT_EXTENSION[exportAs];
|
|
540
|
+
let finalFilename = filename ?? "document";
|
|
541
|
+
if (!finalFilename.endsWith(`.${extension}`)) {
|
|
542
|
+
finalFilename = `${finalFilename}.${extension}`;
|
|
543
|
+
}
|
|
544
|
+
return {
|
|
545
|
+
buffer,
|
|
546
|
+
mimeType: targetMimeType,
|
|
547
|
+
filename: finalFilename,
|
|
548
|
+
size: buffer.length
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
async function readDriveFile(drive, fileId) {
|
|
552
|
+
const metaRes = await drive.files.get({
|
|
553
|
+
fileId,
|
|
554
|
+
fields: "name,mimeType,size"
|
|
555
|
+
});
|
|
556
|
+
const { mimeType } = metaRes.data;
|
|
557
|
+
if (mimeType?.startsWith(GOOGLE_NATIVE_PREFIX)) {
|
|
558
|
+
if (mimeType === GOOGLE_DOC) {
|
|
559
|
+
return exportGoogleDoc(drive, fileId, "docx");
|
|
560
|
+
}
|
|
561
|
+
if (mimeType === GOOGLE_SHEET) {
|
|
562
|
+
return exportGoogleDoc(drive, fileId, "xlsx");
|
|
563
|
+
}
|
|
564
|
+
if (mimeType === GOOGLE_SLIDES) {
|
|
565
|
+
return exportGoogleDoc(drive, fileId, "pptx");
|
|
566
|
+
}
|
|
567
|
+
if (mimeType === GOOGLE_DRAWING) {
|
|
568
|
+
return exportGoogleDoc(drive, fileId, "png");
|
|
569
|
+
}
|
|
570
|
+
throw new Error(`Unsupported Google-native type: ${mimeType}`);
|
|
571
|
+
}
|
|
572
|
+
return downloadDriveFile(drive, fileId);
|
|
573
|
+
}
|
|
477
574
|
export {
|
|
478
575
|
GoogleCalendarConnector,
|
|
479
576
|
GoogleDriveConnector,
|
|
480
577
|
GoogleGmailConnector,
|
|
481
578
|
createOAuth2Client,
|
|
482
579
|
createServiceAccountClient,
|
|
580
|
+
downloadDriveFile,
|
|
483
581
|
exchangeCode,
|
|
582
|
+
exportGoogleDoc,
|
|
484
583
|
getAuthUrl,
|
|
485
584
|
loadTokens,
|
|
585
|
+
readDriveFile,
|
|
486
586
|
refreshIfNeeded,
|
|
487
587
|
saveTokens
|
|
488
588
|
};
|