MCPcopy Index your code
hub / github.com/CapSoftware/Cap / ensureGoogleDriveFolder

Function ensureGoogleDriveFolder

packages/web-backend/src/Storage/GoogleDrive.ts:538–595  ·  view source on GitHub ↗
(
	config: GoogleDriveIntegrationConfig,
	name: string,
	parentId?: string,
	tokenStore?: GoogleDriveTokenStore,
)

Source from the content-addressed store, hash-verified

536 );
537
538export const ensureGoogleDriveFolder = (
539 config: GoogleDriveIntegrationConfig,
540 name: string,
541 parentId?: string,
542 tokenStore?: GoogleDriveTokenStore,
543) =>
544 Effect.gen(function* () {
545 const query = [
546 `name='${escapeDriveQueryValue(name)}'`,
547 "mimeType='application/vnd.google-apps.folder'",
548 "trashed=false",
549 ...(parentId ? [`'${escapeDriveQueryValue(parentId)}' in parents`] : []),
550 ].join(" and ");
551 const listUrl = appendSharedDriveListParams(
552 `${DRIVE_API_BASE}/files?q=${encodeURIComponent(query)}&fields=files(id,name)&spaces=drive`,
553 config,
554 );
555 const listResponse = yield* driveFetch(
556 config,
557 listUrl,
558 undefined,
559 tokenStore,
560 );
561 const listBody = yield* Effect.tryPromise({
562 try: () => parseDriveJson<GoogleDriveListResponse>(listResponse),
563 catch: (cause) => new Storage.StorageError({ cause }),
564 });
565 const existingId = listBody.files?.[0]?.id;
566 if (existingId) return existingId;
567
568 const createResponse = yield* driveFetch(
569 config,
570 appendSharedDriveCreateParams(`${DRIVE_API_BASE}/files`),
571 {
572 method: "POST",
573 headers: { "Content-Type": "application/json" },
574 body: JSON.stringify({
575 name,
576 mimeType: GOOGLE_DRIVE_FOLDER_MIME_TYPE,
577 ...(parentId ? { parents: [parentId] } : {}),
578 }),
579 },
580 tokenStore,
581 );
582
583 const created = yield* Effect.tryPromise({
584 try: () => parseDriveJson<GoogleDriveFile>(createResponse),
585 catch: (cause) => new Storage.StorageError({ cause }),
586 });
587 if (!created.id) {
588 return yield* Effect.fail(
589 new Storage.StorageError({
590 cause: new Error("Google Drive folder creation did not return an id"),
591 }),
592 );
593 }
594 return created.id;
595 });

Callers 1

storage.tsFile · 0.90

Calls 5

driveFetchFunction · 0.85
parseDriveJsonFunction · 0.85
escapeDriveQueryValueFunction · 0.70

Tested by

no test coverage detected