Skip to content

Commit a6032cb

Browse files
committed
Add dropbox watch node.
1 parent fc34ce8 commit a6032cb

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

dropbox/dropbox.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,49 @@
6565
})();
6666
</script>
6767

68+
<script type="text/x-red" data-template-name="dropbox in">
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-filepattern">
74+
<label for="node-input-filepattern"><i class="fa fa-file"></i> Filename Pattern</label>
75+
<input type="text" id="node-input-filepattern" placeholder="Filepattern">
76+
</div>
77+
<div class="form-row">
78+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
79+
<input type="text" id="node-input-name" placeholder="Name">
80+
</div>
81+
</script>
82+
83+
<script type="text/x-red" data-help-name="dropbox in">
84+
<p>Dropbox watch node. Watches for file events on Dropbox. By default all
85+
file events are reported, but the filename pattern can be supplied
86+
to limit the events to files which have full filenames that match
87+
the glob pattern. The event messages consist of the full filename
88+
in <b>msg.payload</b> property, the filename in <b>msg.file</b>,
89+
the event type in <b>msg.event</b> and the full change object
90+
in <b>msg.data</b>.</p>
91+
</script>
92+
93+
<script type="text/javascript">
94+
RED.nodes.registerType('dropbox in',{
95+
category: 'storage-input',
96+
color:"#C0DEED",
97+
defaults: {
98+
dropbox: {type:"dropbox-config",required:true},
99+
filepattern: {value:""},
100+
name: {value:""}
101+
},
102+
inputs: 0,
103+
outputs: 1,
104+
icon: "dropbox.png",
105+
label: function() {
106+
return this.name||this.filepattern||'Dropbox';
107+
}
108+
});
109+
</script>
110+
68111
<script type="text/x-red" data-template-name="dropbox">
69112
<div class="form-row">
70113
<label for="node-input-dropbox"><i class="fa fa-user"></i> Dropbox</label>

dropbox/dropbox.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,66 @@ module.exports = function(RED) {
3030
}
3131
});
3232

33+
function DropboxInNode(n) {
34+
RED.nodes.createNode(this,n);
35+
this.filepattern = n.filepattern || "";
36+
this.dropboxConfig = RED.nodes.getNode(n.dropbox);
37+
var credentials = this.dropboxConfig ? this.dropboxConfig.credentials : {};
38+
if (!credentials.appkey || !credentials.appsecret ||
39+
!credentials.accesstoken) {
40+
this.warn("Missing dropbox credentials");
41+
return;
42+
}
43+
44+
var node = this;
45+
var dropbox = new Dropbox.Client({
46+
//uid: credentials.uid,
47+
key: credentials.appkey,
48+
secret: credentials.appsecret,
49+
token: credentials.accesstoken,
50+
});
51+
node.status({fill:"blue",shape:"dot",text:"initializing"});
52+
dropbox.pullChanges(function(err, data) {
53+
if (err) {
54+
node.error("initialization failed " + err.toString());
55+
node.status({fill:"red",shape:"ring",text:"failed"});
56+
return;
57+
}
58+
node.status({});
59+
node.state = data.cursor();
60+
node.on("input", function(msg) {
61+
node.status({fill:"blue",shape:"dot",text:"checking for changes"});
62+
dropbox.pullChanges(node.state, function(err, data) {
63+
if (err) {
64+
node.warn("failed to fetch changes" + err.toString());
65+
node.status({}); // clear status since poll retries anyway
66+
return;
67+
}
68+
node.state = data.cursor();
69+
node.status({});
70+
var changes = data.changes;
71+
for (var i = 0; i < changes.length; i++) {
72+
var change = changes[i];
73+
msg.payload = change.path;
74+
msg.file = change.path.substring(change.path.lastIndexOf('/') + 1);
75+
msg.event = change.wasRemoved ? 'delete' : 'add';
76+
msg.data = change;
77+
node.send(msg);
78+
}
79+
});
80+
});
81+
var interval = setInterval(function() {
82+
node.emit("input", {});
83+
}, 900000); // 15 minutes
84+
node.on("close", function() {
85+
if (interval !== null) {
86+
clearInterval(interval);
87+
}
88+
});
89+
});
90+
}
91+
RED.nodes.registerType("dropbox in",DropboxInNode);
92+
3393
function DropboxQueryNode(n) {
3494
RED.nodes.createNode(this,n);
3595
this.filename = n.filename || "";
@@ -71,7 +131,6 @@ module.exports = function(RED) {
71131
}
72132
RED.nodes.registerType("dropbox",DropboxQueryNode);
73133

74-
75134
function DropboxOutNode(n) {
76135
RED.nodes.createNode(this,n);
77136
this.filename = n.filename || "";

0 commit comments

Comments
 (0)