Skip to content

Commit f95d938

Browse files
committed
Add simple dropbox upload node.
1 parent 0fc7e13 commit f95d938

File tree

4 files changed

+224
-1
lines changed

4 files changed

+224
-1
lines changed

dropbox/dropbox.html

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
<script type="text/x-red" data-template-name="dropbox">
18+
<div class="form-row">
19+
<label for="node-config-input-appkey"><i class="fa fa-bookmark"></i>App Key</label>
20+
<input class="input-append-left" type="text" id="node-config-input-appkey" style="width: 40%;" >
21+
</div>
22+
<div class="form-row">
23+
<label for="node-config-input-appsecret"><i class="fa fa-bookmark"></i>App Secret</label>
24+
<input class="input-append-left" type="password" id="node-config-input-appsecret" style="width: 40%;" >
25+
</div>
26+
<div class="form-row">
27+
<label for="node-config-input-accesstoken"><i class="fa fa-bookmark"></i>Access Token</label>
28+
<input class="input-append-left" type="password" id="node-config-input-accesstoken" style="width: 40%;" >
29+
</div>
30+
<div class="form-tips">
31+
To obtain these credentials, visit the <a
32+
href="https://www.dropbox.com/developers">Dropbox developer home</a>.
33+
Once signed up:
34+
<ol>
35+
<li>click "Create app",</li>
36+
<li>select "Dropbox API app",</li>
37+
<li>select "Files and datastores",</li>
38+
<li>choose an appropriate answer as to whether your node should be
39+
limited to its own folder or given full access. (This can be
40+
changed later so choose "Yes" to restrict the application
41+
while testing might be a good idea.)</li>
42+
<li>choose an app name</li>
43+
<li>click "Create app".</li>
44+
</ol>
45+
The subsequent app page will contain the App key, App secret and a
46+
"Generate" button to produce a suitable "access token".
47+
</div>
48+
</script>
49+
50+
<script type="text/javascript">
51+
(function() {
52+
RED.nodes.registerType('dropbox',{
53+
category: 'config',
54+
defaults: { },
55+
credentials: {
56+
appkey: {type:"text",required:true},
57+
appsecret: {type: "password",required:true},
58+
accesstoken: {type: "password",required:true},
59+
},
60+
label: function() {
61+
return 'Dropbox'; // TODO: get display_name from account info?
62+
},
63+
exportable: false,
64+
});
65+
})();
66+
</script>
67+
68+
<script type="text/x-red" data-template-name="dropbox out">
69+
<div class="form-row">
70+
<label for="node-input-dropbox"><i class="fa fa-user"></i> Dropbox</label>
71+
<input type="text" id="node-input-dropbox">
72+
</div>
73+
<div class="form-row node-input-filename">
74+
<label for="node-input-filename"><i class="fa fa-file"></i> Filename</label>
75+
<input type="text" id="node-input-filename" placeholder="Filename">
76+
</div>
77+
<div class="form-row node-input-localFilename">
78+
<label for="node-input-localFilename"><i class="fa fa-file"></i> Local Filename</label>
79+
<input type="text" id="node-input-localFilename" placeholder="Local Filename">
80+
</div>
81+
<div class="form-row">
82+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
83+
<input type="text" id="node-input-name" placeholder="Name">
84+
</div>
85+
</script>
86+
87+
<script type="text/x-red" data-help-name="dropbox out">
88+
<p>Dropbox out node. Uploads content to Dropbox. The filename on Dropbox is taken from the node <b>filename</b> property which may be overriden by the <b>msg.filename</b> property. The content is taken from either the node <b>localFilename</b> (which may be overriden by the <b>msg.localFilename</b> property) or the <b>msg.payload</b> property.</p>
89+
<p>If a <b>msg.delete</b> property exists then the file will be removed from Dropbox instead.</p>
90+
</script>
91+
92+
<script type="text/javascript">
93+
RED.nodes.registerType('dropbox out',{
94+
category: 'storage-output',
95+
color:"#C0DEED",
96+
defaults: {
97+
dropbox: {type:"dropbox",required:true},
98+
filename: {value:""},
99+
localFilename: {value:""},
100+
name: {value:"Dropbox"}
101+
},
102+
inputs:1,
103+
outputs:0,
104+
icon: "dropbox.png",
105+
align: "right",
106+
label: function() {
107+
return this.name||this.filename||this.localFilename||'Dropbox';
108+
}
109+
});
110+
</script>

dropbox/dropbox.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
};

dropbox/icons/dropbox.png

482 Bytes
Loading

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version" : "0.0.1",
44
"description" : "A collection of Node-RED nodes aimed at web services.",
55
"dependencies" : {
6+
"dropbox":"^0.10.3",
67
"oauth":"~0.9.11",
78
"request":"~2.40.0"
89
},
@@ -15,7 +16,8 @@
1516
"node-red" : {
1617
"nodes" : {
1718
"pinboard": "pinboard/pinboard.js",
18-
"flickr": "flickr/flickr.js"
19+
"flickr": "flickr/flickr.js",
20+
"dropbox": "dropbox/dropbox.js"
1921
}
2022
}
2123
}

0 commit comments

Comments
 (0)