1.0.0 • Published 9 months ago

gdrivevault v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

gdrivevault

gdrivevault simplifies Google Drive integration for your Node.js projects. It lets you search and download files from specific Google Drive folders while managing a local database for faster searches.

npm

Quick start

  1. Install the package:
npm install gdrivevault
# or
yarn add gdrivevault
  1. Set up Google Drive API credentials:

    • Go to the Google Cloud Console
    • Create a new project or select an existing one
    • Enable the Google Drive API
    • Create OAuth 2.0 Client IDs credentials (choose "Desktop App" as the application type)
    • Download the credentials.json file and save it to storage/auth/credentials.json. You can customize the path if needed (see Customization)
  2. Initialize and use gdrivevault:

import {DriveFileManager} from 'gdrivevault';

const config = {
    folderId: 'your-google-drive-folder-id',
};

const driveManager = new DriveFileManager(config);
await driveManager.init();

// Search for files
const results = await driveManager.searchFiles('your-search-query');
console.log('Search Results:', results);

// Download a file
const fileLink = 'file-web-view-link';
const filePath = await driveManager.downloadFile(fileLink);
console.log(`File downloaded to: ${filePath}`);

// Refresh the local database
await driveManager.refreshDatabase();
console.log('Database refreshed successfully.');

On first run, you'll be prompted to authenticate your application through a browser window. Grant the necessary permissions, and gdrivevault will save the access token for future use.

Features

  • Easy Google Drive integration
  • Fast file searches using local database caching
  • Automatic database management
  • Customizable storage paths
  • Support for multiple Google Drive folders

Customization

You can customize various paths and settings:

const config = {
    folderId: 'your-google-drive-folder-id',
    tokenPath: './auth/tokens/token.json',
    credentialsPath: './auth/credentials.json',
    databasePath: './data/databases/my_custom_database.sqlite',
    downloadsPath: './my_downloads',
    logsPath: './my_logs',
};

API Reference

DriveFileManager

The main class for interacting with Google Drive.

constructor(config: DriveFileManagerConfig)

Configuration options

OptionTypeRequiredDefaultDescription
folderIdstringYesN/AGoogle Drive folder ID
tokenPathstringNo./storage/auth/tokens/token.jsonPath to store the authentication token
credentialsPathstringNo./storage/auth/credentials.jsonPath to your credentials.json file
databasePathstringNo./storage/databases/{folderId}_drive_database.sqlitePath to the SQLite database file
downloadsPathstringNo./storage/downloads/{folderId}Directory for downloaded files
logsPathstringNo./logs/{folderId}Directory for log files

Methods

  • init(): Promise<void>: Initialize the DriveFileManager
  • searchFiles(query: string): Promise<DatabaseFile[]>: Search for files
  • downloadFile(fileLink: string): Promise<string>: Download a file
  • refreshDatabase(): Promise<void>: Update the local database

Managing multiple folders

To manage multiple Google Drive folders, create separate instances of DriveFileManager:

const folderIds = ['folder-id-1', 'folder-id-2'];

const managers = await Promise.all(
    folderIds.map(async folderId => {
        const config = {folderId};
        const manager = new DriveFileManager(config);
        await manager.init();
        return manager;
    })
);

// Use managers[0] and managers[1] to interact with each folder

Contributing

We welcome contributions! Fork the repository and submit a pull request for any improvements or bug fixes.

License

This project is licensed under the MIT License.

Support

If you run into issues or have questions, please open an issue on our GitHub repository.