UNPKG

node-red-contrib-opentext

Version:

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

285 lines (230 loc) 7.94 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.... console.log("init "); let request = require("request"); var querystring = require("querystring"); // The main node definition - most things happen in here function UploadNode(uploadNode) { // Create a RED node RED.nodes.createNode(this, uploadNode); // Store local copies of the node configuration (as defined in the .html) this.name = uploadNode.name || ""; this.base_url = uploadNode.base_url || ""; this.upload_type = uploadNode.upload_type || ""; this.accesstoken = uploadNode.access_token || ""; this.file_name = uploadNode.file_name || ""; this.file_content = uploadNode.file_content || ""; this.file_url = uploadNode.file_url || ""; // 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 console.log("input "); node.status({ fill: "yellow", shape: "dot", text: "Uploading....", }); // TODO - ??? =) let Method = "Post"; let Authorization = ""; //let url = ""; let Document = ""; let Body = ""; let Headers =""; let Options = ""; //let Form = {}; let Databuffer =""; console.log("input end"); console.log("type"); console.log(node.upload_type); if (msg.coreSignatureUploadRequest.base_url){ node.base_url = msg.coreSignatureUploadRequest.base_url; } if (msg.coreSignatureUploadRequest.type){ node.upload_type = msg.coreSignatureUploadRequest.upload_type; } if (msg.coreSignatureUploadRequest.file){ // content from input node.file_name = msg.coreSignatureUploadRequest.file.file_name; node.file_content = msg.coreSignatureUploadRequest.file.file_content; node.file_url = msg.coreSignatureUploadRequest.file.file_url; } console.log("coreSignatureUploadRequest "); //set token node.accesstoken = msg.payload.access_token; Authorization = "Bearer " + node.accesstoken; Databuffer = node.file_content; let upload_url = node.base_url + "/api/v1/documents/"; if (node.upload_type == "capture") { upload_url = node.base_url + "/cp-rest/session/files"; console.log(node.upload_type); Body =JSON.stringify({ data: Databuffer, contentType: 'image:tiff' }); console.log("cap body ready "); console.log(Body); Headers = { 'Content-Type': 'application/json', "Content-Length": Buffer.byteLength(Body), Authorization: Authorization, }; console.log("Cap Headers ready "); console.log("Headers ready "); console.log(Headers); console.log("URL"); console.log(upload_url); Options = { method: Method, url: upload_url, headers: Headers , body: Body , }; console.log("options upload ok"); } else{ let upload_url = node.base_url + "/api/v1/documents/"; //upload Document--------------------------------------------------------------------------- //set upload url node.status({ fill: "yellow", shape: "ring", text: "Uploading document!!", }); //body if (node.file_url) { Body =JSON.stringify({ "file_from_url": node.file_url, //"file_from_content_name": node.file_name, "email": "hvantil+demo@opentext.com", "auto_delete_days": 1, "auto_expire_days": 1, "link_expire_days": 1 }); } else{ Body =JSON.stringify({ "file_from_content": Databuffer, "file_from_content_name": node.file_name, "email": "hvantil+demo@opentext.com", "auto_delete_days": 1, "auto_expire_days": 1, "link_expire_days": 1 }); } console.log("Body ready "); // console.log(Body); //headers Headers = { // 'Accept': 'application/json', "Content-Type": "application/json", "Content-Length": Buffer.byteLength(Body), Authorization: Authorization, }; console.log("Headers ready "); console.log(Headers); console.log("URL"); console.log(upload_url); // set Options Options = { method: Method, url: upload_url, headers: Headers, body: Body, json: false, }; console.log("options upload ok"); } delete msg.file_content; // make a post request request(Options, function (err, response, body) { console.log("request"); if (msg.coreSignatureRequest) delete msg.coreSignatureRequest; try { let SignBody = JSON.parse(body ? body : JSON.stringify("{}")); if (response && response.statusCode < 299 && response.statusCode > 199) { console.log(response.statusMessage); console.log(response.body); msg.payload = SignBody; msg.payload.access_token = node.accesstoken; msg.payload.statusCode = response.statusCode; node.status({ fill: "green", shape: "dot", text: `HTTP ${response.statusCode}, Document uploaded!`, }); } else if ( response && response.statusCode && response.statusCode !== 200 ) { console.log(response.statusMessage); console.log(response.body); msg.payload = { statusCode: response.statusCode, statusMessage: response.statusMessage, body: SignBody, }; node.status({ fill: "red", shape: "dot", text: `HTTP ${response.statusCode}, error uploading 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}`, }); } Document = msg.payload.url; 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("upload", UploadNode); };