This commit is contained in:
Flummi 2016-11-28 08:25:23 +01:00
parent e26930bdbf
commit 8d6ac4afcc

View File

@ -181,22 +181,29 @@ function Websrv(tlib) {
var start = 0; var start = 0;
var end = 0; var end = 0;
var range = req.headers['range']; var range = req.headers['range'];
var stat = fs.statSync(filePath); fs.stat(filePath, (err, stat) => {
if(range != null) { if(!err) {
start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-'))); if(range != null) {
end = parseInt(range.slice(range.indexOf('-')+1, range.length)); start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-')));
} end = parseInt(range.slice(range.indexOf('-')+1, range.length));
if(isNaN(end) || end == 0) end = stat.size-1; }
if(start > end) return; if(isNaN(end) || end == 0) end = stat.size-1;
res.writeHead(206, { if(start > end) return;
'Connection':'close', res.writeHead(206, {
'Content-Type':contentType, 'Connection':'close',
'Content-Length':end - start, 'Content-Type':contentType,
'Content-Range':'bytes '+start+'-'+end+'/'+stat.size, 'Content-Length':end - start,
'Transfer-Encoding':'chunked' 'Content-Range':'bytes '+start+'-'+end+'/'+stat.size,
'Transfer-Encoding':'chunked'
});
var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end});
stream.pipe(res);
}
else {
res.writeHead(200, { 'Content-Type': contentType });
res.end('404 - f0ck you', 'utf-8');
}
}); });
var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end});
stream.pipe(res);
} }
else { else {
fs.readFile(filePath, (error, content) => { fs.readFile(filePath, (error, content) => {
@ -218,20 +225,6 @@ function Websrv(tlib) {
}); });
} }
} }
/*fs.readFile(filePath, (error, content) => {
if(error) {
if(error.code == 'ENOENT') {
res.writeHead(200, { 'Content-Type': contentType });
res.end('404 - f0ck you', 'utf-8');
}
else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
res.end();
}
}
});*/
//}
else if(filePath.match(/^\.\/api/i)) { // api else if(filePath.match(/^\.\/api/i)) { // api
var url = filePath.split('/'); var url = filePath.split('/');
if(url[2] === undefined) { // Mainpage if(url[2] === undefined) { // Mainpage