From f8f9592caeb8a7d3b5162dfb84712b4d8da404d9 Mon Sep 17 00:00:00 2001 From: strawmanbobi Date: Sun, 4 Dec 2016 16:37:06 +0800 Subject: [PATCH] [undone] removed Ali-OSS related logic from irext console - 2 --- .../configuration/system_configs.js | 6 ++ src/web_console/mini_poem/http/request.js | 26 ++++- src/web_console/package.json | 7 ++ src/web_console/script_npm.sh | 3 + src/web_console/work_unit/internal_logic.js | 101 +++++++++--------- 5 files changed, 90 insertions(+), 53 deletions(-) create mode 100644 src/web_console/package.json diff --git a/src/web_console/configuration/system_configs.js b/src/web_console/configuration/system_configs.js index e73d858..72dd1ab 100644 --- a/src/web_console/configuration/system_configs.js +++ b/src/web_console/configuration/system_configs.js @@ -23,6 +23,8 @@ exports.setupEnvironment = function () { REDIS_HOST = "localhost"; REDIS_PORT = "6379"; REDIS_PASSWORD = ""; + PRIMARY_SERVER_ADDRESS = "irext.net" + PRIMARY_SERVER_PORT = "80" } else if (enums.APP_DEVELOPMENT_MODE == env) { MYSQL_DB_SERVER_ADDRESS = "localhost"; MYSQL_DB_NAME = "irext"; @@ -36,6 +38,8 @@ exports.setupEnvironment = function () { REDIS_HOST = "localhost"; REDIS_PORT = "6379"; REDIS_PASSWORD = ""; + PRIMARY_SERVER_ADDRESS = "irext.net" + PRIMARY_SERVER_PORT = "80" } else if (enums.APP_USERDEBUG_MODE == env) { MYSQL_DB_SERVER_ADDRESS = "localhost"; MYSQL_DB_NAME = "irext"; @@ -49,5 +53,7 @@ exports.setupEnvironment = function () { REDIS_HOST = "localhost"; REDIS_PORT = "6379"; REDIS_PASSWORD = ""; + PRIMARY_SERVER_ADDRESS = "irext.net" + PRIMARY_SERVER_PORT = "80" } }; \ No newline at end of file diff --git a/src/web_console/mini_poem/http/request.js b/src/web_console/mini_poem/http/request.js index baab361..94bdbd6 100644 --- a/src/web_console/mini_poem/http/request.js +++ b/src/web_console/mini_poem/http/request.js @@ -8,7 +8,6 @@ var queryString = require('querystring'); var http = require('http'); var request = require('request'); - // local inclusion var Map = require('../mem/map.js'); var ErrorCode = require('../configuration/error_code.js'); @@ -62,8 +61,9 @@ Request.prototype.sendGetRequest = function(options, callback) { if(options.https) { request( - { method: 'GET' - , uri: url + { + method: 'GET', + uri: url }, function (error, response, body) { if(!error && response.statusCode == '200') { callback(errorCode.SUCCESS, JSON.parse(body)); @@ -129,4 +129,24 @@ Request.prototype.sendPostRequest = function(bodyData, callback) { } }; +// post simple file to HTTP server +Request.prototype.postSimpleFile = function(fileName, fileContent, contentType, options, callback) { + var httpTag = options.https ? "https://" : "http://"; + var url = httpTag + this.host + ":" + this.port + this.service + + this.urlizeQueryParams(); + + var req = request.post(url, function (err, resp, body) { + if (err) { + callback(errorCode.FAILED, resp); + } else { + callback(errorCode.SUCCESS, resp); + } + }); + var form = req.form(); + form.append('file', fileContent, { + filename: fileName, + contentType: contentType + }); +}; + module.exports = Request; diff --git a/src/web_console/package.json b/src/web_console/package.json new file mode 100644 index 0000000..19ff150 --- /dev/null +++ b/src/web_console/package.json @@ -0,0 +1,7 @@ +{ + "name": "application-name", + "version": "0.0.1", + "dependencies": { + "orm": "latest" + } +} diff --git a/src/web_console/script_npm.sh b/src/web_console/script_npm.sh index fb200f7..8d4c5fd 100755 --- a/src/web_console/script_npm.sh +++ b/src/web_console/script_npm.sh @@ -32,6 +32,9 @@ npm install python-shell echo "npm install request" npm install request +echo "npm install form-data" +npm install form-data + echo "npm install async" npm install async diff --git a/src/web_console/work_unit/internal_logic.js b/src/web_console/work_unit/internal_logic.js index b7a85a6..2bac233 100644 --- a/src/web_console/work_unit/internal_logic.js +++ b/src/web_console/work_unit/internal_logic.js @@ -8,8 +8,8 @@ fs = require('fs'); var crypto = require('crypto'); // global inclusion +require('../mini_poem/configuration/constants'); var orm = require('orm'); -var Constants = require('../mini_poem/configuration/constants'); var PythonCaller = require('../mini_poem/external/python_caller'); var Category = require('../model/category_dao.js'); @@ -35,11 +35,6 @@ var async = require('async'); // relative XML file path var PROTOCOL_PATH = "protocol"; -var DEBUG_BUCKET_NAME = "irext-debug"; -var USER_DEBUG_BUCKET_NAME = "irext-userdebug"; -var RELEASE_BUCKET_NAME = "irext-release"; -var PROTOCOL_BUCKET_NAME = "irext-protocol"; - // out going HTTP request parameters // var PRIMARY_SERVER_ADDRESS = "irext.net"; var PRIMARY_SERVER_ADDRESS = "127.0.0.1"; @@ -49,6 +44,7 @@ var REQUEST_APP_KEY = "d6119900556c4c1e629fd92d"; var REQUEST_APP_TOKEN = "fcac5496cba7a12b3bae34abf061f526"; var PUBLISH_BRAND_SERVICE = "/irext/remote/publish_brands"; +var UPLOAD_BINARY_SERVICE = "/irext/remote/upload_binary"; var PUBLISH_REMOTE_INDEX_SERVICE = "/irext/remote/publish_remote_indexes"; var DELETE_REMOTE_INDEX_SERVICE = "/irext/remote/delete_remote_index"; @@ -212,7 +208,6 @@ exports.createRemoteIndexWorkUnit = function(remoteIndex, filePath, contentType, var protocolPath; var outputPath; var outputFilePath; - var outputFileName; var newRemoteIndex; var newACRemoteNumber; var tagType; @@ -272,7 +267,7 @@ exports.createRemoteIndexWorkUnit = function(remoteIndex, filePath, contentType, userArgs.push(remoteXMLFilePath); // set the xml directory as output path userArgs.push(fileDir + "/"); - var pythonCaller = new PythonCaller(); + pythonCaller = new PythonCaller(); try { pythonCaller.call(pythonRuntimeDir, pythonFile, userArgs, function(remoteGenErr, genResult) { if (errorCode.SUCCESS.code == remoteGenErr) { @@ -656,13 +651,12 @@ exports.publishRemoteIndexWorkUnit = function (callback) { async.eachSeries(remoteIndexes, function (remoteIndex, innerCallback) { var remoteName = remoteIndex.remote; var protocolName = remoteIndex.protocol; - var binFileName = fileDir + "/irda_" + protocolName + "_" + remoteName + ".bin"; + var binFileName = "irda_" + protocolName + "_" + remoteName + ".bin"; logger.info("binary file name = " + binFileName); + // step 2, copy file to transfer path - sourceFileName = FILE_TEMP_PATH + "/irda_" + protocolName + "_" + - remoteName + ".bin"; - targetFileName = FILE_TEMP_PATH + "/binary_transfer" + "/irda_" + protocolName + "_" + - remoteName + ".bin"; + sourceFileName = fileDir + "/" + binFileName; + targetFileName = fileDir + "/binary_transfer/" + binFileName; var readStream = fs.createReadStream(sourceFileName); var writeStream = fs.createWriteStream(targetFileName); readStream.pipe(writeStream); @@ -673,8 +667,28 @@ exports.publishRemoteIndexWorkUnit = function (callback) { innerCallback(); } else { logger.info("read remote binary file successfully, file size = " + fileData.length); - // TODO: send HTTP post request to release server + var queryParams = new Map(); + queryParams.put("app_key", REQUEST_APP_KEY); + queryParams.put("app_token", REQUEST_APP_TOKEN); + var requestSender = + new RequestSender(PRIMARY_SERVER_ADDRESS, + PRIMARY_SERVER_PORT, + UPLOAD_BINARY_SERVICE, + queryParams); + var options = { + https: false + }; + requestSender.postSimpleFile(binFileName, fileData, 'application/octet-stream', + options, function(errorCode, response) { + if (errorCode.SUCCESS.code == errorCode.code) { + logger.info("upload file successfully to primary server"); + uploadedRIIds.push(remoteIndex.id); + } else { + logger.info("upload file failed to primary server"); + } + innerCallback(); + }); } }); }, function(err) { @@ -696,7 +710,6 @@ exports.publishRemoteIndexWorkUnit = function (callback) { if (errorCode.SUCCESS.code == findRemoteIndexesErr.code && undefined != remoteIndexes && null != remoteIndexes && remoteIndexes.length > 0) { // send out going HTTP request to primary server - var queryParams = new Map(); queryParams.put("app_key", REQUEST_APP_KEY); queryParams.put("app_token", REQUEST_APP_TOKEN); @@ -872,43 +885,31 @@ exports.createProtocolWorkUnit = function(protocol, filePath, contentType, admin callback(errorCode.FAILED); } else { logger.info("read protocol binary successfully, file size = " + fileData.length); + ////////////////////////////////////// + // step 4, try register protocol to db + var newProtocol = { + name: protocolName, + status: enums.ITEM_VALID, + type: protocolType + }; - var aliOss = new OSS(OSS_HOST, PROTOCOL_BUCKET_NAME, OSS_APP_ID, OSS_APP_SECRET); - remoteProtocolFile = protocolName + ".bin"; - aliOss.saveObjectFromBinary(remoteProtocolFile, fileData, - "application/octet-stream", - function (createObjectErr, objectID) { - if (errorCode.SUCCESS.code == createObjectErr) { - ////////////////////////////////////// - // step 4, try register protocol to db - var newProtocol = { - name: protocolName, - status: enums.ITEM_VALID, - type: protocolType - }; + var conditions = { + name: protocolName + }; - var conditions = { - name: protocolName - }; - - logger.info("irda_tv_protocol.py called successfully, create protocol in DB"); - IRProtocol.findIRProtocolByConditions(conditions, function(findIRProtocolErr, IRProtocols) { - if(errorCode.SUCCESS.code == findIRProtocolErr.code && - null != IRProtocols && - IRProtocols.length > 0) { - logger.info("protocol " + protocolName + " already exists, nothing to be updated"); - callback(errorCode.SUCCESS); - } else { - IRProtocol.createIRProtocol(newProtocol, function(createIRProtocolErr, createdIRProtocol) { - callback(createIRProtocolErr); - }); - } - }); - } else { - logger.error("upload protocol binary file failed"); - callback(errorCode.FAILED); - } - }); + logger.info("irda_tv_protocol.py called successfully, create protocol in DB"); + IRProtocol.findIRProtocolByConditions(conditions, function(findIRProtocolErr, IRProtocols) { + if(errorCode.SUCCESS.code == findIRProtocolErr.code && + null != IRProtocols && + IRProtocols.length > 0) { + logger.info("protocol " + protocolName + " already exists, nothing to be updated"); + callback(errorCode.SUCCESS); + } else { + IRProtocol.createIRProtocol(newProtocol, function(createIRProtocolErr, createdIRProtocol) { + callback(createIRProtocolErr); + }); + } + }); } }); } else {