new(basic.gblib): #394 UPLOAD keyword.
This commit is contained in:
parent
3fa89851d2
commit
10871af6ca
1 changed files with 93 additions and 84 deletions
|
@ -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,29 +673,37 @@ 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 blobServiceClient = new BlobServiceClient(
|
||||||
|
`${baseUrl}`,
|
||||||
|
sharedKeyCredential
|
||||||
|
);
|
||||||
|
const container = blobServiceClient.getContainerClient(accountName);
|
||||||
|
const hash = new Uint8Array(md5.array(data));
|
||||||
const blockBlobClient: BlockBlobClient = container.getBlockBlobClient(blobName);
|
const blockBlobClient: BlockBlobClient = container.getBlockBlobClient(blobName);
|
||||||
|
|
||||||
const hash = new Uint8Array(md5.array(data));
|
const res = await blockBlobClient.uploadFile(data.filename,
|
||||||
|
{
|
||||||
|
blobHTTPHeaders: {
|
||||||
|
blobContentMD5: hash
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
await blockBlobClient.uploadFile(data.filename,
|
if (res._response.status===200 && res.contentMD5 === hash) {
|
||||||
{blobHTTPHeaders:{
|
const tmpFile = '';
|
||||||
blobContentMD5:hash}});
|
Fs.rmSync(tmpFile);
|
||||||
|
}
|
||||||
const tmpFile = '';
|
else{
|
||||||
await blockBlobClient.downloadToFile(tmpFile);
|
GBLog.error(`BASIC: BLOB HTTP ${res.errorCode} ${res._response.status} .`);
|
||||||
|
}
|
||||||
Fs.rmSync(tmpFile);
|
|
||||||
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -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)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -2146,7 +2155,7 @@ export class SystemKeywords {
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
rows = this.cachedMerge[pid][file];
|
rows = this.cachedMerge[pid][file];
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue