-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (115 loc) · 3.34 KB
/
index.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
const Koa = require('koa');
const Router = require('koa-router');
const utils = require('sm-utils');
const koaStatic = require('koa-static');
const fs = require('fs');
const app = new Koa();
const router = new Router()
const port = process.env.PORT || 8080;
app.use(koaStatic(__dirname + '/dist'));
const uploads = {};
async function isUserLoggedIn(ctx, next) {
if (ctx.request.headers.user) {
console.log('User Authenticated');
await next();
return;
}
console.log('user not authenticated');
ctx.status = 200;
ctx.body = {
type: 'Authentication failure',
data: 'user not authenticated',
};
return;
}
router.post('/upload', isUserLoggedIn, async (ctx) => {
const fileId = ctx.request.headers['fileid'];
const name = ctx.request.headers['name'];
const fileSize = parseInt(ctx.request.headers['size'], 10);
const startFrom = parseInt(ctx.request.headers['startfrom'], 10);
if (uploads[fileId] && fileSize == uploads[fileId].bytesReceived) {
ctx.body = {};
return;
}
if (!uploads[fileId]) uploads[fileId] = {bytesReceived: 0};
let writeStream;
if (!startFrom) {
writeStream = fs.createWriteStream(name);
}
else {
writeStream = fs.createWriteStream(name, {flags: 'a'});
}
ctx.req.on('data', (data) => {
uploads[fileId].bytesReceived += data.length;
});
ctx.req.pipe(writeStream);
let status = 200;
let body = {};
writeStream.on('close', () => {
if (uploads[fileId].bytesReceived == fileSize) {
console.log("success");
delete uploads[fileId];
status = 200;
body = {
type: 'success',
};
return;
}
else {
console.log("error while writing: " + uploads[fileId].bytesReceived);
status = 500;
return;
}
});
writeStream.on('error', (err) => {
console.log(err);
status = 500;
});
ctx.status = status;
ctx.body = body;
});
router.get('/status', isUserLoggedIn, async (ctx) => {
const fileId = ctx.request.headers['fileid'];
const name = ctx.request.headers['name'];
const fileSize = parseInt(ctx.request.headers['size'], 10);
if (name) {
try {
let stats = await utils.file(name).stat();
if (stats.isFile())
{
console.log(`file found on server with size: ${stats.size}`);
if (fileSize == stats.size) {
ctx.body = {
uploaded: fileSize,
};
return;
}
if (!uploads[fileId]) uploads[fileId] = {};
uploads[fileId].bytesReceived = stats.size;
}
}
catch (err){
// console.log(err);
}
}
if (uploads[fileId]) {
ctx.body = {
uploaded : uploads[fileId].bytesReceived,
};
return;
}
ctx.body = {
uploaded : 0,
};
return;
});
router.get('/(.*)', async (ctx) => {
const html = await utils.file('./dist/index.html').read();
ctx.type = 'text/html';
ctx.body = html;
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(port, () => {
console.log('Listening on port: ', port);
});