|
| 1 | +/** |
| 2 | + * Copyright 2014 IBM Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | + |
| 17 | +module.exports = function(RED) { |
| 18 | + "use strict"; |
| 19 | + var Dropbox = require("dropbox"); |
| 20 | + var fs = require("fs"); |
| 21 | + |
| 22 | + function DropboxNode(n) { |
| 23 | + RED.nodes.createNode(this,n); |
| 24 | + } |
| 25 | + RED.nodes.registerType("dropbox",DropboxNode,{ |
| 26 | + credentials: { |
| 27 | + appkey: { type:"text" }, |
| 28 | + appsecret: { type: "password" }, |
| 29 | + accesstoken: { type:"password" }, |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + function DropboxOutNode(n) { |
| 34 | + RED.nodes.createNode(this,n); |
| 35 | + this.filename = n.filename || ""; |
| 36 | + this.localFilename = n.localFilename || ""; |
| 37 | + this.dropboxConfig = RED.nodes.getNode(n.dropbox); |
| 38 | + var credentials = RED.nodes.getCredentials(n.dropbox); |
| 39 | + var node = this; |
| 40 | + if (credentials && credentials.appkey && credentials.appsecret && |
| 41 | + credentials.accesstoken) { |
| 42 | + var dropbox = new Dropbox.Client({ |
| 43 | + //uid: credentials.uid, |
| 44 | + key: credentials.appkey, |
| 45 | + secret: credentials.appsecret, |
| 46 | + token: credentials.accesstoken, |
| 47 | + }); |
| 48 | + node.status({fill:"blue",shape:"dot",text:"checking credentials"}); |
| 49 | + dropbox.getAccountInfo(function (err) { |
| 50 | + if (err) { |
| 51 | + node.error("Error verifying credentials: " + err); |
| 52 | + node.status({fill:"red",shape:"ring",text:"access denied"}); |
| 53 | + return; |
| 54 | + } |
| 55 | + node.status({}); |
| 56 | + node.on("input", function(msg) { |
| 57 | + var filename = msg.filename || this.filename; |
| 58 | + if (filename === "") { |
| 59 | + node.warn("No filename specified"); |
| 60 | + return; |
| 61 | + } |
| 62 | + if (msg.hasOwnProperty("delete")) { |
| 63 | + node.status({fill:"blue",shape:"dot",text:"deleting"}); |
| 64 | + dropbox.remove(filename, function(err) { |
| 65 | + if (err) { |
| 66 | + node.error(err.toString()); |
| 67 | + node.status({fill:"red",shape:"ring",text:"failed"}); |
| 68 | + return; |
| 69 | + } |
| 70 | + node.status({}); |
| 71 | + }); |
| 72 | + return; |
| 73 | + } |
| 74 | + var localFilename = msg.localFilename || this.localFilename; |
| 75 | + if (localFilename) { |
| 76 | + // TODO: use chunked upload for files larger than 150M |
| 77 | + node.status({fill:"blue",shape:"dot",text:"uploading"}); |
| 78 | + fs.readFile(localFilename, function read(err, data) { |
| 79 | + if (err) { |
| 80 | + node.error(err.toString()); |
| 81 | + node.status({fill:"red",shape:"ring",text:"failed"}); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + dropbox.writeFile(filename, data, function(err) { |
| 86 | + if (err) { |
| 87 | + node.error(err.toString()); |
| 88 | + node.status({fill:"red",shape:"ring",text:"failed"}); |
| 89 | + return; |
| 90 | + } |
| 91 | + node.status({}); |
| 92 | + }); |
| 93 | + }); |
| 94 | + } else if (typeof msg.payload !== "undefined") { |
| 95 | + var data = RED.util.ensureBuffer(msg.payload); |
| 96 | + node.status({fill:"blue",shape:"dot",text:"uploading"}); |
| 97 | + dropbox.writeFile(filename, data, function(err) { |
| 98 | + if (err) { |
| 99 | + node.error(err.toString()); |
| 100 | + node.status({fill:"red",shape:"ring",text:"failed"}); |
| 101 | + return; |
| 102 | + } |
| 103 | + node.status({}); |
| 104 | + }); |
| 105 | + } |
| 106 | + }); |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | + RED.nodes.registerType("dropbox out",DropboxOutNode); |
| 111 | +}; |
0 commit comments