-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcontroller.js
234 lines (173 loc) · 5.7 KB
/
controller.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const
crypto = require('crypto'),
config = require('./config'),
NodeCache = require( "node-cache" ),
rp = require('request-promise');
var mycache = new NodeCache();
//steps 1,2,3
module.exports.home = async (req,res,next)=>{
let token = req.session.token;
if(token){
try{
let paths = await getLinksAsync(token);
if(paths.length>0){
res.render('gallery', { imgs: paths, layout:false});
}else{
//if no images, ask user to upload some
res.render('empty', {layout:false});
}
}catch(error){
return next(new Error("Error getting images from Dropbox"));
}
}else{
res.redirect('/login');
}
}
//steps 4,5,6
module.exports.login = (req,res,next)=>{
//create a random state value
let state = crypto.randomBytes(16).toString('hex');
//Save state and temporarysession for 10 mins
// mycache.set(state, "aTempSessionValue", 600);
mycache.set(state, req.sessionID, 600);
let dbxRedirect= config.DBX_OAUTH_DOMAIN
+ config.DBX_OAUTH_PATH
+ "?response_type=code&client_id="+config.DBX_APP_KEY
+ "&redirect_uri="+config.OAUTH_REDIRECT_URL
+ "&state="+state;
res.redirect(dbxRedirect);
}
//steps 8-12
module.exports.oauthredirect = async (req,res,next)=>{
if(req.query.error_description){
return next( new Error(req.query.error_description));
}
let state= req.query.state;
//if(!mycache.get(state)){
if(mycache.get(state)!=req.sessionID){
return next(new Error("session expired or invalid state"));
}
//Exchange code for token
if(req.query.code ){
let options={
url: config.DBX_API_DOMAIN + config.DBX_TOKEN_PATH,
//build query string
qs: {'code': req.query.code,
'grant_type': 'authorization_code',
'client_id': config.DBX_APP_KEY,
'client_secret':config.DBX_APP_SECRET,
'redirect_uri':config.OAUTH_REDIRECT_URL},
method: 'POST',
json: true
}
try{
let response = await rp(options);
//we will replace later cache with a proper storage
//mycache.set("aTempTokenKey", response.access_token, 3600);
await regenerateSessionAsync(req);
req.session.token = response.access_token;
res.redirect("/");
}catch(error){
return next(new Error('error getting token. '+error.message));
}
}
}
//Returns a promise that fulfills when a new session is created
function regenerateSessionAsync(req){
return new Promise((resolve,reject)=>{
req.session.regenerate((err)=>{
err ? reject(err) : resolve();
});
});
}
module.exports.logout = async (req,res,next)=>{
try{
await destroySessionAsync(req);
res.redirect("/login");
}catch(error){
return next(new Error('error logging out. '+error.message));
}
}
//Returns a promise that fulfills when a session is destroyed
function destroySessionAsync(req){
return new Promise(async (resolve,reject)=>{
try{
//First ensure token gets revoked in Dropbox.com
let options={
url: config.DBX_API_DOMAIN + config.DBX_TOKEN_REVOKE_PATH,
headers:{"Authorization":"Bearer "+req.session.token},
method: 'POST'
}
let result = await rp(options);
}catch(error){
reject(new Error('error destroying token. '));
}
//then destroy the session
req.session.destroy((err)=>{
err ? reject(err) : resolve();
});
});
}
/*Gets temporary links for a set of files in the root folder of the app
It is a two step process:
1. Get a list of all the paths of files in the folder
2. Fetch a temporary link for each file in the folder */
async function getLinksAsync(token){
//List images from the root of the app folder
let result= await listImagePathsAsync(token,'');
//Get a temporary link for each of those paths returned
let temporaryLinkResults= await getTemporaryLinksForPathsAsync(token,result.paths);
//Construct a new array only with the link field
var temporaryLinks = temporaryLinkResults.map(function (entry) {
return entry.link;
});
return temporaryLinks;
}
/*
Returns an object containing an array with the path_lower of each
image file and if more files a cursor to continue */
async function listImagePathsAsync(token,path){
let options={
url: config.DBX_API_DOMAIN + config.DBX_LIST_FOLDER_PATH,
headers:{"Authorization":"Bearer "+token},
method: 'POST',
json: true ,
body: {"path":path}
}
try{
//Make request to Dropbox to get list of files
let result = await rp(options);
//Filter response to images only
let entriesFiltered= result.entries.filter(function(entry){
return entry.path_lower.search(/\.(gif|jpg|jpeg|tiff|png)$/i) > -1;
});
//Get an array from the entries with only the path_lower fields
var paths = entriesFiltered.map(function (entry) {
return entry.path_lower;
});
//return a cursor only if there are more files in the current folder
let response= {};
response.paths= paths;
if(result.hasmore) response.cursor= result.cursor;
return response;
}catch(error){
return next(new Error('error listing folder. '+error.message));
}
}
//Returns an array with temporary links from an array with file paths
function getTemporaryLinksForPathsAsync(token,paths){
var promises = [];
let options={
url: config.DBX_API_DOMAIN + config.DBX_GET_TEMPORARY_LINK_PATH,
headers:{"Authorization":"Bearer "+token},
method: 'POST',
json: true
}
//Create a promise for each path and push it to an array of promises
paths.forEach((path_lower)=>{
options.body = {"path":path_lower};
promises.push(rp(options));
});
//returns a promise that fullfills once all the promises in the array complete or one fails
return Promise.all(promises);
}