UNPKG

many-cloud

Version:

A Node module for abstracting file management and interfacing with a variety of cloud storages.

82 lines (75 loc) 2.11 kB
const Item = require("./item"); /** * Folder class * @constructor * @param {String} id - Folder id. "root" for root folder. * @param {Object} connection - A connection to a Google Drive integration. */ function Folder(id, connection) { this.id = id; this.type = "folder"; this.connection = connection; } Folder.prototype = Object.create(Item.prototype); Folder.prototype.constructor = Folder; /** * List the files in this folder. * * @return {Promise} A promise that returns a list of files when resolved */ Folder.prototype.list_files = function() { const File = require("./file"); return new Promise(async (resolve, reject) => { try { let res = (await this.connection.list_all_files(this.id)).files; let ret = []; for (let i = 0; i < res.length; i++) { let f; if (res[i].mimeType === "application/vnd.google-apps.folder") { f = new Folder(res[i].id, this.connection); } else { f = new File(res[i].id, this.connection); } f.name = res[i].name; ret.push(f); } resolve(ret); } catch (err) { reject(err); } }); }; /** * Upload a local file to this folder. * * @param path path to local file to upload * @return {Promise} A promise that returns a file object when resolved */ Folder.prototype.upload_file = function(path) { const File = require("./file"); return new Promise(async (resolve, reject) => { try { let res = await this.connection.upload_file(this.id, path); resolve(new File(res.id, this.connection)); } catch (err) { reject(err); } }); }; /** * Creates a new folder inside of this folder * * @param name Name of folder to create * @return {Promise} A promise that returns a folder object when resolved */ Folder.prototype.new_folder = function(name) { return new Promise(async (resolve, reject) => { try { let res = await this.connection.new_folder(this.id, name); resolve(new Folder(res.id, this.connection)); } catch (err) { reject(err); } }); }; module.exports = Folder;