Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.2 KB
Raw Blame
import { Readable } from 'node:stream';
import {
  FilesService,
} from '@activepieces/pieces-framework';
import { extension } from 'mime-types';
import { GoogleDriveAuthValue, getAccessToken } from '../auth';

async function getMimeType(
  auth: GoogleDriveAuthValue,
  fileId: string
): Promise<string> {
  const accessToken = await getAccessToken(auth);
  const mimeType = (
    await fetch(
      `https://www.googleapis.com/drive/v3/files/${fileId}?fields=mimeType&supportsAllDrives=true`,
      {
        headers: {
          Authorization: `Bearer ${accessToken}`,
        },
      }
    ).then((res) => res.json())
  )['mimeType'] as string;
  return mimeType;
}

const googledlCall = async (
  url: string,
  auth: GoogleDriveAuthValue,
  fileId: string,
  files: FilesService,
  fileName: string | undefined
) => {
  const mimeType = await getMimeType(auth, fileId);
  const accessToken = await getAccessToken(auth);

  const response = await fetch(url, {
    headers: {
      Authorization: `Bearer ${accessToken}`,
    },
  }).catch((error) =>
    Promise.reject(
      new Error(
        `Error when download file:\n\tDownload file response: ${
          (error as Error).message ?? error
        }`
      )
    )
  );

  if (!response.ok || !response.body) {
    throw new Error(
      `Error when download file:\n\tDownload file response: ${response.status} ${response.statusText}`
    );
  }

  const extensionResult = extension(mimeType);
  const fileExtension = extensionResult ? '.' + extensionResult : '';
  const srcFileName = fileName ?? fileId + fileExtension;

  return files.write({
    fileName: srcFileName,
    // @ts-expect-error -- undici streams a Node web ReadableStream body; the DOM fetch types omit the fromWeb overload
    data: Readable.fromWeb(response.body),
  });
};

export async function downloadFileFromDrive(
  auth: GoogleDriveAuthValue,
  files: FilesService,
  fileId: string,
  fileName: string | undefined
): Promise<string> {
  let mimeType = await getMimeType(auth, fileId);

  // the google drive API doesn't allowed downloading google documents but we can export them to office formats
  if (
    [
      'application/vnd.google-apps.document',
      'application/vnd.google-apps.spreadsheet',
      'application/vnd.google-apps.presentation',
    ].includes(mimeType)
  ) {
    switch (mimeType) {
      case 'application/vnd.google-apps.document':
        mimeType =
          'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
        break;
      case 'application/vnd.google-apps.spreadsheet':
        mimeType =
          'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
        break;
      case 'application/vnd.google-apps.presentation':
        mimeType =
          'application/vnd.openxmlformats-officedocument.presentationml.presentation';
        break;
    }
    return await googledlCall(
      `https://www.googleapis.com/drive/v3/files/${fileId}/export?mimeType=${mimeType}&supportsAllDrives=true`,
      auth,
      fileId,
      files,
      fileName
    );
  } else {
    return await googledlCall(
      `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media&supportsAllDrives=true`,
      auth,
      fileId,
      files,
      fileName
    );
  }
}