UNPKG

2.33 kBJavaScriptView Raw
1/*
2 * @package jsDAV
3 * @subpackage DAV
4 * @copyright Copyright(c) 2011 Ajax.org B.V. <info AT ajax.org>
5 * @author Mike de Boer <info AT mikedeboer DOT nl>
6 * @license http://github.com/mikedeboer/jsDAV/blob/master/LICENSE MIT License
7 */
8var Http = require("http");
9var Url = require("url");
10var Dbox = require("dbox");
11var jsDAV = require("./../lib/jsdav");
12
13jsDAV.debugMode = true;
14
15var creds = require("./credentials").dropbox;
16
17var appData = creds.app;
18var app = Dbox.app(appData);
19
20// replace the following with:
21// var tokens, access_token;
22// to generate new token pairs.
23var tokens = creds.tokens;
24var access_token = creds.access_token;
25
26function 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
36Http.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 // we continue and generate an access token
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 // all prerequisites are available!
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});