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); | |||||
} |
var fs = require("fs"); | var fs = require("fs"); | ||||
var pathlib = require("path"); | var pathlib = require("path"); | ||||
var fsutil = require("../fsutil"); | |||||
var player = require("./player"); | var player = require("./player"); | ||||
var httpStream = require("./http-stream"); | var httpStream = require("./http-stream"); | ||||
var torrentStreamer = require("./torrent-streamer"); | var torrentStreamer = require("./torrent-streamer"); | ||||
exports.httpPath = player.httpPath; | exports.httpPath = player.httpPath; | ||||
exports.cleanupFiles = []; | |||||
var app; | var app; | ||||
var conf | |||||
exports.init = function(_app, conf) { | |||||
exports.init = function(_app, _conf) { | |||||
app = _app; | app = _app; | ||||
conf = _conf; | |||||
player.init(app, conf); | player.init(app, conf); | ||||
httpStream.init(app, conf); | httpStream.init(app, conf); | ||||
torrentStreamer.init(app, conf); | torrentStreamer.init(app, conf); | ||||
subtitleFinder.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 | // Find file's length | ||||
fs.stat(path, (err, stat) => { | fs.stat(path, (err, stat) => { | ||||
if (err) { | |||||
console.trace(err); | |||||
return cb(); | |||||
} | |||||
// Find subtitles | // Find subtitles | ||||
subtitleFinder.find(stat.size, pathlib.basename(path), subFile => { | |||||
subtitleFinder.find(stat.size, filename, subFile => { | |||||
exports.cleanupFiles.push(subFile); | |||||
// Play! | // Play! | ||||
player.play(path, subFile, cb); | player.play(path, subFile, cb); | ||||
// Find subtitles | // Find subtitles | ||||
subtitleFinder.find(filesize, filename, subFile => { | subtitleFinder.find(filesize, filename, subFile => { | ||||
exports.cleanupFiles.push(subFile); | |||||
// Play! | // Play! | ||||
player.play(app.getAddress()+httpStream.httpPath, subFile, cb); | player.play(app.getAddress()+httpStream.httpPath, subFile, cb); | ||||
player.onstop = function() { | player.onstop = function() { | ||||
torrentStreamer.stop(); | torrentStreamer.stop(); | ||||
httpStream.stop(); | httpStream.stop(); | ||||
exports.cleanupFiles.forEach(f => { | |||||
fs.unlink(f, err => { if (err) console.trace(err) }); | |||||
}); | |||||
exports.cleanupFiles = []; | |||||
fsutil.rmdir(conf.tmpdir+"/torrent-stream"); | |||||
} | } |
path, | path, | ||||
"--no-cache-pause", | "--no-cache-pause", | ||||
"--no-resume-playback", | "--no-resume-playback", | ||||
"--input-unix-socket", ipcServer | |||||
"--input-ipc-server", ipcServer | |||||
]; | ]; | ||||
if (subFile) { | if (subFile) { |
var fs = require("fs"); | var fs = require("fs"); | ||||
var pathlib = require("path"); | |||||
var web = require("webstuff"); | var web = require("webstuff"); | ||||
var play = require("./js/play"); | var play = require("./js/play"); | ||||
var fsutil = require("./js/fsutil"); | |||||
var conf = JSON.parse(fs.readFileSync("conf.json")); | var conf = JSON.parse(fs.readFileSync("conf.json")); | ||||
}); | }); | ||||
}); | }); | ||||
}); | }); | ||||
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); | |||||
}); | |||||
}); | |||||
}); |
<input type="url" name="magnet" autocomplete="off"> | <input type="url" name="magnet" autocomplete="off"> | ||||
<button>Play</button> | <button>Play</button> | ||||
</form> | </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> | </div> | ||||
<script src="/webstuff.js"></script> | <script src="/webstuff.js"></script> |