From 32d44447823f28fcb2c3a77b28f04ee900ff843e Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 6 Apr 2019 16:28:01 +0200 Subject: [PATCH 1/4] First Batch of changes to support Minio together with Azure as File Providers --- AABrowserAPI/BotCode.js | 61 --- AABrowserAPI/TeamSetup.js | 7 +- Project.njsproj | 10 +- Scripts/AppPreLoader.js | 36 ++ Server/{Storage.js => AzureStorage.js} | 36 +- ...anager.js => AzureStorageAccessManager.js} | 2 +- Server/BotsScripts.js | 7 +- Server/ConfigReader.js | 6 +- Server/MinioStorage.js | 463 ++++++++++++++++++ Server/SessionManager.js | 5 +- server.js | 98 ++-- 11 files changed, 599 insertions(+), 132 deletions(-) delete mode 100644 AABrowserAPI/BotCode.js rename Server/{Storage.js => AzureStorage.js} (92%) rename Server/{StorageAccessManager.js => AzureStorageAccessManager.js} (96%) create mode 100644 Server/MinioStorage.js diff --git a/AABrowserAPI/BotCode.js b/AABrowserAPI/BotCode.js deleted file mode 100644 index c8f2bf687..000000000 --- a/AABrowserAPI/BotCode.js +++ /dev/null @@ -1,61 +0,0 @@ -exports.newBotCode = function newBotCode() { - - let thisObject = { - saveBotCode: saveBotCode, - initialize: initialize - } - - let storage; - - return thisObject; - - function initialize(pServerConfig) { - - const STORAGE = require('../Server/Storage'); - storage = STORAGE.newStorage(); - - storage.initialize(undefined, pServerConfig); - - } - - function saveBotCode(pDevTeam, pSource, pRepo, pPath, pSourceCode, callBackFunction) { - - let blobText = nodify(pSourceCode.toString()); - - storage.writeData(pDevTeam + "/" + pSource, pRepo, pPath, blobText, onDataWritten); - - function onDataWritten(err) { - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - console.log("[ERROR] API -> saveBotCode -> Source code file could not be saved. "); - console.log("[ERROR] API -> saveBotCode -> err.message = " + err.message); - - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - callBackFunction(global.DEFAULT_OK_RESPONSE); - } - } - - function nodify(pData) { - - let start = pData.indexOf('function') + 9; - let end = pData.indexOf('(') + 0; - - let functionName = pData.substring(start, end); - - let result = "exports." + functionName + " = " + pData; - - result = replaceAll(result, "webRequire", "require"); - result = replaceAll(result, "window", "global"); - - return result; - - function replaceAll(text, search, replacement) { - return text.replace(new RegExp(search, 'g'), replacement); - } - } - -} \ No newline at end of file diff --git a/AABrowserAPI/TeamSetup.js b/AABrowserAPI/TeamSetup.js index 5fcd5485b..c7d5bef28 100644 --- a/AABrowserAPI/TeamSetup.js +++ b/AABrowserAPI/TeamSetup.js @@ -12,13 +12,10 @@ return thisObject; - function initialize(pServerConfig) { + function initialize(pServerConfig, pStorage) { - const STORAGE = require('../Server/Storage'); - storage = STORAGE.newStorage(); - masterAppServerURL = pServerConfig.masterAppServerURL; + storage = pStorage; - storage.initialize(undefined, pServerConfig); } function newTeam(pTeamCodeName, pTeamDisplayName, pUserName, pBotCodeName, pBotDisplayName, pUserId, callBackFunction) { diff --git a/Project.njsproj b/Project.njsproj index 0b11d7487..a5cb51182 100644 --- a/Project.njsproj +++ b/Project.njsproj @@ -30,9 +30,6 @@ true - - Code - Code @@ -44,6 +41,7 @@ + @@ -57,13 +55,13 @@ Code - + Code Code - + Code @@ -73,7 +71,7 @@ Code - + Code diff --git a/Scripts/AppPreLoader.js b/Scripts/AppPreLoader.js index 7faa9c053..e35291b0e 100644 --- a/Scripts/AppPreLoader.js +++ b/Scripts/AppPreLoader.js @@ -136,3 +136,39 @@ function callServer(pContentToSend, pPath, callBackFunction) { } } } + + +function getBlobToText(pContainerName, pPath, callBackFunction) { + + let xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = function () { + if (this.readyState === 4 && this.status === 200) { + + let response = JSON.parse(xhttp.responseText); + callBackFunction(response.err, response.text, ""); + + } + }; + + try { + + let request = { + conatinerName: pContainerName, + path: pPath + }; + + request = JSON.stringify(request); + + let path = "FileService"; + + let blob = new Blob([request], { type: 'text/plain' }); + + xhttp.open("POST", path, true); + xhttp.send(blob); + + } catch (err) { + + if (ERROR_LOG === true) { console.log(spacePad(MODULE_NAME, 50) + " : " + "[ERROR] getBlobToText -> err.message = " & err.message); } + + } +} diff --git a/Server/Storage.js b/Server/AzureStorage.js similarity index 92% rename from Server/Storage.js rename to Server/AzureStorage.js index f094c0fc7..707187d56 100644 --- a/Server/Storage.js +++ b/Server/AzureStorage.js @@ -1,4 +1,4 @@ -exports.newStorage = function newStorage() { +exports.newAzureStorage = function newAzureStorage() { let thisObject = { readData: readData, @@ -12,13 +12,24 @@ let storageData; let serverConfig; + let Minio = require('minio'); + let minioClient; + return thisObject; function initialize(pStorageData, pServerConfig) { storageData = pStorageData; serverConfig = pServerConfig; - + + minioClient = new Minio.Client({ + endPoint: process.env.MINIO_END_POINT, + port: JSON.parse(process.env.MINIO_PORT), + useSSL: JSON.parse(process.env.MINIO_USE_SSL), + accessKey: process.env.MINIO_ACCESS_KEY, + secretKey: process.env.MINIO_SECRET_KEY + }) + } function readData(pOrg, pPath, pFile, saveAtCache, callBackFunction) { @@ -98,6 +109,27 @@ } + /* TEMPORARY CODE TO UPDATE THE MINIO SERVER DURING TRANSITION. */ + + try { + + let bucketName = 'aaplatform'; + + let textFilename = pOrg + "/" + pPath + "/" + pFile; + + minioClient.putObject(bucketName, textFilename, text, 'text/plain', function (err) { + if (err) { + console.log("ERROR AT MINIO SERVER PUTTING OBJECT : " + err); + return; + } + console.log(textFilename + ' Successfully uploaded ' + textFilename + ' the MINIO SERVER'); + }) + + } catch (err) { + console.log("ERROR UPDATING MINIO SERVER : " + err); + } + + callBackFunction(global.DEFAULT_OK_RESPONSE, text); } catch (err) { diff --git a/Server/StorageAccessManager.js b/Server/AzureStorageAccessManager.js similarity index 96% rename from Server/StorageAccessManager.js rename to Server/AzureStorageAccessManager.js index f05ef7d7e..d3c7da237 100644 --- a/Server/StorageAccessManager.js +++ b/Server/AzureStorageAccessManager.js @@ -1,4 +1,4 @@ -exports.newStorageAccessManager = function newStorageAccessManager() { +exports.newAzureStorageAccessManager = function newAzureStorageAccessManager() { let thisObject = { getPermission: getPermission, diff --git a/Server/BotsScripts.js b/Server/BotsScripts.js index 1f58ade6d..c0fb31b91 100644 --- a/Server/BotsScripts.js +++ b/Server/BotsScripts.js @@ -5,15 +5,16 @@ initialize: initialize } - const STORAGE = require('../Server/Storage'); - let storage = STORAGE.newStorage(); + let storage; let serverConfig; let storageData; return thisObject; - function initialize(pServerConfig, callBackFunction) { + function initialize(pServerConfig, pStorage, callBackFunction) { + + storage = pStorage; let storageData = new Map(); // For backwards compatibility only. Not used. diff --git a/Server/ConfigReader.js b/Server/ConfigReader.js index 149506cd8..954aae42b 100644 --- a/Server/ConfigReader.js +++ b/Server/ConfigReader.js @@ -8,8 +8,7 @@ const GITHUB = require('../Server/Github'); let github = GITHUB.newGithub(); - const STORAGE = require('../Server/Storage'); - let storage = STORAGE.newStorage(); + let storage; let serverConfig; let storageData; @@ -18,8 +17,9 @@ return thisObject; - function initialize(pEcosystem, pEcosystemObject, pStorageData, callBackFunction) { + function initialize(pEcosystem, pEcosystemObject, pStorageData, pStorage, callBackFunction) { + storage = pStorage; storageData = pStorageData; ecosystem = pEcosystem; ecosystemObject = pEcosystemObject; diff --git a/Server/MinioStorage.js b/Server/MinioStorage.js new file mode 100644 index 000000000..5a129d343 --- /dev/null +++ b/Server/MinioStorage.js @@ -0,0 +1,463 @@ +exports.newMinioStorage = function newMinioStorage() { + + let thisObject = { + readData: readData, + writeData: writeData, + createContainer: createContainer, + deleteContainer: deleteContainer, + deleteBlob: deleteBlob, + initialize: initialize + } + + let storageData; + let serverConfig; + + let Minio = require('minio'); + let minioClient; + + return thisObject; + + function initialize(pStorageData, pServerConfig) { + + storageData = pStorageData; + serverConfig = pServerConfig; + + minioClient = new Minio.Client({ + endPoint: process.env.MINIO_END_POINT, + port: JSON.parse(process.env.MINIO_PORT), + useSSL: JSON.parse(process.env.MINIO_USE_SSL), + accessKey: process.env.MINIO_ACCESS_KEY, + secretKey: process.env.MINIO_SECRET_KEY + }) + + } + + function readData(pOrg, pPath, pFile, saveAtCache, callBackFunction) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> pOrg = " + pOrg); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> pPath = " + pPath); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> pFile = " + pFile); } + + let cacheVersion; + + if (storageData !== undefined) { + + cacheVersion = storageData.get(pOrg + '.' + pPath + '.' + pFile); + } + + if (cacheVersion !== undefined) { + + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> " + pOrg + '.' + pPath + '.' + pFile + " found at cache."); } + + callBackFunction(global.DEFAULT_OK_RESPONSE, cacheVersion); + + } else { + + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> " + pOrg + '.' + pPath + '.' + pFile + " NOT found at cache."); } + + let storage = require('azure-storage'); + let connectionString; + + switch (serverConfig.environment) { + + case "Develop": { + + connectionString = serverConfig.configAndPlugins.Develop.connectionString; + break; + } + + case "Production": { + + connectionString = serverConfig.configAndPlugins.Production.connectionString; + break; + } + } + + let blobService = storage.createBlobService(connectionString); + + blobService.getBlobToText('aaplatform', pOrg + "/" + pPath + "/" + pFile, onFileReceived); + + + function onFileReceived(err, text, response) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> err = " + JSON.stringify(err)); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> response = " + JSON.stringify(response)); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> pOrg = " + pOrg); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> pPath = " + pPath); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> pFile = " + pFile); } + + if (saveAtCache === true) { + + storageData.set(pOrg + '.' + pPath + '.' + pFile, text); + + } + + if (err !== null || text === null) { + + if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> readData -> onFileReceived -> Error Received from Storage Library. "); } + if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> readData -> onFileReceived -> err = " + JSON.stringify(err)); } + if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> readData -> onFileReceived -> Returning an empty JSON object string. "); } + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + + } + + /* TEMPORARY CODE TO UPDATE THE MINIO SERVER DURING TRANSITION. */ + + try { + + let bucketName = 'aaplatform'; + + let textFilename = pOrg + "/" + pPath + "/" + pFile; + + minioClient.putObject(bucketName, textFilename, text, 'text/plain', function (err) { + if (err) { + console.log("ERROR AT MINIO SERVER PUTTING OBJECT : " + err); + return; + } + console.log(textFilename + ' Successfully uploaded ' + textFilename + ' the MINIO SERVER'); + }) + + } catch (err) { + console.log("ERROR UPDATING MINIO SERVER : " + err); + } + + + callBackFunction(global.DEFAULT_OK_RESPONSE, text); + + } catch (err) { + console.log("[ERROR] Storage -> readData -> onFileReceived -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + } + + } catch (err) { + console.log("[ERROR] Storage -> readData -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function writeData(pOrg, pPath, pFile, pFileContent, callBackFunction) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pOrg = " + pOrg); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pPath = " + pPath); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pFile = " + pFile); } + + let storage = require('azure-storage'); + let connectionString; + + switch (serverConfig.environment) { + + case "Develop": { + + connectionString = serverConfig.configAndPlugins.Develop.connectionString; + break; + } + + case "Production": { + + connectionString = serverConfig.configAndPlugins.Production.connectionString; + break; + } + } + + let blobService = storage.createBlobService(connectionString); + let blobPath = pOrg + "/" + pPath + "/" + pFile; + let blobText = pFileContent.toString(); + + blobService.createBlockBlobFromText('aaplatform', blobPath, blobText, onFileCreated); + + /* TEMPORARY CODE TO UPDATE THE MINIO SERVER DURING TRANSITION. */ + + try { + + let bucketName = 'aaplatform'; + let text = blobText; + let textFilename = blobPath; + + minioClient.putObject(bucketName, textFilename, text, 'text/plain', function (err) { + if (err) { + console.log("ERROR AT MINIO SERVER PUTTING OBJECT : " + err); + return; + } + console.log(textFilename + ' Successfully uploaded ' + textFilename + ' the MINIO SERVER'); + }) + + } catch (err) { + console.log("ERROR UPDATING MINIO SERVER : " + err); + } + + function onFileCreated(err, text, response) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> err = " + JSON.stringify(err)); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> response = " + JSON.stringify(response)); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> pOrg = " + pOrg); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> pPath = " + pPath); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> pFile = " + pFile); } + + if (err !== null || text === null) { + + if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> writeData -> onFileCreated -> Error Received from Storage Library. "); } + if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> writeData -> onFileCreated -> err = " + JSON.stringify(err)); } + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + + } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + + } catch (err) { + console.log("[ERROR] Storage -> writeData -> onFileCreated -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + } catch (err) { + console.log("[ERROR] Storage -> writeData -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function deleteBlob(pOrg, pPath, pFile, callBackFunction) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> pOrg = " + pOrg); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> pPath = " + pPath); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> pFile = " + pFile); } + + let storage = require('azure-storage'); + let connectionString; + + switch (serverConfig.environment) { + + case "Develop": { + + connectionString = serverConfig.configAndPlugins.Develop.connectionString; + break; + } + + case "Production": { + + connectionString = serverConfig.configAndPlugins.Production.connectionString; + break; + } + } + + let blobService = storage.createBlobService(connectionString); + let blobPath = pOrg + "/" + pPath + "/" + pFile; + + blobService.deleteBlob('aaplatform', blobPath, onBlobDeleted); + + /* TEMPORARY CODE TO UPDATE THE MINIO SERVER DURING TRANSITION. */ + + try { + + let bucketName = 'aaplatform'; + let textFilename = blobPath; + + minioClient.removeObject(bucketName, textFilename, function (err) { + if (err) { + console.log("ERROR AT MINIO SERVER REMOVING OBJECT : " + textFilename + " " + err); + return; + } + console.log(textFilename + ' Successfully uploaded ' + textFilename + ' the MINIO SERVER'); + }) + + } catch (err) { + console.log("ERROR UPDATING MINIO SERVER : " + err); + } + + function onBlobDeleted(err, text, response) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> err = " + JSON.stringify(err)); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> response = " + JSON.stringify(response)); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> pOrg = " + pOrg); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> pPath = " + pPath); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> pFile = " + pFile); } + + if (err !== null || text === null) { + + if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> deleteBlob -> onBlobDeleted -> Error Received from Storage Library. "); } + if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> deleteBlob -> onBlobDeleted -> err = " + JSON.stringify(err)); } + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + + } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + + } catch (err) { + console.log("[ERROR] Storage -> deleteBlob -> onBlobDeleted -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + } catch (err) { + console.log("[ERROR] Storage -> deleteBlob -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function createContainer(pContainerName, callBackFunction) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> createContainer -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> createContainer -> pContainerName = " + pContainerName); } + + let storage = require('azure-storage'); + let connectionString; + + switch (serverConfig.environment) { + + case "Develop": { + + connectionString = serverConfig.configAndPlugins.Develop.connectionString; + break; + } + + case "Production": { + + connectionString = serverConfig.configAndPlugins.Production.connectionString; + break; + } + } + + let blobService = storage.createBlobService(connectionString); + + let containerName = pContainerName.toLowerCase(); + + blobService.createContainer(containerName, onContainerCreated); + + function onContainerCreated(err) { + + if (err) { + + if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> createContainer -> onContainerCreated -> Error Received from Storage Library. "); } + if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> createContainer -> onContainerCreated -> err = " + JSON.stringify(err)); } + + + /* ContainerAlreadyExists check */ + + if (JSON.stringify(err).indexOf("ContainerAlreadyExists") > 0) { + + let err = { + resutl: global.CUSTOM_FAIL_RESPONSE.result, + message: "ContainerAlreadyExists" + }; + + callBackFunction(err); + return; + + } else { + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + + } catch (err) { + console.log("[ERROR] Storage -> createContainer -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + function deleteContainer(pContainerName, callBackFunction) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteContainer -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteContainer -> pContainerName = " + pContainerName); } + + let storage = require('azure-storage'); + let connectionString; + + switch (serverConfig.environment) { + + case "Develop": { + + connectionString = serverConfig.configAndPlugins.Develop.connectionString; + break; + } + + case "Production": { + + connectionString = serverConfig.configAndPlugins.Production.connectionString; + break; + } + } + + let blobService = storage.createBlobService(connectionString); + + let containerName = pContainerName.toLowerCase(); + + blobService.deleteContainer(containerName, onContainerDeleted); + + function onContainerDeleted(err) { + + if (err) { + + if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> deleteContainer -> onContainerDeleted -> Error Received from Storage Library. "); } + if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> deleteContainer -> onContainerDeleted -> err = " + JSON.stringify(err)); } + + + /* ContainerAlreadyExists check */ + + if (JSON.stringify(err).indexOf("ContainerNotFound") > 0) { + + let err = { + resutl: global.CUSTOM_FAIL_RESPONSE.result, + message: "ContainerNotFound" + }; + + callBackFunction(err); + return; + + } else { + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + return; + } + + } catch (err) { + console.log("[ERROR] Storage -> deleteContainer -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } +} \ No newline at end of file diff --git a/Server/SessionManager.js b/Server/SessionManager.js index 73a05e60b..0f4183b25 100644 --- a/Server/SessionManager.js +++ b/Server/SessionManager.js @@ -9,7 +9,7 @@ return thisObject; - function initialize(pServerConfig, callBackFunction) { + function initialize(pServerConfig, storage, callBackFunction) { readSessions(); @@ -17,9 +17,6 @@ try { - const STORAGE = require('../Server/Storage'); - let storage = STORAGE.newStorage(); - storage.initialize(undefined, pServerConfig); storage.readData("AdvancedAlgos", "AAPlatform", "open.sessions.json", false, onData); diff --git a/server.js b/server.js index 3c712e8c7..581a5ca6a 100644 --- a/server.js +++ b/server.js @@ -37,15 +37,7 @@ global.CUSTOM_FAIL_RESPONSE = { } /* -var Minio = require('minio') -var minioClient = new Minio.Client({ - endPoint: 'niksadvancedalgos.ovh', - port: 80, - useSSL: false, - accessKey: 'luis1234', - secretKey: '123456789' -}) const stage = 'textUpload' const bucketName = 'test-bucket' @@ -77,8 +69,23 @@ let port = process.env.PORT || 1337 let isHttpServerStarted = false -const STORAGE = require('./Server/Storage') -let storage = STORAGE.newStorage() +const AZURE_STORAGE = require('./Server/AzureStorage') +const MINIO_STORAGE = require('./Server/MinioStorage') + +switch (process.env.STORAGE_PROVIDER) { + case 'Azure': { + storage = AZURE_STORAGE.newAzureStorage(); + break; + } + case 'Minio': { + storage = MINIO_STORAGE.newMinioStorage(); + break; + } + default: { + console.log('[ERROR] server -> Storage Provider not supported -> process.env.STORAGE_PROVIDER = ' + process.env.STORAGE_PROVIDER) + return; + } +} let sessionManager // This module have all authenticated active sessions. let storageAccessManager // This module manages the SAS for user to access the cloud storage from the browser. @@ -95,7 +102,7 @@ function initialize () { const CONFIG_READER = require('./Server/ConfigReader') let configReader = CONFIG_READER.newConfigReader() - configReader.initialize(ecosystem, ecosystemObject, storageData, onInitialized) + configReader.initialize(ecosystem, ecosystemObject, storageData, storage, onInitialized) function onInitialized (err, pServerConfig) { if (err.result !== global.DEFAULT_OK_RESPONSE.result) { @@ -125,11 +132,11 @@ function initialize () { const SESSION_MANAGER = require('./Server/SessionManager') sessionManager = SESSION_MANAGER.newSessionManager() - sessionManager.initialize(serverConfig, onInitialized) + sessionManager.initialize(serverConfig, storage, onInitialized) function onInitialized () { - const STORAGE_ACCESS_MANAGER = require('./Server/StorageAccessManager') - storageAccessManager = STORAGE_ACCESS_MANAGER.newStorageAccessManager() + const STORAGE_ACCESS_MANAGER = require('./Server/AzureStorageAccessManager') + storageAccessManager = STORAGE_ACCESS_MANAGER.newAzureStorageAccessManager() storageAccessManager.initialize(serverConfig, onInitialized) @@ -137,7 +144,7 @@ function initialize () { const BOT_SCRIPTS = require('./Server/BotsScripts') botScripts = BOT_SCRIPTS.newBotScripts() - botScripts.initialize(serverConfig, onInitialized) + botScripts.initialize(serverConfig, storage, onInitialized) function onInitialized () { startHtttpServer() @@ -203,7 +210,33 @@ function onBrowserRequest (request, response) { respondWithFile(filePath, response) break; - } + } + + case 'FileService': + { + + processPost(request, response, onPostReceived) + + function onPostReceived(pData) { + + let request = JSON.parse(pData); + + fileStorage.getFile(container, path, onReady); + + function onReady(err, pFileContent) { + + let response = { + err: err, + text: pFileContent + }; + + respondWithContent(JSON.stringify(response)); + } + } + + respondWithFile(filePath, response) + break; + } case 'Plotter.js': { /* @@ -475,35 +508,6 @@ function onBrowserRequest (request, response) { { switch (requestParameters[2]) { - case 'saveBotCode': { - const AABROWSER_API_BOT_CODE = require('./AABrowserAPI/' + 'BotCode') - let botCode = AABROWSER_API_BOT_CODE.newBotCode() - - botCode.initialize(serverConfig) - - processPost(request, response, onPostReceived) - - function onPostReceived (pData) { - let devTeam = requestParameters[3] - let source = requestParameters[4] + '/' + requestParameters[5] - let repo = requestParameters[6] - let path - - if (requestParameters[8] !== undefined) { - path = requestParameters[7] + '/' + requestParameters[8] - } else { - path = requestParameters[7] - } - botCode.saveBotCode(devTeam, source, repo, path, pData, onResponse) - - function onResponse (err) { - respondWithContent(JSON.stringify(err), response) - } - } - - break - } - case "authenticateUser": { const AABROWSER_API_USER_AUTHENTICATION = require('./AABrowserAPI/' + 'UserAuthentication'); @@ -530,7 +534,7 @@ function onBrowserRequest (request, response) { const AABROWSER_API_TEAM_SETUP = require('./AABrowserAPI/' + 'TeamSetup'); let teamSetup = AABROWSER_API_TEAM_SETUP.newTeamSetup(); - teamSetup.initialize(serverConfig); + teamSetup.initialize(serverConfig, storage); let devTeamCodeName = decodeURI(requestParameters[3]); let devTeamDisplayName = decodeURI(requestParameters[4]); @@ -575,7 +579,7 @@ function onBrowserRequest (request, response) { const AABROWSER_API_TEAM_SETUP = require('./AABrowserAPI/' + 'TeamSetup') let teamSetup = AABROWSER_API_TEAM_SETUP.newTeamSetup() - teamSetup.initialize(serverConfig) + teamSetup.initialize(serverConfig, storage) let devTeamCodeName = decodeURI(requestParameters[3]) let userName = decodeURI(requestParameters[4]) From a92ccff26558c746ec3d801c291f7bbcf4d5cd1a Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 6 Apr 2019 21:24:06 +0200 Subject: [PATCH 2/4] Working with Minio & Azure (missing Team management) --- Scripts/AppPreLoader.js | 51 ++++++++++------- Server/AzureStorage.js | 34 +++++++++-- Server/MinioStorage.js | 122 ++++++++++++++++++++++++---------------- server.js | 7 +-- 4 files changed, 135 insertions(+), 79 deletions(-) diff --git a/Scripts/AppPreLoader.js b/Scripts/AppPreLoader.js index e35291b0e..c8a3bdf8a 100644 --- a/Scripts/AppPreLoader.js +++ b/Scripts/AppPreLoader.js @@ -138,37 +138,48 @@ function callServer(pContentToSend, pPath, callBackFunction) { } -function getBlobToText(pContainerName, pPath, callBackFunction) { +function newFileStorage() { - let xhttp = new XMLHttpRequest(); - xhttp.onreadystatechange = function () { - if (this.readyState === 4 && this.status === 200) { - - let response = JSON.parse(xhttp.responseText); - callBackFunction(response.err, response.text, ""); - - } + thisObject = { + getBlobToText: getBlobToText }; - try { + return thisObject; - let request = { - conatinerName: pContainerName, - path: pPath + function getBlobToText(pContainerName, pPath, callBackFunction) { + + let xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = function () { + if (this.readyState === 4 && this.status === 200) { + + let response = JSON.parse(xhttp.responseText); + callBackFunction(response.err, response.text, ""); + + } }; - request = JSON.stringify(request); + try { - let path = "FileService"; + let request = { + conatinerName: pContainerName, + path: pPath + }; - let blob = new Blob([request], { type: 'text/plain' }); + request = JSON.stringify(request); - xhttp.open("POST", path, true); - xhttp.send(blob); + let path = "FileService"; - } catch (err) { + let blob = new Blob([request], { type: 'text/plain' }); - if (ERROR_LOG === true) { console.log(spacePad(MODULE_NAME, 50) + " : " + "[ERROR] getBlobToText -> err.message = " & err.message); } + xhttp.open("POST", path, true); + xhttp.send(blob); + } catch (err) { + + if (ERROR_LOG === true) { console.log(spacePad(MODULE_NAME, 50) + " : " + "[ERROR] getBlobToText -> err.message = " & err.message); } + + } } } + + diff --git a/Server/AzureStorage.js b/Server/AzureStorage.js index 707187d56..8a8a29b24 100644 --- a/Server/AzureStorage.js +++ b/Server/AzureStorage.js @@ -78,7 +78,35 @@ let blobService = storage.createBlobService(connectionString); - blobService.getBlobToText('aaplatform', pOrg + "/" + pPath + "/" + pFile, onFileReceived); + let pParam1 = pOrg; + let pParam2 = pPath; + let pParam3 = pFile; + let container = 'aaplatform'; + let azurePath = pOrg + "/" + pPath + "/" + pFile; + + let bucketName = 'aaplatform'; + let textFilename; + + if (pParam1 === undefined) { + + /* This is used when the requests come from the browser, for background compatibility. */ + + container = pParam3; + azurePath = pParam2; + + bucketName = pParam3; + textFilename = pParam2.substring(pParam2.indexOf("/") + 1, pParam2.length); + + } else { + + /* This is used for internal requests within AAWeb, for background compatibility. */ + + bucketName = 'aaplatform'; + textFilename = pParam1 + "/" + pParam2 + "/" + pParam3; + } + + + blobService.getBlobToText(container, azurePath, onFileReceived); function onFileReceived(err, text, response) { @@ -113,10 +141,6 @@ try { - let bucketName = 'aaplatform'; - - let textFilename = pOrg + "/" + pPath + "/" + pFile; - minioClient.putObject(bucketName, textFilename, text, 'text/plain', function (err) { if (err) { console.log("ERROR AT MINIO SERVER PUTTING OBJECT : " + err); diff --git a/Server/MinioStorage.js b/Server/MinioStorage.js index 5a129d343..050647ba7 100644 --- a/Server/MinioStorage.js +++ b/Server/MinioStorage.js @@ -32,31 +32,31 @@ } - function readData(pOrg, pPath, pFile, saveAtCache, callBackFunction) { + function readData(pParam1, pParam2, pParam3, saveAtCache, callBackFunction) { try { if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> pPath = " + pPath); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> pFile = " + pFile); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> pParam1 = " + pParam1); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> pParam2 = " + pParam2); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> pParam3 = " + pParam3); } let cacheVersion; if (storageData !== undefined) { - cacheVersion = storageData.get(pOrg + '.' + pPath + '.' + pFile); + cacheVersion = storageData.get(pParam1 + '.' + pParam2 + '.' + pParam3); } if (cacheVersion !== undefined) { - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> " + pOrg + '.' + pPath + '.' + pFile + " found at cache."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> " + pParam1 + '.' + pParam2 + '.' + pParam3 + " found at cache."); } callBackFunction(global.DEFAULT_OK_RESPONSE, cacheVersion); } else { - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> " + pOrg + '.' + pPath + '.' + pFile + " NOT found at cache."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> " + pParam1 + '.' + pParam2 + '.' + pParam3 + " NOT found at cache."); } let storage = require('azure-storage'); let connectionString; @@ -76,10 +76,41 @@ } } - let blobService = storage.createBlobService(connectionString); + let bucketName = 'aaplatform'; + let textFilename; - blobService.getBlobToText('aaplatform', pOrg + "/" + pPath + "/" + pFile, onFileReceived); + if (pParam1 === undefined) { + /* This is used when the requests come from the browser, for background compatibility. */ + + bucketName = pParam3; + textFilename = pParam2.substring(pParam2.indexOf("/") + 1, pParam2.length); + + } else { + + /* This is used for internal requests within AAWeb, for background compatibility. */ + + bucketName = 'aaplatform'; + textFilename = pParam1 + "/" + pParam2 + "/" + pParam3; + } + + minioClient.getObject(bucketName, textFilename, function (err, dataStream) { + let data = ''; + + if (err) { + onFileReceived(err, null, "Error retrieving file " + textFilename + " from bucket " + bucketName); + return + } + dataStream.on('data', function (chunk) { + data += chunk + }) + dataStream.on('end', function () { + onFileReceived(null, data, "Data retrieved."); + }) + dataStream.on('error', function (err) { + onFileReceived(err, null, "Error on data stream while retrieving file " + textFilename + " from bucket " + bucketName); + }) + }) function onFileReceived(err, text, response) { @@ -88,13 +119,13 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> Entering function."); } if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> err = " + JSON.stringify(err)); } if (LOG_FILE_CONTENT === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> response = " + JSON.stringify(response)); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> pPath = " + pPath); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> pFile = " + pFile); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> pParam1 = " + pParam1); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> pParam2 = " + pParam2); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> pParam3 = " + pParam3); } if (saveAtCache === true) { - storageData.set(pOrg + '.' + pPath + '.' + pFile, text); + storageData.set(pParam1 + '.' + pParam2 + '.' + pParam3, text); } @@ -104,32 +135,23 @@ if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> readData -> onFileReceived -> err = " + JSON.stringify(err)); } if (CONSOLE_ERROR_LOG === true) { console.log("[ERROR] Storage -> readData -> onFileReceived -> Returning an empty JSON object string. "); } + if (err.code === 'NoSuchKey') { + + let customErr = { + result: GLOBAL.CUSTOM_FAIL_RESPONSE.result, + message: 'FileNotFound', + code: 'FileNotFound' + } + + callBackFunction(customErr); + return; + } + callBackFunction(global.DEFAULT_FAIL_RESPONSE); return; } - /* TEMPORARY CODE TO UPDATE THE MINIO SERVER DURING TRANSITION. */ - - try { - - let bucketName = 'aaplatform'; - - let textFilename = pOrg + "/" + pPath + "/" + pFile; - - minioClient.putObject(bucketName, textFilename, text, 'text/plain', function (err) { - if (err) { - console.log("ERROR AT MINIO SERVER PUTTING OBJECT : " + err); - return; - } - console.log(textFilename + ' Successfully uploaded ' + textFilename + ' the MINIO SERVER'); - }) - - } catch (err) { - console.log("ERROR UPDATING MINIO SERVER : " + err); - } - - callBackFunction(global.DEFAULT_OK_RESPONSE, text); } catch (err) { @@ -145,14 +167,14 @@ } } - function writeData(pOrg, pPath, pFile, pFileContent, callBackFunction) { + function writeData(pParam1, pParam2, pParam3, pFileContent, callBackFunction) { try { if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pPath = " + pPath); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pFile = " + pFile); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pParam1 = " + pParam1); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pParam2 = " + pParam2); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pParam3 = " + pParam3); } let storage = require('azure-storage'); let connectionString; @@ -173,7 +195,7 @@ } let blobService = storage.createBlobService(connectionString); - let blobPath = pOrg + "/" + pPath + "/" + pFile; + let blobPath = pParam1 + "/" + pParam2 + "/" + pParam3; let blobText = pFileContent.toString(); blobService.createBlockBlobFromText('aaplatform', blobPath, blobText, onFileCreated); @@ -205,9 +227,9 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> Entering function."); } if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> err = " + JSON.stringify(err)); } if (LOG_FILE_CONTENT === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> response = " + JSON.stringify(response)); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> pPath = " + pPath); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> pFile = " + pFile); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> pParam1 = " + pParam1); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> pParam2 = " + pParam2); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> onFileCreated -> pParam3 = " + pParam3); } if (err !== null || text === null) { @@ -233,14 +255,14 @@ } } - function deleteBlob(pOrg, pPath, pFile, callBackFunction) { + function deleteBlob(pParam1, pParam2, pParam3, callBackFunction) { try { if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> pPath = " + pPath); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> pFile = " + pFile); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> pParam1 = " + pParam1); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> pParam2 = " + pParam2); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> pParam3 = " + pParam3); } let storage = require('azure-storage'); let connectionString; @@ -261,7 +283,7 @@ } let blobService = storage.createBlobService(connectionString); - let blobPath = pOrg + "/" + pPath + "/" + pFile; + let blobPath = pParam1 + "/" + pParam2 + "/" + pParam3; blobService.deleteBlob('aaplatform', blobPath, onBlobDeleted); @@ -291,9 +313,9 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> Entering function."); } if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> err = " + JSON.stringify(err)); } if (LOG_FILE_CONTENT === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> response = " + JSON.stringify(response)); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> pPath = " + pPath); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> pFile = " + pFile); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> pParam1 = " + pParam1); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> pParam2 = " + pParam2); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> onBlobDeleted -> pParam3 = " + pParam3); } if (err !== null || text === null) { diff --git a/server.js b/server.js index 581a5ca6a..c17c6097c 100644 --- a/server.js +++ b/server.js @@ -221,20 +221,19 @@ function onBrowserRequest (request, response) { let request = JSON.parse(pData); - fileStorage.getFile(container, path, onReady); + storage.readData(undefined, request.path, request.conatinerName, false, onReady) function onReady(err, pFileContent) { - let response = { + let responseToClient = { err: err, text: pFileContent }; - respondWithContent(JSON.stringify(response)); + respondWithContent(JSON.stringify(responseToClient), response); } } - respondWithFile(filePath, response) break; } case 'Plotter.js': From 2a4a9c13fedd024f9399a8b0cb998f4d5337aaee Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 7 Apr 2019 14:46:15 +0200 Subject: [PATCH 3/4] Final Stage for Minio File Server integration. Tested with the Teams module to create and delete teams. --- Server/MinioStorage.js | 152 +++-------------------------------------- 1 file changed, 9 insertions(+), 143 deletions(-) diff --git a/Server/MinioStorage.js b/Server/MinioStorage.js index 050647ba7..180bc03cc 100644 --- a/Server/MinioStorage.js +++ b/Server/MinioStorage.js @@ -29,7 +29,6 @@ accessKey: process.env.MINIO_ACCESS_KEY, secretKey: process.env.MINIO_SECRET_KEY }) - } function readData(pParam1, pParam2, pParam3, saveAtCache, callBackFunction) { @@ -58,24 +57,6 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> " + pParam1 + '.' + pParam2 + '.' + pParam3 + " NOT found at cache."); } - let storage = require('azure-storage'); - let connectionString; - - switch (serverConfig.environment) { - - case "Develop": { - - connectionString = serverConfig.configAndPlugins.Develop.connectionString; - break; - } - - case "Production": { - - connectionString = serverConfig.configAndPlugins.Production.connectionString; - break; - } - } - let bucketName = 'aaplatform'; let textFilename; @@ -176,49 +157,11 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pParam2 = " + pParam2); } if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> writeData -> pParam3 = " + pParam3); } - let storage = require('azure-storage'); - let connectionString; + let bucketName = 'aaplatform'; + let text = pFileContent.toString(); + let textFilename = pParam1 + "/" + pParam2 + "/" + pParam3; - switch (serverConfig.environment) { - - case "Develop": { - - connectionString = serverConfig.configAndPlugins.Develop.connectionString; - break; - } - - case "Production": { - - connectionString = serverConfig.configAndPlugins.Production.connectionString; - break; - } - } - - let blobService = storage.createBlobService(connectionString); - let blobPath = pParam1 + "/" + pParam2 + "/" + pParam3; - let blobText = pFileContent.toString(); - - blobService.createBlockBlobFromText('aaplatform', blobPath, blobText, onFileCreated); - - /* TEMPORARY CODE TO UPDATE THE MINIO SERVER DURING TRANSITION. */ - - try { - - let bucketName = 'aaplatform'; - let text = blobText; - let textFilename = blobPath; - - minioClient.putObject(bucketName, textFilename, text, 'text/plain', function (err) { - if (err) { - console.log("ERROR AT MINIO SERVER PUTTING OBJECT : " + err); - return; - } - console.log(textFilename + ' Successfully uploaded ' + textFilename + ' the MINIO SERVER'); - }) - - } catch (err) { - console.log("ERROR UPDATING MINIO SERVER : " + err); - } + minioClient.putObject(bucketName, textFilename, text, 'text/plain', onFileCreated) function onFileCreated(err, text, response) { @@ -264,47 +207,10 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> pParam2 = " + pParam2); } if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteBlob -> pParam3 = " + pParam3); } - let storage = require('azure-storage'); - let connectionString; + let bucketName = 'aaplatform'; + let textFilename = pParam1 + "/" + pParam2 + "/" + pParam3; - switch (serverConfig.environment) { - - case "Develop": { - - connectionString = serverConfig.configAndPlugins.Develop.connectionString; - break; - } - - case "Production": { - - connectionString = serverConfig.configAndPlugins.Production.connectionString; - break; - } - } - - let blobService = storage.createBlobService(connectionString); - let blobPath = pParam1 + "/" + pParam2 + "/" + pParam3; - - blobService.deleteBlob('aaplatform', blobPath, onBlobDeleted); - - /* TEMPORARY CODE TO UPDATE THE MINIO SERVER DURING TRANSITION. */ - - try { - - let bucketName = 'aaplatform'; - let textFilename = blobPath; - - minioClient.removeObject(bucketName, textFilename, function (err) { - if (err) { - console.log("ERROR AT MINIO SERVER REMOVING OBJECT : " + textFilename + " " + err); - return; - } - console.log(textFilename + ' Successfully uploaded ' + textFilename + ' the MINIO SERVER'); - }) - - } catch (err) { - console.log("ERROR UPDATING MINIO SERVER : " + err); - } + minioClient.removeObject(bucketName, textFilename, onBlobDeleted) function onBlobDeleted(err, text, response) { @@ -348,29 +254,9 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> createContainer -> Entering function."); } if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> createContainer -> pContainerName = " + pContainerName); } - let storage = require('azure-storage'); - let connectionString; - - switch (serverConfig.environment) { - - case "Develop": { - - connectionString = serverConfig.configAndPlugins.Develop.connectionString; - break; - } - - case "Production": { - - connectionString = serverConfig.configAndPlugins.Production.connectionString; - break; - } - } - - let blobService = storage.createBlobService(connectionString); - let containerName = pContainerName.toLowerCase(); - blobService.createContainer(containerName, onContainerCreated); + minioClient.makeBucket(containerName, "", onContainerCreated) function onContainerCreated(err) { @@ -419,29 +305,9 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteContainer -> Entering function."); } if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> deleteContainer -> pContainerName = " + pContainerName); } - let storage = require('azure-storage'); - let connectionString; - - switch (serverConfig.environment) { - - case "Develop": { - - connectionString = serverConfig.configAndPlugins.Develop.connectionString; - break; - } - - case "Production": { - - connectionString = serverConfig.configAndPlugins.Production.connectionString; - break; - } - } - - let blobService = storage.createBlobService(connectionString); - let containerName = pContainerName.toLowerCase(); - blobService.deleteContainer(containerName, onContainerDeleted); + minioClient.removeBucket(containerName, onContainerDeleted) function onContainerDeleted(err) { From d125a81950703ff401801166d7c1b5b536976ed6 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 8 Apr 2019 09:18:07 +0200 Subject: [PATCH 4/4] Cleaning Code --- server.js | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/server.js b/server.js index c17c6097c..675653a59 100644 --- a/server.js +++ b/server.js @@ -36,26 +36,6 @@ global.CUSTOM_FAIL_RESPONSE = { message: 'Custom Message' } -/* - - -const stage = 'textUpload' -const bucketName = 'test-bucket' -const textFilename = 'test.json' -/* -if (stage === 'textUpload') { - var str = 'some string you want to save' - minioClient.putObject(bucketName, textFilename, str, 'text/plain', function (e) { - if (e) { - return console.log(e) - } - console.log('Successfully uploaded the string') - }) -} - - -return -*/ let storageData = new Map() let botScripts // This module is the one which grabs user bots scrips from the storage, and browserifys them.