diff --git a/AABrowserAPI/BotCode.js b/AABrowserAPI/BotCode.js index b60e05229..c8f2bf687 100644 --- a/AABrowserAPI/BotCode.js +++ b/AABrowserAPI/BotCode.js @@ -5,19 +5,24 @@ initialize: initialize } - let serverConfig; + let storage; return thisObject; function initialize(pServerConfig) { - serverConfig = pServerConfig; + const STORAGE = require('../Server/Storage'); + storage = STORAGE.newStorage(); + storage.initialize(undefined, pServerConfig); + } function saveBotCode(pDevTeam, pSource, pRepo, pPath, pSourceCode, callBackFunction) { - writeData(pDevTeam + "/" + pSource, pRepo, pPath, pSourceCode, onDataWritten); + let blobText = nodify(pSourceCode.toString()); + + storage.writeData(pDevTeam + "/" + pSource, pRepo, pPath, blobText, onDataWritten); function onDataWritten(err) { @@ -34,80 +39,8 @@ } } - function writeData(pOrg, pRepo, pPath, pFileContent, callBackFunction) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> pRepo = " + pRepo); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> pPath = " + pPath); } - - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> " + pOrg + '.' + pRepo + '.' + pPath + " 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); - let blobPath = pOrg + "/" + pRepo + "/" + pPath; - let blobText = nodify(pFileContent.toString()); - - blobService.createBlockBlobFromText('aaplatform', blobPath, blobText, onFileCreated); - - function onFileCreated(err, text, response) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> err = " + JSON.stringify(err)); } - if (LOG_FILE_CONTENT === true) { console.log("[INFO] API -> writeData -> onFileCreated -> response = " + JSON.stringify(response)); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> pRepo = " + pRepo); } - if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> pPath = " + pPath); } - - if (err !== null || text === null) { - - if (CONSOLE_LOG === true) { console.log("[ERROR] API -> writeData -> onFileCreated -> Error Received from API Library. "); } - if (CONSOLE_LOG === true) { console.log("[ERROR] API -> writeData -> onFileCreated -> err = " + JSON.stringify(err)); } - if (CONSOLE_LOG === true) { console.log("[ERROR] API -> writeData -> onFileCreated -> Returning an empty JSON object string. "); } - - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - - } - - callBackFunction(global.DEFAULT_OK_RESPONSE); - - } catch (err) { - console.log("[ERROR] API -> writeData -> onFileCreated -> err.message = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - - } catch (err) { - console.log("[ERROR] API -> writeData -> err.message = " + err.message); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - function nodify(pData) { - let start = pData.indexOf('function') + 9; let end = pData.indexOf('(') + 0; diff --git a/Server/BotsScripts.js b/Server/BotsScripts.js index 0c1a0f54f..9d662f2fc 100644 --- a/Server/BotsScripts.js +++ b/Server/BotsScripts.js @@ -24,7 +24,7 @@ function getScript(pDevTeam, pSource, pRepo, pPath, callBackFunction) { - storage.getData(pDevTeam + "/" + pSource, pRepo, pPath, false, onDataArrived); + storage.readData(pDevTeam + "/" + pSource, pRepo, pPath, false, onDataArrived); function onDataArrived(pData) { diff --git a/Server/CloudScripts.js b/Server/CloudScripts.js index 135e362e5..abd880b81 100644 --- a/Server/CloudScripts.js +++ b/Server/CloudScripts.js @@ -42,7 +42,7 @@ */ - storage.getData('AdvancedAlgos', 'AACloud', 'web.config.json', true, onDataArrived); + storage.readData('AdvancedAlgos', 'AACloud', 'web.config.json', true, onDataArrived); function onDataArrived(pData) { @@ -84,7 +84,7 @@ let webModule = pCloudWebConfig.webModules[i].name + '.js'; - storage.getData('AdvancedAlgos', 'AACloud', webModule, true, onDataArrived); + storage.readData('AdvancedAlgos', 'AACloud', webModule, true, onDataArrived); function onDataArrived(pData) { diff --git a/Server/ConfigReader.js b/Server/ConfigReader.js index 7b42ff3eb..5bf31cdcb 100644 --- a/Server/ConfigReader.js +++ b/Server/ConfigReader.js @@ -91,7 +91,7 @@ */ - storage.getData('AdvancedAlgos', 'AAPlatform', 'ecosystem.json', true, onDataArrived); + storage.readData('AdvancedAlgos', 'AAPlatform', 'ecosystem.json', true, onDataArrived); function onDataArrived(pData) { @@ -145,7 +145,7 @@ requestsSent++; - storage.getData(host.codeName + "/" + "competitions", competition.repo, competition.configFile, true, onDataArrived); + storage.readData(host.codeName + "/" + "competitions", competition.repo, competition.configFile, true, onDataArrived); function onDataArrived(pData) { @@ -202,7 +202,7 @@ requestsSent++; - storage.getData(host.codeName + "/" + "plotters", plotter.repo, plotter.configFile, true, onDataArrived); + storage.readData(host.codeName + "/" + "plotters", plotter.repo, plotter.configFile, true, onDataArrived); function onDataArrived(pData) { @@ -291,7 +291,7 @@ requestsSent++; - storage.getData(devTeam.codeName + "/" + "bots", bot.repo, bot.configFile, true, onDataArrived); + storage.readData(devTeam.codeName + "/" + "bots", bot.repo, bot.configFile, true, onDataArrived); function onDataArrived(pData) { @@ -354,7 +354,7 @@ requestsSent++; - storage.getData(devTeam.codeName + "/" + "plotters", plotter.repo, plotter.configFile, true, onDataArrived); + storage.readData(devTeam.codeName + "/" + "plotters", plotter.repo, plotter.configFile, true, onDataArrived); function onDataArrived(pData) { diff --git a/Server/SessionManager.js b/Server/SessionManager.js index abee25522..b41a17425 100644 --- a/Server/SessionManager.js +++ b/Server/SessionManager.js @@ -21,7 +21,7 @@ let storage = STORAGE.newStorage(); storage.initialize(undefined, pServerConfig); - storage.getData("AdvancedAlgos", "AAPlatform", "Open.Sessions.json", false, onData); + storage.readData("AdvancedAlgos", "AAPlatform", "Open.Sessions.json", false, onData); function onData(pText) { diff --git a/Server/Storage.js b/Server/Storage.js index 6a9b995cc..1f8164b14 100644 --- a/Server/Storage.js +++ b/Server/Storage.js @@ -1,7 +1,8 @@ exports.newStorage = function newStorage() { let thisObject = { - getData: getData, + readData: readData, + writeData: writeData, initialize: initialize } @@ -17,14 +18,14 @@ } - function getData(pOrg, pRepo, pPath, saveAtCache, callBackFunction) { + function readData(pOrg, pRepo, pPath, saveAtCache, callBackFunction) { try { - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> getData -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> getData -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> getData -> pRepo = " + pRepo); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> getData -> pPath = " + pPath); } + 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 -> pRepo = " + pRepo); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> pPath = " + pPath); } let cacheVersion; @@ -35,13 +36,13 @@ if (cacheVersion !== undefined) { - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> getData -> " + pOrg + '.' + pRepo + '.' + pPath + " found at cache."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> " + pOrg + '.' + pRepo + '.' + pPath + " found at cache."); } callBackFunction(cacheVersion); } else { - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> getData -> " + pOrg + '.' + pRepo + '.' + pPath + " NOT found at cache."); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> " + pOrg + '.' + pRepo + '.' + pPath + " NOT found at cache."); } let storage = require('azure-storage'); let connectionString; @@ -70,12 +71,12 @@ try { - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> getData -> onFileReceived -> Entering function."); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> getData -> onFileReceived -> err = " + JSON.stringify(err)); } - if (LOG_FILE_CONTENT === true) { console.log("[INFO] Storage -> getData -> onFileReceived -> response = " + JSON.stringify(response)); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> getData -> onFileReceived -> pOrg = " + pOrg); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> getData -> onFileReceived -> pRepo = " + pRepo); } - if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> getData -> onFileReceived -> pPath = " + pPath); } + 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 -> pRepo = " + pRepo); } + if (CONSOLE_LOG === true) { console.log("[INFO] Storage -> readData -> onFileReceived -> pPath = " + pPath); } if (saveAtCache === true) { @@ -85,9 +86,9 @@ if (err !== null || text === null) { - if (CONSOLE_LOG === true) { console.log("[ERROR] Storage -> getData -> onFileReceived -> Error Received from Storage Library. "); } - if (CONSOLE_LOG === true) { console.log("[ERROR] Storage -> getData -> onFileReceived -> err = " + JSON.stringify(err)); } - if (CONSOLE_LOG === true) { console.log("[ERROR] Storage -> getData -> onFileReceived -> Returning an empty JSON object string. "); } + if (CONSOLE_LOG === true) { console.log("[ERROR] Storage -> readData -> onFileReceived -> Error Received from Storage Library. "); } + if (CONSOLE_LOG === true) { console.log("[ERROR] Storage -> readData -> onFileReceived -> err = " + JSON.stringify(err)); } + if (CONSOLE_LOG === true) { console.log("[ERROR] Storage -> readData -> onFileReceived -> Returning an empty JSON object string. "); } callBackFunction(""); return; @@ -97,15 +98,86 @@ callBackFunction(text); } catch (err) { - console.log("[ERROR] Storage -> getData -> onFileReceived -> err.message = " + err.message); + console.log("[ERROR] Storage -> readData -> onFileReceived -> err.message = " + err.message); callBackFunction("{}"); } } } } catch (err) { - console.log("[ERROR] Storage -> getData -> err.message = " + err.message); + console.log("[ERROR] Storage -> readData -> err.message = " + err.message); callBackFunction("{}"); } } + + function writeData(pOrg, pRepo, pPath, pFileContent, callBackFunction) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> pOrg = " + pOrg); } + if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> pRepo = " + pRepo); } + if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> pPath = " + pPath); } + + if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> " + pOrg + '.' + pRepo + '.' + pPath + " 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); + let blobPath = pOrg + "/" + pRepo + "/" + pPath; + let blobText = pFileContent.toString(); + + blobService.createBlockBlobFromText('aaplatform', blobPath, blobText, onFileCreated); + + function onFileCreated(err, text, response) { + + try { + + if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> Entering function."); } + if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> err = " + JSON.stringify(err)); } + if (LOG_FILE_CONTENT === true) { console.log("[INFO] API -> writeData -> onFileCreated -> response = " + JSON.stringify(response)); } + if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> pOrg = " + pOrg); } + if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> pRepo = " + pRepo); } + if (CONSOLE_LOG === true) { console.log("[INFO] API -> writeData -> onFileCreated -> pPath = " + pPath); } + + if (err !== null || text === null) { + + if (CONSOLE_LOG === true) { console.log("[ERROR] API -> writeData -> onFileCreated -> Error Received from API Library. "); } + if (CONSOLE_LOG === true) { console.log("[ERROR] API -> writeData -> onFileCreated -> err = " + JSON.stringify(err)); } + if (CONSOLE_LOG === true) { console.log("[ERROR] API -> writeData -> onFileCreated -> Returning an empty JSON object string. "); } + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + + } + + callBackFunction(global.DEFAULT_OK_RESPONSE); + + } catch (err) { + console.log("[ERROR] API -> writeData -> onFileCreated -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } + + } catch (err) { + console.log("[ERROR] API -> writeData -> err.message = " + err.message); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } } \ No newline at end of file diff --git a/server.js b/server.js index 0956a7984..6bc1840d2 100644 --- a/server.js +++ b/server.js @@ -800,7 +800,7 @@ function onBrowserRequest(request, response) { case "Plotters": // This means the plotter folder, not to be confused with the Plotters script! { - storage.getData(requestParameters[2] + "/" + "plotters", requestParameters[3], requestParameters[4], true, onDataArrived); + storage.readData(requestParameters[2] + "/" + "plotters", requestParameters[3], requestParameters[4], true, onDataArrived); function onDataArrived(pData) { @@ -812,7 +812,7 @@ function onBrowserRequest(request, response) { case "PlotterPanels": // This means the PlotterPanels folder, not to be confused with the Plotter Panels scripts! { - storage.getData(requestParameters[2] + "/" + "plotters", requestParameters[3], requestParameters[4], true, onDataArrived); + storage.readData(requestParameters[2] + "/" + "plotters", requestParameters[3], requestParameters[4], true, onDataArrived); function onDataArrived(pData) {