-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
79 lines (64 loc) · 1.94 KB
/
app.js
File metadata and controls
79 lines (64 loc) · 1.94 KB
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
var koa = require('koa');
var app = koa();
var serve = require('koa-static');
var subdomain = require('koa-sub-domain');
var _ = require('koa-route');
var os = require('os');
var path = require('path');
var parse = require('co-busboy');
var fs = require('fs');
var toml = require('toml');
var mv = require('mv');
/*app.use(subdomain('potm',function *(next){
this.body = 'Hey, you got it :-)';
}));*/
var config_string = fs.readFileSync('./config.toml', 'utf-8');
var config = toml.parse(config_string);
app.use(function *(next){
// ignore non-POSTs
if ('POST' != this.method) return yield next;
// multipart upload
var parts = parse(this);
var part;
//if (config["password"] != )
var allow_upload = false;
var uploaded_files = [];
while (part = yield parts) {
if (part.length)
{
if (part[1] == config["password"])
{
allow_upload = true;
continue;
}
}
if (allow_upload == false)
{
console.log("Password mismatch.")
return yield next;
}
var stream = fs.createWriteStream(path.join(os.tmpdir(), part.filename));
part.pipe(stream);
console.log('uploading %s -> %s', part.filename, stream.path);
uploaded_files.push(part.filename);
}
console.log(uploaded_files);
var i = 0;
for (i = 0; i < uploaded_files.length; i++)
{
var target_sub_dir = path.extname(uploaded_files[i]);
target_sub_dir = target_sub_dir.substring(1);
if (target_sub_dir == "bic")
target_sub_dir = "dmvault";
mv(path.join(os.tmpdir(), uploaded_files[i]), './files/' + target_sub_dir + '/' + uploaded_files[i], function(err) {
if (err)
console.log("Error in moving file, " + err);
// done. it tried fs.rename first, and then falls back to
// piping the source file to the dest file and then unlinking
// the source file.
});
}
this.redirect('/');
});
app.use(serve('files'));
app.listen(2020);