-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·278 lines (228 loc) · 7.29 KB
/
server.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
var express = require('express');
var exphbs = require('express-handlebars');
var formidable = require('formidable');
var fb = require('fb');
var path = require('path');
var http = require('http');
var fs = require('fs');
var util = require("util");
var async = require("async");
var ERROR_RESPONSES = {
authnfail : 'Authentication failed',
authzfail : 'Authorization failed',
fnameblacklisted : 'Filename blacklisted',
notfriends : 'Authorization failed. Not friends with Martin',
extblacklisted : 'Extension blacklisted'
};
var BLACKLISTED_EXTENSIONS = [
'.htm',
'.html',
'.xhtml'
];
// My (Martin Engström) facebook ID will be used if none is specified
var FB_USERID = (process.env.fbuid || '1020902295');
/*
It seems that formidable does not fetch content-range from the form-data
A possible hackish solution is to simply append the data to a file when its filename already exists
NOTE: Very poorly implemented at the moment since if a user tries to upload a file with identical filenames
then their data gets appended on top of each other
*/
/* Since fs.rename and fs.renameSync doesnt work between partitions/hdds */
function properMove(oldFile, newFile) {
var exists = fs.existsSync(newFile);
var is = fs.createReadStream(oldFile);
var os = null;
if (exists)
os = fs.createWriteStream(newFile, {'flags': 'a'});
else
os = fs.createWriteStream(newFile);
is.pipe(os);
is.on('end', function() {
fs.unlinkSync(oldFile);
});
}
function getFbAccessToken(callback) {
/* TODO. need to re-do the whole facebook thing */
}
function getFbUserID(callback) {
fb.api('/me', function (res) {
if (!res || res.error) {
console.log(!res ? 'error occured' : res.error);
callback(null);
return;
}
callback(res.id);
return;
});
}
function fbCheckFriend(friendID, callback) {
fb.api('fql', { q: 'SELECT uid2 FROM friend WHERE uid1 = me() AND uid2 = ' + friendID }, function(res) {
if (!res || res.error) {
console.log(!res ? 'error occured' : res.error);
return;
}
if (res.data.length > 0) {
var friendResult = res.data[0].fql_result_set;
callback(true);
} else {
callback(false);
}
});
}
function parseContentRange(header) {
var result = {
start: 0,
end: 0,
total: 0
};
if (typeof (header) != "undefined") {
// Parse format "bytes start-end/total"
var data = header.split(' ')[1];
result.start = parseInt((data.split('/')[0]).split('-')[0]);
result.end = parseInt((data.split('/')[0]).split('-')[1]);
result.total = parseInt(data.split('/')[1])-1;
}
return result;
}
function checkFbAuth(fields, callback) {
fb.setAccessToken(fields.fbAccessToken);
getFbUserID(function (userid) {
if (userid != FB_USERID) {
fbCheckFriend(FB_USERID, function(result) {
callback(result);
});
} else
callback(true);
});
}
function fixFilenameConflict(filename) {
var new_filename = filename;
if (fs.existsSync(filename)) {
var basename = path.basename(filename, path.extname(filename));
basename = path.dirname(filename) + "/" + basename + "_" + path.extname(filename);
new_filename = fixFilenameConflict(basename);
}
return new_filename;
}
var app = express();
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + "/static"));
app.use(express.static(__dirname + "/files")); // uploaded files
app.engine('handlebars', exphbs({defaultLayout: 'main', layoutsDir: __dirname + '/views/'}));
app.set('view engine', 'handlebars');
/* ONLY ENABLE THE NEXT LINE WHEN IN PRODUCTION */
app.enable('view cache');
app.get('/upload', function(req, res) {
res.write("hello");
res.end();
});
app.post('/json/filelist', function(req, res) {
var uploadsDir = __dirname + "/files";
var files = {};
fs.readdir(uploadsDir, function(err, _files) {
async.each(_files, function(file, callback) {
var _file = file;
fs.stat(path.join(uploadsDir, file), function(err, stat) {
var now, age, ageMinutes, ageSeconds, ageStr;
now = new Date().getTime();
age = (now - (new Date(stat.ctime).getTime()))/1000;
if (age >= 60) {
ageMinutes = Math.floor(age/60);
ageStr = ageMinutes + 'm ';
if (age%60 > 0) {
ageSeconds = Math.floor(age%60);
ageStr = ageStr + ageSeconds + 's';
}
} else {
ageStr = Math.floor(age) + ' s';
}
var file = {
name: _file,
size: stat.size,
path: _file,
age: age,
ageFormatted: ageStr
};
files[_files.indexOf(_file)] = file;
callback();
});
}, function(err) {
if (err) {
console.log(err);
res.status(500);
res.end();
} else {
res.status(200);
res.write(JSON.stringify({ files: files }));
res.end();
}
});
});
});
app.post('/upload', function(req, res) {
var form = new formidable.IncomingForm();
var json_files = new Array();
function declineFile(path, name, size, error_message) {
fs.unlinkSync(path);
json_files.push({name: name, size: size, error: error_message});
}
function uploadComplete() {
res.status(200);
res.json({'files': json_files});
res.end();
}
function handleChunk(fields, files, onComplete) {
checkFbAuth(fields, function(result) {
if (BLACKLISTED_EXTENSIONS.indexOf(path.extname(files.files.name)) < 0) {
if (result) {
onComplete(files);
} else {
declineFile(files.files.path, files.files.name, files.files.size, ERROR_RESPONSES.notfriends);
}
} else {
declineFile(files.files.path, files.files.name, files.files.size, ERROR_RESPONSES.extblacklisted);
}
uploadComplete();
});
}
form.parse(req, function(err, fields, files) {
var chunk = parseContentRange(req.headers['content-range']);
if (chunk.total == 0) {
handleChunk(fields, files, function(files) {
var finalName = fixFilenameConflict(__dirname + '/files/' + files.files.name);
properMove(files.files.path, finalName);
json_files.push({name: path.basename(finalName), size: files.files.size, error: ''});
});
} else {
if (chunk.start == 0) {
handleChunk(fields, files, function(files) {
properMove(files.files.path, __dirname + '/tmp/' + files.files.name);
json_files.push({name: files.files.name, size: files.files.size, error: ''});
});
} else {
if (chunk.end == chunk.total) {
handleChunk(fields, files, function(files) {
properMove(files.files.path, __dirname + '/tmp/' + files.files.name);
var finalName = fixFilenameConflict(__dirname + '/files/' + files.files.name);
fs.renameSync(__dirname + '/tmp/' + files.files.name, finalName);
json_files.push({name: path.basename(finalName), size: files.files.size, error: ''});
});
} else {
/* Mid-piece chunk, append to tmp file */
properMove(files.files.path, __dirname + '/tmp/' + files.files.name);
json_files.push({name: files.files.name, size: files.files.size, error: ''});
uploadComplete();
}
}
}
});
});
app.get('*', function(req, res) {
// 404 / catch all - user probably attempted to access a deleted file
res.render('404', {
adress: req.originalUrl
});
});
app.listen(process.env.port || 8007, function() {
console.log('Uploader listening');
});