-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (45 loc) · 1.7 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const fileUpload = require('express-fileupload');
const fs = require('fs');
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(fileUpload());
const fileStorage = {};
app.post('/upload/single-stream', (req, res) => {
// let data = req.body.file;
// let buff = new Buffer(data, 'base64');
// console.log(buff.toString('ascii'));
let chunks = [];
// const chunkSize = Number(req.headers["content-length"]);
const chunkId = Number(req.headers["x-chunk-id"]);
const fileName = req.headers["x-content-name"];
const chunksQuantity = Number(req.headers["x-chunks-quantity"]);
const fileId = 1;
const file = fileStorage[fileId] = fileStorage[fileId] || [];
req.on("data", (part) => {
chunks.push(part);
// console.log(part.length);
}).on("end", () => {
const completeChunk= Buffer.concat(chunks);
file[chunkId] = completeChunk;
if(chunksQuantity === chunkId + 1) {
const completeFile = Buffer.concat(file);
// console.log('GO ' + completeFile.length);
const fileStream = fs.createWriteStream(__dirname + '/uploads/' + fileName);
fileStream.write(completeFile);
fileStream.end();
delete fileStorage[fileId];
}
res.setHeader("Content-Type", "application/json");
res.write(JSON.stringify({status: 200}));
res.end();
});
});
app.get('/', (req, res) => {
console.log(req.body);
res.status(200).send('App is up');
});
app.listen(5000, () => console.log('Server is running on Port 5000'));