Class: Gdfs

Gdfs()

new Gdfs()

Gdfs class is an interface for the Google Drive API v3.

The instance manages a current working directory(CWD) and offers methods to operate files and folders on the Google Drive by its pathname.

Before creating an instance, the APIs must be loaded by the class method loadApi with a ClientId and ApiKey. These had to be created in a project of Google devloper Console.

And to operates files, user must sign-in with the Google account. See signIn and signOut.

Instance's CWD is initialized to the root on constructor. It can be changed by chdir method. When it is changed, the 'oncwdupdate' callback is fired. To know where the CWD is, The cwd method is available.

Source:

Members

(static) mimeTypeFolder :string

A mime type of the Google Drive's folder.

Type:
  • string
Source:

(static) signInStatusChangeEvent :GdfsEvent

signInStatusChangeEvent

Type:
Source:

Methods

(static) createClient() → {Gdfs}

Create Gdfs client.

Source:
Returns:

The google drive interface that has a current directory.

Type
Gdfs

(static) createFile(folderId, filename, mimeType) → {Promise.<object>}

Create a new file's resource.

Parameters:
Name Type Description
folderId string

The folder id where the file is created.

filename string

The file name.

mimeType string

The mime type for the new file.

Source:
Returns:

The response of the API.

Type
Promise.<object>

(static) downloadFile(fileId, acknowledgeAbuse) → {Promise.<string>}

Get a file content as text from Google Drive. Even if the file is not a text actually, it could be converted to ArrayBuffer, Blob or JSON to use by Web App.

Parameters:
Name Type Description
fileId string

The file id to download.

acknowledgeAbuse boolean | null

A user acknowledgment status for the potential to abuse. This parameter is optional. default value is false.

Source:
Returns:

A downloaded content as text.

Type
Promise.<string>

(async, static) findFileByName(parentFolderId, fileName) → {Promise.<Array.<object>>}

Find a file by name from a folder.

Parameters:
Name Type Description
parentFolderId string

A parent folder id.

fileName string

A file name to find

Source:
Returns:

A folder list that found.

Type
Promise.<Array.<object>>

(async, static) findFolderByName(parentFolderId, folderName) → {Array.<object>}

Find a folder by name from a folder.

Parameters:
Name Type Description
parentFolderId string

A parent folder id.

folderName string

A folder name to find

Source:
Returns:

A folder list that found.

Type
Array.<object>

(async, static) getActualRootFolderId() → {Promise.<string>}

Get actual root folder id.

Source:
Returns:

The root folder's id

Type
Promise.<string>

(async, static) getFileList(queryParameters) → {Promise.<object>}

Get file list.

Parameters:
Name Type Description
queryParameters object

The parameters for the API.

Source:
Returns:

The result of the API.

Type
Promise.<object>

(async, static) getFileResource(queryParameters) → {Promise.<object>}

Get file resource.

Parameters:
Name Type Description
queryParameters object

The parameters for the API.

Source:
Returns:

The result of the API.

Type
Promise.<object>

(static) isFolder(file) → {boolean}

Check if the file is a folder.

Parameters:
Name Type Description
file object

The file object provided from the result of getFileList method.

Source:
Returns:

The file is a folder or not.

Type
boolean

(static) isSignedIn() → {boolean}

Check if gapi was signed in.

Source:
Returns:

true if gapi is signed in, otherwise false.

Type
boolean

(static) loadApi(clientId, clientSecret) → {Promise}

Load Google Drive APIs and initialize its client object.

The loaded all APIs are accessible with a global gapi object. But it is wrapped by this class so the users should not use it directly.

Parameters:
Name Type Description
clientId string

A clientId from the Developer console.

clientSecret string

An clientSecret from the Developer console.

Source:
Returns:

A promise that will be resolved when the loading completed.

Type
Promise

(async, static) signIn() → {undefined}

Sign in to Google Drive.

Source:
Returns:
Type
undefined

(async, static) signOut() → {undefined}

Sign out from the Google Drive.

Source:
Returns:
Type
undefined

(static) updateFile(fileId, mimeType, data) → {Promise.<object>}

Upload a file content to update a existing file.

Parameters:
Name Type Description
fileId string

The file id to update.

mimeType string

The content type of the file.

data any

The file content.

Source:
Returns:

The response of the API.

Type
Promise.<object>

(async) chdir(directory) → {Promise.<boolean>}

Changes the current working directory of this client session.

Parameters:
Name Type Description
directory string

A pathname to operate.

Source:
Returns:

the status of the operation.

Type
Promise.<boolean>

(async) chdirById(folderId) → {Promise.<boolean>}

Move current directory to root, parent or one of children.

Parameters:
Name Type Description
folderId string

A destination file id to move. To move to parent, ".." is available.

Source:
Returns:

