From 4b4205a59905c7d0f0274b7a567eab488a76b781 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Tue, 12 Dec 2023 19:53:05 -0300 Subject: [PATCH] new(core.gbapp): Save to Azure Blob. --- package.json | 2 + .../basic.gblib/services/SystemKeywords.ts | 67 +++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/package.json b/package.json index 7aa386fe..7f7ca291 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "@azure/ms-rest-js": "2.6.2", "@azure/msal-node": "1.14.3", "@azure/search-documents": "11.3.1", + "@azure/storage-blob": "12.17.0", "@google-cloud/pubsub": "3.2.1", "@google-cloud/translate": "7.0.4", "@hubspot/api-client": "7.1.2", @@ -119,6 +120,7 @@ "googleapis": "109.0.1", "ibm-watson": "7.1.2", "join-images-updated": "1.1.4", + "js-md5": "0.8.3", "keyv": "4.5.2", "koa": "2.13.4", "koa-body": "6.0.1", diff --git a/packages/basic.gblib/services/SystemKeywords.ts b/packages/basic.gblib/services/SystemKeywords.ts index 7981f49c..5cbde6d5 100644 --- a/packages/basic.gblib/services/SystemKeywords.ts +++ b/packages/basic.gblib/services/SystemKeywords.ts @@ -65,6 +65,14 @@ import exts from '../../../extensions.json' assert { type: 'json' }; import { SecService } from '../../security.gbapp/services/SecService.js'; import { GBLogEx } from '../../core.gbapp/services/GBLogEx.js'; import retry from 'async-retry'; +import { + BlobServiceClient, + BlockBlobClient, + ContainerClient, + StoragePipelineOptions +} from '@azure/storage-blob'; + +import { md5 } from 'js-md5'; /** * @fileoverview General Bots server core. @@ -643,6 +651,63 @@ export class SystemKeywords { } } + /** + * Saves the content of variable into the file in .gbdata default folder. + * + * @exaple SAVE file AS "blob/my.txt" + * + */ + public async saveBlob({ pid, file, data }): Promise { + const { min, user } = await DialogKeywords.getProcessInfo(pid); + GBLog.info(`BASIC: Saving Blob'${file}' (SAVE file).`); + + let { baseUrl, client } = await GBDeployer.internalGetDriveClient(min); + const path = DialogKeywords.getGBAIPath(min.botId, `gbdrive`); + + // Checks if it is a GB FILE object. + + if (data.data && data.filename) { + data = data.data; + } + + try { + data = GBServer.globals.files[data].data; + + const blobName = min.getParam('Blob Name'); + const connString = min.getParam('Blob ConnectionString'); + const containerName = min.getParam('Blob Container Name'); + const storagePipelineOptions: StoragePipelineOptions = {}; + + const client: BlobServiceClient = BlobServiceClient.fromConnectionString( + connString, + storagePipelineOptions + ); + const container = client.getContainerClient(containerName); + const blockBlobClient: BlockBlobClient = container.getBlockBlobClient(blobName); + + const hash = new Uint8Array(md5.array(data)); + + await blockBlobClient.uploadFile(data.filename, + {blobHTTPHeaders:{ + blobContentMD5:hash}}); + + const tmpFile = ''; + await blockBlobClient.downloadToFile(tmpFile); + + Fs.rmSync(tmpFile); + + + } catch (error) { + if (error.code === 'itemNotFound') { + GBLog.info(`BASIC: BASIC source file not found: ${file}.`); + } else if (error.code === 'nameAlreadyExists') { + GBLog.info(`BASIC: BASIC destination file already exists: ${file}.`); + } + throw error; + } + } + + /** * Takes note inside a notes.xlsx of .gbdata. * @@ -2452,4 +2517,6 @@ export class SystemKeywords { } return { category: 'Other', description: 'General documents' }; } + + }