new(basic.gblib): #394 UPLOAD keyword.

This commit is contained in:
Rodrigo Rodriguez 2023-12-12 23:47:19 -03:00
parent 3fa89851d2
commit 10871af6ca

View file

@ -69,7 +69,8 @@ import {
BlobServiceClient, BlobServiceClient,
BlockBlobClient, BlockBlobClient,
ContainerClient, ContainerClient,
StoragePipelineOptions StoragePipelineOptions,
StorageSharedKeyCredential
} from '@azure/storage-blob'; } from '@azure/storage-blob';
import { md5 } from 'js-md5'; import { md5 } from 'js-md5';
@ -672,31 +673,39 @@ export class SystemKeywords {
try { try {
data = GBServer.globals.files[data].data; data = GBServer.globals.files[data].data;
const accountName = min.getParam('Blob Account');
const accountKey = min.getParam('Blob Key');
const blobName = min.getParam('Blob Name'); const blobName = min.getParam('Blob Name');
const connString = min.getParam('Blob ConnectionString'); const sharedKeyCredential = new StorageSharedKeyCredential(
const containerName = min.getParam('Blob Container Name'); accountName,
const storagePipelineOptions: StoragePipelineOptions = {}; accountKey
const client: BlobServiceClient = BlobServiceClient.fromConnectionString(
connString,
storagePipelineOptions
); );
const container = client.getContainerClient(containerName); const baseUrl = `https://${accountName}.blob.core.windows.net`;
const blockBlobClient: BlockBlobClient = container.getBlockBlobClient(blobName);
const blobServiceClient = new BlobServiceClient(
`${baseUrl}`,
sharedKeyCredential
);
const container = blobServiceClient.getContainerClient(accountName);
const hash = new Uint8Array(md5.array(data)); const hash = new Uint8Array(md5.array(data));
const blockBlobClient: BlockBlobClient = container.getBlockBlobClient(blobName);
await blockBlobClient.uploadFile(data.filename, const res = await blockBlobClient.uploadFile(data.filename,
{blobHTTPHeaders:{ {
blobContentMD5:hash}}); blobHTTPHeaders: {
blobContentMD5: hash
const tmpFile = ''; }
await blockBlobClient.downloadToFile(tmpFile); });
if (res._response.status===200 && res.contentMD5 === hash) {
const tmpFile = '';
Fs.rmSync(tmpFile);
}
else{
GBLog.error(`BASIC: BLOB HTTP ${res.errorCode} ${res._response.status} .`);
}
Fs.rmSync(tmpFile);
} catch (error) { } catch (error) {
if (error.code === 'itemNotFound') { if (error.code === 'itemNotFound') {
GBLog.info(`BASIC: BASIC source file not found: ${file}.`); GBLog.info(`BASIC: BASIC source file not found: ${file}.`);
@ -1659,19 +1668,19 @@ export class SystemKeywords {
return res; return res;
} }
public async getCustomToken({pid, token}) :Promise<string>{ public async getCustomToken({ pid, token }): Promise<string> {
const { min } = await DialogKeywords.getProcessInfo(pid); const { min } = await DialogKeywords.getProcessInfo(pid);
GBLogEx.info(min, `GET TOKEN: ${token}`); GBLogEx.info(min, `GET TOKEN: ${token}`);
return await (min.adminService as any)['acquireElevatedToken'] return await (min.adminService as any)['acquireElevatedToken']
(min.instance.instanceId, false, (min.instance.instanceId, false,
token, token,
min.core.getParam(min.instance, `${token} Client ID`, null), min.core.getParam(min.instance, `${token} Client ID`, null),
min.core.getParam(min.instance, `${token} Client Secret`, null), min.core.getParam(min.instance, `${token} Client Secret`, null),
min.core.getParam(min.instance, `${token} Host`, null), min.core.getParam(min.instance, `${token} Host`, null),
min.core.getParam(min.instance, `${token} Tenant`, null) min.core.getParam(min.instance, `${token} Tenant`, null)
); );
} }
@ -1783,7 +1792,7 @@ export class SystemKeywords {
} }
if (res) { res['pageMode'] = pageMode; } if (res) { res['pageMode'] = pageMode; }
return res; return res;
} }
@ -2142,11 +2151,11 @@ export class SystemKeywords {
retries: 5, retries: 5,
onRetry: (err) => { GBLog.error(`MERGE: Retrying SELECT ALL on table: ${err.message}.`); } onRetry: (err) => { GBLog.error(`MERGE: Retrying SELECT ALL on table: ${err.message}.`); }
} }
); );
} }
else { else {
rows = this.cachedMerge[pid][file]; rows = this.cachedMerge[pid][file];
} }
} else { } else {
@ -2208,7 +2217,7 @@ export class SystemKeywords {
else { else {
table = this.cachedMerge[pid][file]; table = this.cachedMerge[pid][file];
} }
let key1Index, key2Index; let key1Index, key2Index;
if (key1) { if (key1) {
@ -2338,7 +2347,7 @@ export class SystemKeywords {
let dst = {}; let dst = {};
// Uppercases fields. // Uppercases fields.
let i = 0; let i = 0;
Object.keys(fieldsValues).forEach(fieldSrc => { Object.keys(fieldsValues).forEach(fieldSrc => {
const field = fieldsNames[i].charAt(0).toUpperCase() + fieldsNames[i].slice(1); const field = fieldsNames[i].charAt(0).toUpperCase() + fieldsNames[i].slice(1);
@ -2361,8 +2370,8 @@ export class SystemKeywords {
// In case of storage, persist to DB in batch. // In case of storage, persist to DB in batch.
if (fieldsValuesList.length){ if (fieldsValuesList.length) {
await this.saveToStorageBatch({ pid, table: file, rows:fieldsValuesList }); await this.saveToStorageBatch({ pid, table: file, rows: fieldsValuesList });
} }
GBLog.info(`BASIC: MERGE updated (merges:${merges}, additions:${adds}, skipped: ${skipped}).`); GBLog.info(`BASIC: MERGE updated (merges:${merges}, additions:${adds}, skipped: ${skipped}).`);
@ -2518,58 +2527,58 @@ export class SystemKeywords {
return { category: 'Other', description: 'General documents' }; return { category: 'Other', description: 'General documents' };
} }
/** /**
* Loads all para from tabular file Config.xlsx. * Loads all para from tabular file Config.xlsx.
*/ */
public async dirFolder( public async dirFolder(
min: GBMinInstance, min: GBMinInstance,
remotePath: string, remotePath: string,
baseUrl: string = null, baseUrl: string = null,
array = null array = null
): Promise<any> { ): Promise<any> {
GBLogEx.info(min, `dirFolder: remotePath=${remotePath}, baseUrl=${baseUrl}`); GBLogEx.info(min, `dirFolder: remotePath=${remotePath}, baseUrl=${baseUrl}`);
if (!baseUrl) { if (!baseUrl) {
let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min); let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min);
remotePath = remotePath.replace(/\\/gi, '/'); remotePath = remotePath.replace(/\\/gi, '/');
// Retrieves all files in remote folder. // Retrieves all files in remote folder.
let path = DialogKeywords.getGBAIPath(min.botId); let path = DialogKeywords.getGBAIPath(min.botId);
path = urlJoin(path, remotePath); path = urlJoin(path, remotePath);
let url = `${baseUrl}/drive/root:/${path}:/children`; 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);
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; 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;
}
});
}
} }
}
} }