Skip to content

Commit 86182a3

Browse files
committed
Make missing credential checking more consistent.
1 parent a6032cb commit 86182a3

File tree

2 files changed

+80
-77
lines changed

2 files changed

+80
-77
lines changed

dropbox/dropbox.html

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,15 @@
8181
</script>
8282

8383
<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>
84+
<p>Dropbox watch node. Watches for file events on Dropbox. By
85+
default all file events are reported, but the filename pattern can
86+
be supplied to limit the events to files which have full filenames
87+
that match the glob pattern. The event messages consist of the
88+
full filename in <b>msg.payload</b> property, the filename
89+
in <b>msg.file</b>, the event type in <b>msg.event</b> and
90+
the <a href="https://github.com/dropbox/dropbox-js">dropbox.js</a>
91+
API <a href="http://coffeedoc.info/github/dropbox/dropbox-js/master/classes/Dropbox/Http/PulledChange.html">PulledChange</a>
92+
object in <b>msg.data</b>.</p>
9193
</script>
9294

9395
<script type="text/javascript">

dropbox/dropbox.js

Lines changed: 71 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,28 @@ module.exports = function(RED) {
9595
this.filename = n.filename || "";
9696
this.dropboxConfig = RED.nodes.getNode(n.dropbox);
9797
var credentials = this.dropboxConfig ? this.dropboxConfig.credentials : {};
98+
if (!credentials.appkey || !credentials.appsecret ||
99+
!credentials.accesstoken) {
100+
this.warn("Missing dropbox credentials");
101+
return;
102+
}
103+
98104
var node = this;
99-
if (credentials.appkey && credentials.appsecret &&
100-
credentials.accesstoken) {
101-
var dropbox = new Dropbox.Client({
102-
//uid: credentials.uid,
103-
key: credentials.appkey,
104-
secret: credentials.appsecret,
105-
token: credentials.accesstoken,
106-
});
107-
node.on("input", function(msg) {
108-
var filename = this.filename || msg.filename;
109-
if (filename === "") {
110-
node.warn("No filename specified");
111-
return;
112-
}
113-
msg.filename = filename;
114-
node.status({fill:"blue",shape:"dot",text:"downloading"});
115-
dropbox.readFile(filename, function(err, data) {
105+
var dropbox = new Dropbox.Client({
106+
//uid: credentials.uid,
107+
key: credentials.appkey,
108+
secret: credentials.appsecret,
109+
token: credentials.accesstoken,
110+
});
111+
node.on("input", function(msg) {
112+
var filename = this.filename || msg.filename;
113+
if (filename === "") {
114+
node.warn("No filename specified");
115+
return;
116+
}
117+
msg.filename = filename;
118+
node.status({fill:"blue",shape:"dot",text:"downloading"});
119+
dropbox.readFile(filename, function(err, data) {
116120
if (err) {
117121
node.warn("download failed " + err.toString());
118122
delete msg.payload;
@@ -121,13 +125,10 @@ module.exports = function(RED) {
121125
msg.payload = data;
122126
delete msg.error;
123127
}
124-
node.status({});
125-
node.send(msg);
126-
});
128+
node.status({});
129+
node.send(msg);
127130
});
128-
} else {
129-
node.warn("Missing dropbox credentials");
130-
}
131+
});
131132
}
132133
RED.nodes.registerType("dropbox",DropboxQueryNode);
133134

@@ -137,52 +138,43 @@ module.exports = function(RED) {
137138
this.localFilename = n.localFilename || "";
138139
this.dropboxConfig = RED.nodes.getNode(n.dropbox);
139140
var credentials = this.dropboxConfig ? this.dropboxConfig.credentials : {};
141+
if (!credentials.appkey || !credentials.appsecret ||
142+
!credentials.accesstoken) {
143+
this.warn("Missing dropbox credentials");
144+
return;
145+
}
140146
var node = this;
141-
if (credentials.appkey && credentials.appsecret &&
142-
credentials.accesstoken) {
143-
var dropbox = new Dropbox.Client({
144-
//uid: credentials.uid,
145-
key: credentials.appkey,
146-
secret: credentials.appsecret,
147-
token: credentials.accesstoken,
148-
});
149-
node.status({fill:"blue",shape:"dot",text:"checking credentials"});
150-
dropbox.getAccountInfo(function (err) {
151-
if (err) {
152-
node.error("Error verifying credentials: " + err);
153-
node.status({fill:"red",shape:"ring",text:"access denied"});
147+
var dropbox = new Dropbox.Client({
148+
//uid: credentials.uid,
149+
key: credentials.appkey,
150+
secret: credentials.appsecret,
151+
token: credentials.accesstoken,
152+
});
153+
node.status({fill:"blue",shape:"dot",text:"checking credentials"});
154+
dropbox.getAccountInfo(function (err) {
155+
if (err) {
156+
node.error("Error verifying credentials: " + err);
157+
node.status({fill:"red",shape:"ring",text:"access denied"});
158+
return;
159+
}
160+
node.status({});
161+
node.on("input", function(msg) {
162+
var filename = this.filename || msg.filename;
163+
if (filename === "") {
164+
node.warn("No filename specified");
154165
return;
155166
}
156-
node.status({});
157-
node.on("input", function(msg) {
158-
var filename = this.filename || msg.filename;
159-
if (filename === "") {
160-
node.warn("No filename specified");
161-
return;
162-
}
163-
var localFilename = this.localFilename || msg.localFilename;
164-
if (localFilename) {
165-
// TODO: use chunked upload for files larger than 150M
166-
node.status({fill:"blue",shape:"dot",text:"uploading"});
167-
fs.readFile(localFilename, function read(err, data) {
168-
if (err) {
169-
node.error(err.toString());
170-
node.status({fill:"red",shape:"ring",text:"failed"});
171-
return;
172-
}
167+
var localFilename = this.localFilename || msg.localFilename;
168+
if (localFilename) {
169+
// TODO: use chunked upload for files larger than 150M
170+
node.status({fill:"blue",shape:"dot",text:"uploading"});
171+
fs.readFile(localFilename, function read(err, data) {
172+
if (err) {
173+
node.error(err.toString());
174+
node.status({fill:"red",shape:"ring",text:"failed"});
175+
return;
176+
}
173177

174-
dropbox.writeFile(filename, data, function(err) {
175-
if (err) {
176-
node.error(err.toString());
177-
node.status({fill:"red",shape:"ring",text:"failed"});
178-
return;
179-
}
180-
node.status({});
181-
});
182-
});
183-
} else if (typeof msg.payload !== "undefined") {
184-
var data = RED.util.ensureBuffer(msg.payload);
185-
node.status({fill:"blue",shape:"dot",text:"uploading"});
186178
dropbox.writeFile(filename, data, function(err) {
187179
if (err) {
188180
node.error(err.toString());
@@ -191,12 +183,21 @@ module.exports = function(RED) {
191183
}
192184
node.status({});
193185
});
194-
}
195-
});
186+
});
187+
} else if (typeof msg.payload !== "undefined") {
188+
var data = RED.util.ensureBuffer(msg.payload);
189+
node.status({fill:"blue",shape:"dot",text:"uploading"});
190+
dropbox.writeFile(filename, data, function(err) {
191+
if (err) {
192+
node.error(err.toString());
193+
node.status({fill:"red",shape:"ring",text:"failed"});
194+
return;
195+
}
196+
node.status({});
197+
});
198+
}
196199
});
197-
} else {
198-
node.warn("Missing dropbox credentials");
199-
}
200+
});
200201
}
201202
RED.nodes.registerType("dropbox out",DropboxOutNode);
202203
};

0 commit comments

Comments
 (0)