diff --git a/src/websrv.js b/src/websrv.js index b1e15e3..17fa3a8 100644 --- a/src/websrv.js +++ b/src/websrv.js @@ -173,54 +173,52 @@ function Websrv(tlib) { } else if(filePath.match(/^\.\/(b|s|t)\/.*/)) { // file contentType = mimeTypes[extname]; - switch(contentType) { - case "video/webm": - case "video/mp4": - case "video/quicktime": - case "audio/mpeg": - case "audio/ogg": - var start = 0; - var end = 0; - var range = req.headers['range']; - var stat = fs.statSync(filePath); - if(range != null) { - 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; - res.writeHead(206, { - 'Connection':'close', - 'Content-Type':contentType, - 'Content-Length':end - start, - 'Content-Length':stat.size, - 'Content-Range':'bytes '+start+'-'+end+'/'+stat.size, - 'Transfer-Encoding':'chunked' - }); - var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end}); - stream.pipe(res); - break; - default: - 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(); - } + if(( contentType === "video/webm" + || contentType === "video/mp4" + || contentType === "video/quicktime" + || contentType === "audio/mpeg" + || contentType === "audio/ogg") && req.headers['range']) { + var start = 0; + var end = 0; + var range = req.headers['range']); + var stat = fs.statSync(filePath); + if(range != null) { + 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; + res.writeHead(206, { + 'Connection':'close', + 'Content-Type':contentType, + 'Content-Length':end - start, + '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 { + 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(200, { 'Content-Type': contentType, 'Content-Length': content.length, 'Cache-Control': 'max-age=2592000, public' }); - res.end(content, 'utf-8'); + res.writeHead(500); + res.end('Sorry, check with the site admin for error: '+error.code+' ..\n'); + res.end(); } - }); - break; + } + else { + res.writeHead(200, { 'Content-Type': contentType, 'Content-Length': content.length, 'Cache-Control': 'max-age=2592000, public' }); + res.end(content, 'utf-8'); + } + }); } - fs.readFile(filePath, (error, content) => { + } + /*fs.readFile(filePath, (error, content) => { if(error) { if(error.code == 'ENOENT') { res.writeHead(200, { 'Content-Type': contentType }); @@ -232,8 +230,8 @@ function Websrv(tlib) { res.end(); } } - }); - } + });*/ + //} else if(filePath.match(/^\.\/api/i)) { // api var url = filePath.split('/'); if(url[2] === undefined) { // Mainpage