From 3fa89851d2b148670237ec71ad00a4cadd010884 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Tue, 12 Dec 2023 23:23:50 -0300 Subject: [PATCH] new(basic.gblib): #393 DIR keyword. --- .../basic.gblib/services/SystemKeywords.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/packages/basic.gblib/services/SystemKeywords.ts b/packages/basic.gblib/services/SystemKeywords.ts index 5cbde6d5..8928fd53 100644 --- a/packages/basic.gblib/services/SystemKeywords.ts +++ b/packages/basic.gblib/services/SystemKeywords.ts @@ -2518,5 +2518,58 @@ export class SystemKeywords { return { category: 'Other', description: 'General documents' }; } + /** + * Loads all para from tabular file Config.xlsx. + */ + public async dirFolder( + min: GBMinInstance, + remotePath: string, + baseUrl: string = null, + array = null + ): Promise { + GBLogEx.info(min, `dirFolder: remotePath=${remotePath}, baseUrl=${baseUrl}`); + if (!baseUrl) { + let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min); + + remotePath = remotePath.replace(/\\/gi, '/'); + + // Retrieves all files in remote folder. + + let path = DialogKeywords.getGBAIPath(min.botId); + path = urlJoin(path, remotePath); + let url = `${baseUrl}/drive/root:/${path}:/children`; + + const res = await client.api(url).get(); + const documents = res.value; + if (documents === undefined || documents.length === 0) { + GBLogEx.info(min, `${remotePath} is an empty folder.`); + return array; + } + + // Navigate files / directory to recurse. + + await CollectionUtil.asyncForEach(documents, async item => { + + if (item.folder) { + const nextFolder = urlJoin(remotePath, item.name); + array = [array, ... await this.dirFolder(min, null, nextFolder, array)]; + } else { + + // TODO: https://raw.githubusercontent.com/ishanarora04/quickxorhash/master/quickxorhash.js + + let obj = {}; + obj['modified'] = item.lastModifiedDateTime; + obj['name'] = item.name; + obj['size'] = item.size; + obj['hash'] = item.file?.hashes?.quickXorHash; + obj['path'] = Path.join(remotePath, item.name); + + array.push(obj); + + return array; + } + }); + } + } }