UNPKG

node-red-contrib-opentext

Version:

node-red-contrib-opentext - An Opentext Core client

227 lines (183 loc) 6.82 kB
/** * MIT License * * Copyright (c) 2019 Marcos Caputo <caputo.marcos@gmail.com> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software.. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * **/ module.exports = function (RED) { "use strict"; // require any external libraries we may need.... let request = require("request"); var querystring = require("querystring"); const fs = require('fs'); // The main node definition - most things happen in here function DownloadNode(downloadNode) { // Create a RED node RED.nodes.createNode(this, downloadNode); //console.log("DOWNLOADDOWNLOADDOWNLOADDOWNLOADDOWNLOADDOWNLOAD"); // Store local copies of the node configuration (as defined in the .html) this.name = downloadNode.name || ""; this.base_url = downloadNode.base_url || ""; this.file_name = downloadNode.file_name || ""; this.value = downloadNode.value || ""; this.contenttype = downloadNode.contenttype || ""; // copy "this" object in case we need it in context of callbacks of other functions. let node = this; let msg = {}; //msg.payload = this.payload; // respond to inputs.... this.on("input", function (msg) { // set an empty form node.status({ fill: "yellow", shape: "dot", text: "downloading....", }); // TODO - ??? =) let Method = "GET"; let Authorization = ""; let Headers =""; let Options = ""; let download_url = ""; let payload = ""; if (msg.coreDownloadRequest){ if (msg.coreDownloadRequest.url) node.base_url = msg.coreDownloadRequest.base_url; if (msg.coreDownloadRequest.file.file_name) node.file_name = msg.coreDownloadRequest.file.file_name; if (msg.coreDownloadRequest.file.value) node.value = msg.coreDownloadRequest.file.value; if (msg.coreDownloadRequest.file.type) node.contenttype = msg.coreDownloadRequest.file.type; // get accesstoken & content from input } if(!node.file_name) { node.file_name = msg.payload.resultItems[0].files[0].name; } //name = msg.payload.resultItems[0].files[0].name; //type = msg.payload.resultItems[0].files[0].fileType; //set token node.accesstoken = msg.payload.access_token ; Authorization = "Bearer " + node.accesstoken; if (node.value){ // payload.resultItems[0].files[0].value// console.log("NODE VALUEDL1"); download_url = node.base_url + "/cp-rest/session/files/" + node.value; } else { console.log("NODE VALUEDL2"); download_url = msg.payload.resultItems[0].files[0].src; } console.log( download_url); if (!node.contenttype){ // payload.resultItems[0].files[0].value// node.contenttype = msg.payload.resultItems[0].files[0].fileType; console.log("NODE VALUECT2"); } console.log( node.contenttype); console.log("url"); console.log(download_url); //Download------------------------------------------------------------------------------------------------------ //clear // Headers =""; Options = ""; //Body =JSON.stringify({ //}); console.log("before JSON"); //set Headers Headers = { 'Content-Type': 'application/json', Authorization: Authorization }; console.log("after JSON"); node.warn("Headers "); node.warn(Headers); // set Option Options = { method: Method , url: download_url, headers: Headers }; console.log("options ok"); // make a post request request(Options, function (err, response, body) { if (msg.coreDownloadRequest) delete msg.coreDownloadRequest; try { console.log(response.statusCode); let payload = response.body; if (response && response.statusCode < 299 && response.statusCode > 199) { // msg.payload = DownloadBody; msg = {payload, statusCode: response.statusCode, statusMessage: response.statusMessage, filename : node.file_name+"."+node.contenttype, }; //msg.payload.filename = node.file_name; node.status({ fill: "green", shape: "dot", text: `HTTP ${response.statusCode}, Document has been downloaded!!`, }); } else if ( response && response.statusCode && response.statusCode !== 200 ) { console.log(response.statusMessage); console.log(response.body); msg.filetype = node.contenttype; msg.payload = { statusCode: response.statusCode, statusMessage: response.statusMessage, body: DownloadBody, }; node.status({ fill: "red", shape: "dot", text: `HTTP ${response.statusCode}, error downloading document!`, }); } if (err && err.code) { msg.err = JSON.parse(JSON.stringify(err)); node.status({ fill: "yellow", shape: "dot", text: `ERR ${err.code}` }); } else if (err && err.message && err.stack) { msg.err = { message: err.message, stack: err.stack }; node.status({ fill: "black", shape: "dot", text: `ERR ${err.message}`, }); } //fs.writeFileSync('/tmp/test.txt', msg.payload); node.warn(msg); node.send(msg); } catch (err) { var d = new Date(); var n = d.toISOString(); console.log(n + ": " + err.message); //console.log(n + ": " + body.replace(/ |\r\n|\n|\r/gm, "")); msg.err = JSON.parse(JSON.stringify(err)); node.status({ fill: "blue", shape: "dot", text: `ERR ${err.code}` }); } }); }); } RED.nodes.registerType("download", DownloadNode); };