@@ -0,0 +1,50 @@ | |||
var fs = require("fs"); | |||
var pathlib = require("path"); | |||
/* | |||
* Move a file by copying it, to let it move across devices | |||
*/ | |||
exports.move = function(src, dst, cb) { | |||
var ws; | |||
try { | |||
ws = fs.createWriteStream(dst); | |||
} catch (err) { return cb(err) } | |||
var rs; | |||
try { | |||
rs = fs.createReadStream(src); | |||
} catch (err) { return cb(err) } | |||
rs | |||
.on("data", d => ws.write(d)) | |||
.on("end", () => { | |||
ws.end(); | |||
cb(); | |||
}) | |||
.on("error", cb); | |||
} | |||
/* | |||
* Remove directory, deleting its content in the process | |||
*/ | |||
exports.rmdir = function(dir) { | |||
console.log("rmdir", dir); | |||
try { | |||
fs.accessSync(dir, fs.F_OK) | |||
} catch (err) { | |||
console.trace(err); | |||
return; | |||
} | |||
fs.readdirSync(dir).forEach(f => { | |||
var fname = pathlib.join(dir, f); | |||
var stat = fs.statSync(fname); | |||
if (stat.isDirectory()) | |||
exports.rmdir(fname); | |||
else | |||
fs.unlinkSync(fname) | |||
}); | |||
fs.rmdir(dir); | |||
} |
@@ -1,5 +1,6 @@ | |||
var fs = require("fs"); | |||
var pathlib = require("path"); | |||
var fsutil = require("../fsutil"); | |||
var player = require("./player"); | |||
var httpStream = require("./http-stream"); | |||
var torrentStreamer = require("./torrent-streamer"); | |||
@@ -7,23 +8,37 @@ var subtitleFinder = require("./subtitle-finder"); | |||
exports.httpPath = player.httpPath; | |||
exports.cleanupFiles = []; | |||
var app; | |||
var conf | |||
exports.init = function(_app, conf) { | |||
exports.init = function(_app, _conf) { | |||
app = _app; | |||
conf = _conf; | |||
player.init(app, conf); | |||
httpStream.init(app, conf); | |||
torrentStreamer.init(app, conf); | |||
subtitleFinder.init(app, conf); | |||
} | |||
exports.playFile = function(path, cb) { | |||
/* | |||
* Filename is optional; in case you want to provide a filename for subtitles | |||
* but want a different path | |||
*/ | |||
exports.playFile = function(path, cb, filename) { | |||
filename = filename || pathlib.basename(path); | |||
// Find file's length | |||
fs.stat(path, (err, stat) => { | |||
if (err) { | |||
console.trace(err); | |||
return cb(); | |||
} | |||
// Find subtitles | |||
subtitleFinder.find(stat.size, pathlib.basename(path), subFile => { | |||
subtitleFinder.find(stat.size, filename, subFile => { | |||
exports.cleanupFiles.push(subFile); | |||
// Play! | |||
player.play(path, subFile, cb); | |||
@@ -46,6 +61,7 @@ exports.playTorrent = function(magnet, cb) { | |||
// Find subtitles | |||
subtitleFinder.find(filesize, filename, subFile => { | |||
exports.cleanupFiles.push(subFile); | |||
// Play! | |||
player.play(app.getAddress()+httpStream.httpPath, subFile, cb); | |||
@@ -58,4 +74,11 @@ exports.isPlaying = player.isPlaying; | |||
player.onstop = function() { | |||
torrentStreamer.stop(); | |||
httpStream.stop(); | |||
exports.cleanupFiles.forEach(f => { | |||
fs.unlink(f, err => { if (err) console.trace(err) }); | |||
}); | |||
exports.cleanupFiles = []; | |||
fsutil.rmdir(conf.tmpdir+"/torrent-stream"); | |||
} |
@@ -90,7 +90,7 @@ exports.play = function(path, subFile, cb) { | |||
path, | |||
"--no-cache-pause", | |||
"--no-resume-playback", | |||
"--input-unix-socket", ipcServer | |||
"--input-ipc-server", ipcServer | |||
]; | |||
if (subFile) { |
@@ -1,6 +1,8 @@ | |||
var fs = require("fs"); | |||
var pathlib = require("path"); | |||
var web = require("webstuff"); | |||
var play = require("./js/play"); | |||
var fsutil = require("./js/fsutil"); | |||
var conf = JSON.parse(fs.readFileSync("conf.json")); | |||
@@ -37,3 +39,26 @@ app.post("/play/magnet", (req, res) => { | |||
}); | |||
}); | |||
}); | |||
app.post("/play/file", (req, res) => { | |||
req.parseBody((err, fields, files) => { | |||
var file = files.file; | |||
if (file == null || !file.name || file.size === 0) | |||
return res.redirect("/"); | |||
var fname = conf.tmpdir+"/uploaded-file"+pathlib.extname(file.name); | |||
fsutil.move(file.path, fname, err => { | |||
if (err) { | |||
console.trace(err); | |||
return res.redirect("/"); | |||
} | |||
play.cleanupFiles.push(fname); | |||
play.playFile(fname, () => { | |||
res.redirect(play.httpPath); | |||
}, file.name); | |||
}); | |||
}); | |||
}); |
@@ -19,6 +19,12 @@ | |||
<input type="url" name="magnet" autocomplete="off"> | |||
<button>Play</button> | |||
</form> | |||
<form class="part" method="post" enctype="multipart/form-data" action="/play/file"> | |||
<div class="name">File:</div> | |||
<input type="file" name="file" autocomplete="off"> | |||
<button>Play</button> | |||
</form> | |||
</div> | |||
<script src="/webstuff.js"></script> |