node-webplay
Version:
A nodejs streaming server implementation
359 lines (271 loc) • 12.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _fs = require("fs");
var _fs2 = _interopRequireDefault(_fs);
var _path = require("path");
var _path2 = _interopRequireDefault(_path);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var StateManFs = function () {
function StateManFs(processor, opt) {
_classCallCheck(this, StateManFs);
//super();
if (null == processor) {
throw "Invalid option.processor";
}
var defop = {
destination: _path2.default.normalize(_path2.default.join(__dirname, "../.."))
};
if (null != opt) this.options = Object.assign(defop, opt);else this.options = defop;
this.options.subdir = this.options.subdir || "STATIC";
this.options.statusfile = this.options.statusfile || "status.json";
this.processor = processor;
}
//PRIVATE
_createClass(StateManFs, [{
key: "create_processor",
value: function create_processor(owner, name, id, out_opt) {
var opt = { destination: _path2.default.join(this.options.destination, owner) };
var procopt = Object.assign({}, this.options, opt);
if (null != out_opt) {
Object.assign(procopt, out_opt);
}
if (null != id) procopt.id = id;
var p = new this.processor(name, procopt);
p.on("processing", function (perc) {
console.log("--PROCESSING: ", perc);
//this.set_quick_processing(p, perc);
});
//TODO: change it to pass and determine the directory from the statman
p["statman_target_dir"] = p.get_target_dir();
return p;
}
}, {
key: "update_status",
value: function update_status(fspath, status, fail_if_exist) {
var flags = fail_if_exist ? "wx+" : "a+";
return new Promise(function (resolve, reject) {
_fs2.default.readFile(fspath, { flag: flags, encoding: "utf8" }, function (err, data) {
if (err && (err.code != "ENOENT" || !fail_if_exist)) {
reject(err);
} else {
if (err || "" == data) {
data = "{}";
}
var j = {};
try {
j = JSON.parse(data);
} catch (e) {
console.log("JSON PARSE ERR:", e.code, "[", data, "]", flags, fspath, "----", e);
}
if (null != status) {
if (null != status.status) {
if (null == j.previus) {
j.previus = [];
}
if (null != j.status) j.previus.push(j.status);
}
j = Object.assign(j, status);
j.datetime = new Date();
if (null == j.creationtime) {
j.creationtime = new Date();
}
_fs2.default.writeFile(fspath, JSON.stringify(j), { flag: "w+" }, function (err) {
if (err) {
reject(err);
} else {
resolve(j);
}
});
} else {
resolve(j);
}
}
});
});
}
}, {
key: "status_path",
value: function status_path(p) {
return _path2.default.join(p["statman_target_dir"], this.options.statusfile);
}
}, {
key: "set_quick_status",
value: function set_quick_status(p, status) {
var _this = this;
return new Promise(function (resolve, reject) {
var stpath = _this.status_path(p);
var j = { status: status };
_this.update_status(stpath, j, false).then(function () {
return resolve();
}, function (err) {
return reject(err);
});
});
}
}, {
key: "set_quick_processing",
value: function set_quick_processing(p, perc) {
var _this2 = this;
return new Promise(function (resolve, reject) {
var stpath = _this2.status_path(p);
var j = { processing: perc };
_this2.update_status(stpath, j, false).then(function () {
return resolve();
}, function (err) {
return reject(err);
});
});
}
//PUBLIC: ISTATEMAN
}, {
key: "record_error",
value: function record_error(owner, id, err, info) {
var _this3 = this;
return new Promise(function (resolve, reject) {
/*
let procopt = Object.assign({}, this.options, {id: id});
procopt.destination = path.join(procopt.destination, owner);
let p = new this.processor(id, procopt);
*/
var p = _this3.create_processor(owner, id, id);
_this3.update_status(_this3.status_path(p), { status: "error", err: err.toString(), errinfo: info + " [" + err.toString() + "]" }, false).then(function () {
return resolve();
}, function (err) {
return reject(err);
});
});
}
//TODO: processor should get the id and the destination. Should not create an id and should not
// change the destination
}, {
key: "reserve_name",
value: function reserve_name(owner, name) {
var _this4 = this;
return new Promise(function (resolve, reject) {
var p = _this4.create_processor(owner, name); //new this.processor(name, {destination: path.join(this.options.destination, owner)} );
p.mkdirr(p["statman_target_dir"], function (err) {
if (err) {
reject(err);
} else {
var j = { status: "reserved",
name: name,
id: p.id
};
_this4.update_status(_this4.status_path(p), j, true).then(function () /*k*/{
resolve(p.id);
}, function (err) {
reject(err);
});
}
});
});
}
}, {
key: "queue_job",
value: function queue_job(owner, id, file, opt) {
var _this5 = this;
return new Promise(function (resolve, reject) {
/*
let procopt = Object.assign({}, this.options, opt);
procopt.id = id;
procopt.destination = path.join(procopt.destination, owner);
let p = new this.processor(id, procopt);
*/
var p = _this5.create_processor(owner, id, id, opt);
p.read_stream_info(file).then(function (st) {
//console.log("STREAMS: ", st.streams, "\n--------\n", st);
_this5.set_quick_status(p, "analized").then(function () {
console.log("analized");
}, function (err) {
return reject(err);
});
p.encode(file, st.streams).then(function (quality) {
_this5.set_quick_status(p, "encoded").then(function () {}, function (err) {
return reject(err);
});
console.log(">>PACKAGE", _this5.options.subdir, "<<", p["statman_target_dir"], ">>");
var package_dir = _path2.default.join(p["statman_target_dir"], "STATIC");
console.log(">>PACKAGE2", "<<", package_dir, ">>");
p.package(quality, package_dir).then(function () {
var res = {
status: "ok",
owner: owner,
hls3: "STATIC/main.m3u8",
dash: "STATIC/index.mpd",
thumb: ["img001.jpg", "img002.jpg"],
hls4: null,
playready: null,
widevine: null
};
_this5.update_status(_this5.status_path(p), res, false).then(function () /*k*/{
resolve();
}, function (err) {
reject(err);
});
}, function (err) {
reject(err);
});
}, function (err) {
reject(err);
});
}, function (err) {
reject(err);
});
});
}
}, {
key: "list",
value: function list(owner, opt) {
var _this6 = this;
return new Promise(function (resolve, reject) {
//console.log(this.options, opt);
var procopt = Object.assign({}, _this6.options, opt);
// procopt.id = id;
procopt.destination = _path2.default.join(procopt.destination, owner);
//console.log(procopt);
// let p = new this.processor("list", procopt);
//let dir = p.get_target_dir();
_fs2.default.readdir(procopt.destination, function (err, files) {
var j = [];
if (err) {
reject(err);
} else {
for (var i = 0; i < files.length; i++) {
var fstat = _fs2.default.statSync(_path2.default.join(procopt.destination, files[i]));
if (fstat.isDirectory()) {
j.push({ owner: owner, id: files[i] });
}
}
resolve({ assets: j });
}
});
});
}
}, {
key: "status",
value: function status(owner, id) {
var _this7 = this;
return new Promise(function (resolve, reject) {
/*
console.log(owner, id, this.options);
let procopt = Object.assign({}, this.options, {id: id});
procopt.destination = path.join(procopt.destination, owner);
let p = new this.processor(id, procopt);
*/
var p = _this7.create_processor(owner, id, id);
_this7.update_status(_this7.status_path(p), null, false).then(function (j) {
return resolve(j);
}, function (err) {
return reject(err);
});
});
}
}]);
return StateManFs;
}();
exports.default = StateManFs;
//# sourceMappingURL=statmanfs.js.map