-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.js
234 lines (203 loc) · 5.29 KB
/
app.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var express = require("express");
var fs = require("fs");
var path = require("path");
var spawn = require("child_process").spawn;
var ffmpeg = require('fluent-ffmpeg');
var os = require("os");
var async = require("async");
var bodyParser = require("body-parser");
var cookieParser = require("cookie-parser");
var morgan = require("morgan");
function convertBytesToHumanReadableString(bytes) {
var values = ["B", "KB", "MB", "GB", "TB"];
var i = 0;
while (bytes > 1000) {
bytes = bytes / 1000;
i++;
}
return parseInt(bytes) + " " + values[i];
}
function get_stream(metadata, kind) {
return metadata.streams.filter(function(s) {
return s.codec_type === kind;
})[0];
}
var app = express();
app.set('view engine', 'jade');
app.set('view options', {
layout: false
});
app.use(bodyParser());
app.use(cookieParser());
app.use(morgan("dev"));
app.use(express.static(__dirname + '/public'));
var gui = function(req, res) {
var interfaces = os.networkInterfaces();
var rootDirectory = req.params[0] || process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
res.render("index", {
rootDirectory: rootDirectory,
interfaces: interfaces
});
}
app.get("/", gui);
app.param("path", function(){});
app.get( /^\/dir\/(.*)$/, gui);
app.get("/sample", function(req, res) {
res.render("sample");
});
app.post("/folders", function(req, res) {
var dir = req.body.dir;
var entries = fs.readdirSync(dir);
var folders = [];
folders.push({
path: path.join(dir, ".."),
name: "Up"
});
for (var i in entries) {
try {
var p = path.join(dir, entries[i]);
var stat = fs.statSync(p);
if (stat.isDirectory()) {
folders.push({
path: p,
name: require("path").basename(p)
});
}
} catch (ex) {
// Meh
}
}
res.render("folders", {
folders: folders
});
});
function getFileType(path){
var extension = require("path").extname(path);
if (extension && extension.length && extension.length > 0) {
if ( /\b(wmv|mkv|avi|flv|mov|webm|3gp|mp4)$/i.test(extension) ){
return "video";
} else if ( /\b(mp3)$/i.test(extension) ){
return "audio";
} else if ( /\b(jpg|jpeg|png)$/i.test(extension)){
return "image";
} else {
return null;
}
}
return null;
}
app.post("/files", function(req, res) {
var dir = req.body.dir;
var entries = fs.readdirSync(dir);
var files = [];
for (var i in entries) {
try {
var p = path.join(dir, entries[i]);
var stat = fs.statSync(p);
if (stat.isFile()) {
console.log(getFileType(p));
files.push({
filetype: getFileType(p),
path: p,
name: require("path").basename(p)
});
}
} catch (ex) {
// throw ex;
}
}
async.map(files, function(item, callback) {
try {
fs.stat(item.path, function(err, stat) {
// If error unreadable file
if (err) {
item.size = -1;
callback(null, item);
} else {
item.size = convertBytesToHumanReadableString(stat.size);
callback(null, item);
}
});
} catch (err) {
callback(null, item);
}
}, function(err, results) {
res.render("files", {
files: results
});
});
});
app.post("/getFile", function(req, res) {
var dir = req.body.dir;
res.sendFile(dir);
});
app.get("/getFile/*", function(req,res){
var dir = req.url.split("/").splice(2).join("/");
var buf = new Buffer(dir, 'base64');
var src = buf.toString();
res.sendfile(src);
});
app.get("/video/*", function(req, res) {
// It may be wiser to encode it diffferently, since we can hit GET path limit, 2048 characters I guess
var dir = req.url.split("/").splice(2).join("/");
var buf = new Buffer(dir, 'base64');
var src = buf.toString();
var Transcoder = require('./transcoder.js');
// Get the metadata
ffmpeg.ffprobe(src, function(err, metadata) {
// Start ffmpeg
var stream = fs.createReadStream(src);
var video = get_stream(metadata, 'video');
// Feel free to change those, but libx264 is faster than vp8
// A resize mechanism can be used
var transcoder = new Transcoder(stream);
(video.codec === "h264") ? transcoder.videoCodec('copy') : transcoder.videoCodec('libx264');
res.writeHead(200, {
'Content-Type': 'video/mp4',
'X-Content-Duration': video.duration
});
transcoder.audioCodec("libvo_aacenc")
.sampleRate(44100)
.channels(2)
.audioBitrate(128 * 1000)
.format('mp4')
.on('finish', function() {
console.log("ffmpeg process finished");
})
.stream().pipe(res);
});
});
app.post("/metadata", function(req, res) {
var file = req.body.file;
console.log("Metadata of", file, "requested");
var filetype = getFileType(file);
if ( filetype == "video"){
ffmpeg.ffprobe(file, function(err, metadata) {
metadata.path = file;
metadata.filetype = filetype;
res.render("metadata", {
path: file,
filetype: filetype,
video: get_stream(metadata, 'video') || {
width: -1,
height: -1,
duration: -1,
bit_rate: -1,
codec_long_name: 'N/A'
},
audio: get_stream(metadata, 'audio') || {
bit_rate: -1,
codec_long_name: 'N/A'
}
});
});
} else {
// No metada for mp3s/images yet :/
var metadata = {};
metadata.path = file;
metadata.filetype = filetype;
res.render("metadata", metadata);
}
});
console.log("Open http://localhost:8000/ at your Chrome browser.");
app.listen(8000);