Last active
June 7, 2019 10:11
-
-
Save PathToLife/bc7f89c67869a87100053d39717d0edf to your computer and use it in GitHub Desktop.
Revisions
-
PathToLife revised this gist
Jun 7, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ /* * Run these commands in the directory that will be exposed to the network: * $optional: wget "http://gist raw of this js file" * $npm i serve-static serve-index finalhandler ip * $node fileserver.js */ var http = require('http'), -
PathToLife created this gist
Jun 7, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,93 @@ /* * Run these commands in the directory that will be exposed to the network: * $optional: wget "http://gist raw of this js file" * $npm i serve-static serve-index finalhandler * $node fileserver.js */ var http = require('http'), fs = require('fs'), util = require('util'), url = require('url'), path = require('path'), serveStatic = require('serve-static'), finalhandler = require('finalhandler'), ip = require('ip'); var serveIndex = require('serve-index') var port = 1337; var serve = serveStatic(__dirname + '/'); var index = serveIndex(__dirname + '/', { 'icons': true }); function returnMedia(fpath, req, res, stat) { var total = stat.size; var ext = path.extname(fpath).slice(1); if (req.headers['range']) { var range = req.headers.range; var parts = range.replace(/bytes=/, "").split("-"); var partialstart = parts[0]; var partialend = parts[1]; var start = parseInt(partialstart, 10); var end = partialend ? parseInt(partialend, 10) : total - 1; var chunksize = (end - start) + 1; console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunksize); var file = fs.createReadStream(fpath, { start: start, end: end }); res.writeHead(206, { 'Content-Range': 'bytes ' + start + '-' + end + '/' + total, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': 'video/' + ext }); file.pipe(res); } else { console.log('ALL: ' + total); res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'video/' + ext }); fs.createReadStream(fpath).pipe(res); } } http.createServer(function(req, res) { if (req.url == '/index.html' || req.url == '/') { console.log(req.url); return index(req, res, finalhandler(req, res)); } else { var urlParts = url.parse(req.url, true); var fpath = decodeURIComponent(urlParts.pathname.slice(1)); try { var stat = fs.statSync(fpath); } catch (err) { console.log(err); return } if (stat.isDirectory()) { return index(req, res, finalhandler(req, res)); } else { switch (path.extname(fpath)) { case '.avi': case '.webm': case '.gif': case '.ogg': case '.ogv': case '.mov': case '.mp4': case '.rmvb': case '.m4v': case '.mkv': return returnMedia(fpath, req, res, stat); default: return serve(req, res, finalhandler(req, res)); } } } }).listen(port, "0.0.0.0", function() { console.log('Server running at http://' + ip.address() + ':' + port + '/'); });