1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var Http = require("http");
|
9 | var Url = require("url");
|
10 | var Dbox = require("dbox");
|
11 | var jsDAV = require("./../lib/jsdav");
|
12 |
|
13 | jsDAV.debugMode = true;
|
14 |
|
15 | var creds = require("./credentials").dropbox;
|
16 |
|
17 | var appData = creds.app;
|
18 | var app = Dbox.app(appData);
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var tokens = creds.tokens;
|
24 | var access_token = creds.access_token;
|
25 |
|
26 | function next() {
|
27 | console.log("creating jsDAV server with dropbox tokens:", access_token);
|
28 | jsDAV.createServer({
|
29 | type: "dropbox",
|
30 | app_key: appData.app_key,
|
31 | app_secret: appData.app_secret,
|
32 | access_token: access_token
|
33 | }, 8000);
|
34 | }
|
35 |
|
36 | Http.createServer(function(req, res) {
|
37 | var parsedUrl = Url.parse(req.url, true);
|
38 | if (parsedUrl.path == "/") {
|
39 | if (!tokens) {
|
40 | app.requesttoken(function(status, request_token){
|
41 | tokens = request_token;
|
42 | res.writeHead(200, {"content-type": "text/plain"});
|
43 | res.end(JSON.stringify(tokens) + "\n\n\n" +
|
44 | "Go to " + tokens.authorize_url);
|
45 | });
|
46 | }
|
47 | else if (!access_token) {
|
48 |
|
49 | app.accesstoken(tokens, function(status, access) {
|
50 | console.log("Got access token: ", access);
|
51 | access_token = access;
|
52 | var client = app.client(access_token);
|
53 | client.account(function(status, reply){
|
54 | res.writeHead(200, {"content-type": "text/plain"});
|
55 | res.end(JSON.stringify(reply));
|
56 | next();
|
57 | });
|
58 | });
|
59 | }
|
60 |
|
61 | else {
|
62 | res.writeHead(200, {"content-type": "text/plain"});
|
63 | res.end("OK");
|
64 | next();
|
65 | }
|
66 | }
|
67 | else {
|
68 | res.writeHead(404, {"content-type": "text/plain"});
|
69 | res.end("Not Found.");
|
70 | }
|
71 | }).listen(8080, function() {
|
72 | console.log("Browse to http://localhost:8080/ to get started!");
|
73 | });
|