forked from flowjs/flow.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (53 loc) · 1.8 KB
/
app.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
process.env.TMPDIR = 'tmp'; // to avoid the EXDEV rename error, see http://stackoverflow.com/q/21071303/76173
var express = require('express');
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
var flow = require('./flow-node.js')('tmp');
var fs = require('fs');
var app = express();
// Configure access control allow origin header stuff
var ACCESS_CONTROLL_ALLOW_ORIGIN = false;
// Host most stuff in the public folder
app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/../../src'));
// Handle uploads through Flow.js
app.post('/upload', multipartMiddleware, function(req, res) {
flow.post(req, function(status, filename, original_filename, identifier) {
console.log('POST', status, original_filename, identifier);
if (status == 'done') {
// Assemble Chunks
var stream = fs.createWriteStream('uploads/' + filename);
flow.write(identifier, stream);
}
if (ACCESS_CONTROLL_ALLOW_ORIGIN) {
res.header("Access-Control-Allow-Origin", "*");
}
res.status(status).send();
});
});
app.options('/upload', function(req, res){
console.log('OPTIONS');
if (ACCESS_CONTROLL_ALLOW_ORIGIN) {
res.header("Access-Control-Allow-Origin", "*");
}
res.status(200).send();
});
// Handle status checks on chunks through Flow.js
app.get('/upload', function(req, res) {
flow.get(req, function(status, filename, original_filename, identifier) {
console.log('GET', status);
if (ACCESS_CONTROLL_ALLOW_ORIGIN) {
res.header("Access-Control-Allow-Origin", "*");
}
if (status == 'found') {
status = 200;
} else {
status = 404;
}
res.status(status).send();
});
});
app.get('/download/:identifier', function(req, res) {
flow.write(req.params.identifier, res);
});
app.listen(3000);