google drive | merge google drive | sign a file url for uploading to google storage | Search

The code imports necessary dependencies, sets up constants for Google Drive authentication and file paths, and defines two asynchronous functions: convertGoogleDoc to convert Google Docs to various output types and downloadDocs to download Google Docs from a specified folder. The code uses Node.js modules such as path, fs, and google-auth to perform tasks, and includes error handling mechanisms to prevent file corruption or security issues.

Run example

npm run import -- "download all docs as actual data files"

download all docs as actual data files

const listDrive = importer.import("merge google drive")
const getRpcFromSpec = importer.import("get rpc from spec")
const authorize = importer.import("google oauth token client")
const google = {drive: ({version, auth}) => getRpcFromSpec(require(path.join(__dirname, `../Resources/APIs/drive.${version}.json`)), auth)}
const { safeurl } = importer.import("domain cache tools")
const GOOGLE_AUTH_SCOPE = [
  'https://www.googleapis.com/auth/drive'
]

const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const DOWNLOAD_PATH = path.join(PROFILE_PATH, 'Downloads')

async function convertGoogleDoc(fileId, outType) {
  let client = await authorize(GOOGLE_AUTH_SCOPE)
  let drive = await google.drive({version: 'v3', auth: client})
  return await drive.files.export({
    fileId: fileId,
    mimeType: outType
  })
}


async function downloadDocs(folder = 'root') {

  let drive = await listDrive(folder)

  for(let i = 0; i < drive.length; i++) {
    if(drive[i].mimeType == 'application/vnd.google-apps.document') {
      console.log('Exporting ' + drive[i].name)
      try {
        let response = await convertGoogleDoc(drive[i].id, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
        let data = await response.data.arrayBuffer()
        fs.writeFileSync(path.join(DOWNLOAD_PATH, safeurl(drive[i].name + '_' + drive[i].id) + '.docx'), Buffer.from(data))
      } catch (e) {
        console.log(e)
      }
    }

    if(drive[i].mimeType == 'application/vnd.google-apps.spreadsheet') {
      console.log('Exporting ' + drive[i].name)
      try {
        let response = await convertGoogleDoc(drive[i].id, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
        let data = await response.data.arrayBuffer()
        fs.writeFileSync(path.join(DOWNLOAD_PATH, safeurl(drive[i].name + '_' + drive[i].id) + '.xlsx'), Buffer.from(data))
      } catch (e) {
        console.log(e)
      }
    }
    // TODO: application/vnd.openxmlformats-officedocument.presentationml.presentation
  }
}

module.exports = {
  downloadDocs,
  convertGoogleDoc,
  
}

What the code could have been:

// Import required modules
const { importFromCache } = require('import-cache');
const { google } = require('googleapis');
const fs = require('fs');
const path = require('path');
const { safeUrl } = require('domain-cache-tools');
const { getRpcFromSpec } = require('get-rpc-from-spec');
const { listDrives, authorize } = require('google-drive-tools');

// Define constants
const GOOGLE_AUTH_SCOPE = [
  'https://www.googleapis.com/auth/drive'
];

const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const DOWNLOAD_PATH = path.join(PROFILE_PATH, 'Downloads');

// Define a function to get the Google Drive API client
async function getGoogleDriveClient(version, auth) {
  const driveApi = await getRpcFromSpec(require(path.join(__dirname, `../Resources/APIs/drive.${version}.json`)), auth);
  return google.drive({ version, auth });
}

// Define a function to convert a Google Doc to a specific file type
async function convertGoogleDoc(fileId, outType) {
  const client = await authorize(GOOGLE_AUTH_SCOPE);
  const drive = await getGoogleDriveClient('v3', client);
  return await drive.files.export({
    fileId,
    mimeType: outType
  });
}

// Define a function to download Google Docs from a specific folder
async function downloadDocs(folder = 'root') {
  const drives = await listDrives(folder);
  for (const drive of drives) {
    if (drive.mimeType === 'application/vnd.google-apps.document') {
      console.log(`Exporting ${drive.name}`);
      try {
        const data = await convertGoogleDoc(drive.id, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        fs.writeFileSync(path.join(DOWNLOAD_PATH, safeUrl(`${drive.name}_${drive.id}.docx`)), data);
      } catch (error) {
        console.error(error);
      }
    } else if (drive.mimeType === 'application/vnd.google-apps.spreadsheet') {
      console.log(`Exporting ${drive.name}`);
      try {
        const data = await convertGoogleDoc(drive.id, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        fs.writeFileSync(path.join(DOWNLOAD_PATH, safeUrl(`${drive.name}_${drive.id}.xlsx`)), data);
      } catch (error) {
        console.error(error);
      }
    }
  }
}

module.exports = {
  downloadDocs,
  convertGoogleDoc
};

Breakdown of the Code

Importing Dependencies

The code starts by importing various dependencies using the importer.import function. These dependencies include:

Setting up Constants

The code sets up several constants:

Defining Functions

The code defines two functions:

Notes