Lightweight node app to use in place of plex to self host your video and movie collections. Running on just under 50MB of ram this is ideal for people looking to host videos on minimal hardware.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

66 lines
1.8 KiB

const routes = require('express').Router();
const utils = require("../utils");
const userUtils = require("../user");
const fs = require('fs');
const videoManager = require("../videoManager");
routes.get('/', (request, result) =>
{
var videoID = request.query.v;
if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER ||
userUtils.isValidAPI(request.query.api) ||
videoManager.isPublicVideo(videoID))
{
const rootDir= (videoManager.isPublicVideo(videoID)) ?
process.env.PUBLIC_DIR :
process.env.PRIVATE_DIR;
const path = rootDir + videoID;
const stat = fs.statSync(path);
const fileSize = stat.size;
const range = request.headers.range;
if (range)
{
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1]
? parseInt(parts[1], 10)
: fileSize-1;
const chunksize = (end-start)+1;
const file = fs.createReadStream(path, {start, end});
const head =
{
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
};
result.writeHead(206, head);
file.pipe(result);
}
else
{
const head =
{
'Content-Length': fileSize,
'Content-Type': 'video/mp4',
};
result.writeHead(200, head);
fs.createReadStream(path).pipe(result);
}
}
else
{
utils.printError(result, "You need to be logged in");
}
});
module.exports = routes;