-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
95 lines (69 loc) · 2.15 KB
/
Copy pathapp.js
File metadata and controls
95 lines (69 loc) · 2.15 KB
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
const express = require('express');
const app = express();
const path = require("path");
const fs = require("fs");
var port = 3000;
var ip = require("ip");
var directory = `W:\\torrent`;
var videos = [];
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Please enter the port for server : ("+port+" Default)", function(portInput) {
rl.question("Please enter the path form where videos will be served :", function(pathInput) {
port = (portInput == "" || isNaN(portInput)) ? port : portInput;
directory = pathInput == "" ? directory : pathInput;
rl.close();
});
});
rl.on("close", function() {
app.listen(port,function(){
console.log("SERVER SERVING ON :"+port);
console.log("ACCESS WEBSITE ON FOLLOWING URL : ");
console.log("http://"+ip.address() + ":"+port);
console.log(`SERVING DIRECTORY :` + path.join(directory));
});
});
app.use("/assets/",express.static(path.join(__dirname,"assets")));
app.set("view engine","ejs");
app.set("views",path.join(__dirname,"views"));
videos = fromDir(directory);
videos = videos.map(function(video){
return {
name : path.basename(video),
file : video,
url : path.basename(video).replace(/[^A-Z0-9]/ig, "-")
};
});
for(var video of videos){
bindExpressRoute(video);
}
app.get("/",function(req,res){
res.render("index",{videos : videos});
});
// FUNCTIONS
function fromDir(startPath){
var items = [];
if (!fs.existsSync(startPath)){
return items;
}
var files=fs.readdirSync(startPath);
for(var i=0;i<files.length;i++){
var filename=path.join(startPath,files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()){
items = items.concat(fromDir(filename));
}else if ([".mp4",".webm",".ogg"].indexOf(path.extname(filename)) >=0) {
items.push(filename);
};
};
return items;
};
function bindExpressRoute(video){
app.get("/video/"+video.url,function(req,res){
res.sendFile(video.file);
});
return video;
}