the status of the operation.

Type
Promise.<boolean>

cwd() → {string}

Get the current working directory of gdrive-fs.

Source:
Returns:

The current working directory.

Type
string

fireCwdUpdate() → {Promise}

Fire cwdUpdate.

Source:
Returns:

what the handler returns.

Type
Promise

getCurrentFolderId() → {string}

Get current folder id.

Source:
Returns:

The folder id that the instance is.

Type
string

getCurrentPath() → {GdfsPath}

Get current working directory as path object.

Source:
Returns:

the current working directory.

Type
GdfsPath

getFileOfPath(path) → {file}

Get the file object that the path points to.

Parameters:
Name Type Description
path GdfsPath

the path.

Source:
Returns:

the file object of google drive.

Type
file

(async) getPaths(path) → {Promise.<Array.<object>>}

Get an array of path element from root directory.

Parameters:
Name Type Description
path GdfsPath

path object.

Source:
Returns:

the array of the object having an id and the name.

Type
Promise.<Array.<object>>

(async) isDirectory(path) → {Promise.<Boolean>}

Check the path is a directory.

Parameters:
Name Type Description
path GdfsPath

A path to check

Source:
Returns:

The path is a directory or not.

Type
Promise.<Boolean>

(async) mkdir(path) → {Promise.<object>}

Make a directory.

Parameters:
Name Type Description
path string

A pathname to operate.

Source:
Returns:

The API response.

Type
Promise.<object>

onCwdUpdate(handler) → {undefined|function}

Set oncwdchage callback hander.

Parameters:
Name Type Description
handler FUnction | AsyncFunction

a function to be invoked when the current directory is changed.

Source:
Returns:

the previous handler will be returned.

Type
undefined | function

(async) readdir(path, options) → {Promise.<Array.<string>>}

Read the directory to get a list of filenames.

This method may not returns all files in the directory. To know all files were listed, check the pageToken field in the parameter options after the invocation. If the reading was completed, the field would be set null. The rest files unread will be returned at the next invocation with same parameters.

const readDirAll = async path => {
    const opts = { pageSize: 10, pageToken: null };
    const files = [];
    do {
       for(const fn of await files.readdir(path, opts)) {
           files.push(fn);
       }
    } while(opts.pageToken != null);
};
Parameters:
Name Type Description
path string

A path to the directory.

options object | null

(Optional) options for this method.

Only two fields are available:

  • "pageSize": Set maximum array size that this method returns at one time. The default value 10 will be used if this is not specified or zero or negative value is specified.
  • "pageToken": Set null to initial invocation to read from first entry. This would be updated other value if the unread files are remained. The value is used for reading next files. User should not set the value except for null.

If this parameter is ommited, all files will be read. This is not recomended feature for the directory that has a number of files.

Since:
  • v1.1.0
Source:
Returns:

returns an array of filenames.

Type
Promise.<Array.<string>>

(async) readFile(path) → {Promise.<string>}

Read a file. The file must have webContentLink in its resource to read the contents, To get the resource, Use Gdfs#stat.

Parameters:
Name Type Description
path string

A pathname to operate.

Source:
Returns:

The file content.

Type
Promise.<string>

(async) rmdir(path) → {Promise.<(object|null)>}

Remove the directory but not a normal file. The operation will fail, if it is not a directory nor empty.

Parameters:
Name Type Description
path string

A pathname to operate.

Source:
Returns:

Returns the API response. null means it was failed.

Type
Promise.<(object|null)>

(async) setCurrentPath(path) → {Promise.<boolean>}

Set current working directory with path object.

Parameters:
Name Type Description
path GdfsPath

the new current working directory.

Source:
Returns:

the status of the operation.

Type
Promise.<boolean>

(async) stat(path) → {File}

Get file's properties. It is a file resource of Google Drive including id, name, mimeType, webContentLink and webViewLink about the file or directory.

Parameters:
Name Type Description
path string

A pathname.

Since:
  • v1.1.0
Source:
Returns:

The file resource of Google Drive including id, name, mimeType, webContentLink and webViewLink about the file or directory.

Type
File

toAbsolutePath(path) → {GdfsPath}

Convert to absolute path.

Parameters:
Name Type Description
path GdfsPath

path to be converted

Source:
Returns:

An absolute path

Type
GdfsPath

Delete the file but not directory. This does not move the file to the trash-box.

Parameters:
Name Type Description
path string

A pathname to operate.

Source:
Returns:

Returns the API response. null means it was failed.

Type
Promise.<(object|null)>

(async) writeFile(path, mimeType, data) → {Promise.<object>}

Write a file.

Parameters:
Name Type Description
path string

A pathname to operate.

mimeType string

A mimeType of the file content.

data string

A file content.

Source:
Returns:

The API response.

Type
Promise.<object>