This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdropbox.js
146 lines (125 loc) · 3.3 KB
/
dropbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var filefog = require('filefog');
var fs = require('fs');
filefog.use('dropbox',require('filefog-dropbox'),{
client_key : 'jixx3poq3ysx5fo',
client_secret : 'q6een1n7vuro900',
redirect_url: 'http://localhost:3000/api/dropbox/redirect/'
});
var dropboxProvider = filefog.provider("dropbox");
function route(app){
function requieLogin(req, res, next){
var sess=req.session;
if(sess.dropbox_access_token){
next();
}else{
res.redirect('/api/dropbox/auth/');
}
}
app.get('/api/dropbox/auth/',function(req, res){
var sess=req.session;
if(sess.dropbox_access_token){
console.log('user already login');
res.redirect('/api/dropbox/home/');
}else{
auth(req, res);
}
});
app.get('/api/dropbox/redirect/',function(req,res){
redirect(req, res).
then(function(){
res.redirect('/api/dropbox/home/');
}).done();
});
app.get('/api/dropbox/status/',function(req,res){
var sess=req.session;
if(sess.dropbox_access_token){
res.send('logined');
}else{
res.send('not logined');
}
});
app.get('/api/dropbox/fileIndex/',requieLogin,function(req,res){
var sess=req.session;
if(sess.dropbox_access_token){
getFolderMetadata(sess.dropbox_access_token,req.query.i,
function(metadata){
console.log("metadata acquired");
res.send(metadata);
});
}else{
res.send('not logined');
}
});
app.get('/api/dropbox/download/',requieLogin,function(req,res){
var sess=req.session;
if(!req.query.i)
res.send("identifier not set");
else{
downloadFile(sess.dropbox_access_token,req.query.i,
function(data){
console.log(data.data);
console.log("stat:");
console.log(data._raw.stat);
var stat = data._raw.stat;
//res.send(data.data);
res.writeHead(200, {
'Content-Type': stat.mimeType,
'Content-Length': stat.size
});
res.write(data.data);
res.end();
console.log("download called and responsed");
});
}
});
app.get('/api/dropbox/home/',function(req,res){
res.sendFile(__dirname + '/public/views/dropbox_home.html');
});
}
function downloadFile(access_token,location,callback){
dropboxClient = filefog.client("dropbox", {
access_token: access_token,
refresh_token: null
})
.then(function (client) {
return client.downloadFile(location);
}).then(callback);
}
function getFolderMetadata(access_token,location,callback){
dropboxClient = filefog.client("dropbox", {
access_token: access_token,
refresh_token: null
})
.then(function (client) {
return client.retrieveFolderItems(location);
//return client.getFolderInformation(location);
}).then(callback);
}
function auth(req, res){
var url = dropboxProvider.oAuthGetAuthorizeUrl();
res.redirect(url);
}
function redirect(req,res){
var token = dropboxProvider.oAuthGetAccessToken(req.query.code);
return token.then(function(results){
var sess=req.session;
sess.dropbox_access_token = results.access_token;
console.log(results);
}).catch(function(err){
console.log(err);
});
}
module.exports = {
route : route,
auth : auth,
redirect : redirect
};
// dropboxClient = filefog.client("dropbox", {
// access_token: '...',
// refresh_token: '...'
// })
// .then(function (client) {
// return client.getFolderInformation();
// }).then(function (response) {
// //Dropbox folder metadata, in a standardized format.
// });