node-webplay
Version:
A nodejs streaming server implementation
341 lines (276 loc) • 10.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.UploadManager = undefined;
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 _events = require("events");
var _httprequest = require("../core/httprequest");
var _httprequest2 = _interopRequireDefault(_httprequest);
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"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* Utility method to format bytes into the most logical magnitude (KB, MB,
* or GB).
*/
/*function formatBytes(number) {
var units = ['B', 'KB', 'MB', 'GB', 'TB'],
//bytes = this,
i;
for (i = 0; bytes >= 1024 && i < 4; i++) {
bytes /= 1024;
}
return bytes.toFixed(2) + units[i];
}*/
function upload(upl) {
upl._started = true;
var self = upl;
setTimeout(function () {
// Prevent range overflow
if (self._range_end > self._file.size) {
//self.range_end = self.file_size;
throw "Invalid Range On Upload!";
}
//console.log("re2 " + self._range_end + " " + self._range_start + " " + self._opt.chunk_size);
var chunk = self._file[self._slice_method](self._range_start, self._range_end);
var chunk_id = Math.ceil(self._range_start / self._opt.chunk_size);
var opt = { headers: {
"Content-Type": "application/octet-stream",
"Content-Range": "bytes " + self._range_start + "-" + self._range_end + "/" + self._file.size,
"file-name": self._opt.name,
"chunkid": chunk_id.toString()
}
};
if (null != self._opt.owner) {
opt.headers["owner"] = self._opt.owner;
}
if (null != self._opt.id) {
opt.headers["fileid"] = self._opt.id;
}
var http = new _httprequest2.default(opt);
http.put(self._opt.url, chunk).then(function () /*res*/{
//console.log("re3 " + self._range_end + " " + self._range_start + " " + self._opt.chunk_size);
var n = new Number(self._range_start / self._opt.chunk_size / (self._file.size / self._opt.chunk_size) * 100);
var sn = n.toFixed(2);
self._onProgress(sn);
// If the end range is already the same size as our file, we
// can assume that our last chunk has been processed and exit
// out of the function.
if (self._range_end === self._file.size) {
//console.log("upload completed");
self._onUploadComplete();
} else {
// Update our ranges
self._range_start = self._range_end;
self._range_end = self._range_start + self._opt.chunk_size;
// Prevent range overflow
if (self._range_end > self._file.size) {
self._range_end = self._file.size;
}
// Continue as long as we aren't paused
if (!self._is_paused) {
upload(self);
}
}
}, function (err) {
self._raise_error(err);
});
}, 20);
}
var Uploader = function (_EventEmitter) {
_inherits(Uploader, _EventEmitter);
function Uploader(file, options) {
_classCallCheck(this, Uploader);
var _this = _possibleConstructorReturn(this, (Uploader.__proto__ || Object.getPrototypeOf(Uploader)).call(this));
_this._file = file;
_this._started = false;
_this._range_end = 0;
_this._range_start = 0;
_this._is_paused = true;
var opt = {
url: "/upload",
id: null,
tag: null,
name: file.name,
owner: null,
chunk_size: 1024 * 8 * 10,
start_position: 0
};
if (null != options) _this._opt = Object.assign(opt, options);else _this._opt = opt;
if ("mozSlice" in _this._file) {
_this._slice_method = "mozSlice";
} else if ("webkitSlice" in _this._file) {
_this._slice_method = "webkitSlice";
} else {
_this._slice_method = "slice";
}
_this._range_start = _this._opt.start_position;
_this._range_end = _this._range_start + _this._opt.chunk_size;
if (_this._range_end > _this._file.size) _this._range_end = _this._file.size;
//console.log("re1 " + this._range_end + " " + this._range_start + " " + this._opt.chunk_size);
//
_this.status = "inizialized";
return _this;
}
_createClass(Uploader, [{
key: "name",
value: function name() {
return this._opt.name;
}
}, {
key: "_raise_error",
value: function _raise_error(err) {
//console.log("uploader error: " + err.message);
this._is_paused = true;
this.status = "error";
this.emit("error", err);
}
}, {
key: "_onProgress",
value: function _onProgress(sn) {
this.emit("progress", sn);
}
}, {
key: "_onUploadComplete",
value: function _onUploadComplete() {
this.status = "completed";
this.emit("completed");
}
}, {
key: "start",
value: function start() {
this._is_paused = false;
this.emit("start");
this.status = "started";
upload(this);
}
}, {
key: "pause",
value: function pause() {
this._is_paused = true;
this.status = "paused";
}
}, {
key: "paused",
value: function paused() {
return this._is_paused;
}
}, {
key: "resume",
value: function resume() {
this._is_paused = false;
this.status = "started";
upload(this);
}
}]);
return Uploader;
}(_events.EventEmitter);
exports.default = Uploader;
var UploadManager = exports.UploadManager = function (_EventEmitter2) {
_inherits(UploadManager, _EventEmitter2);
function UploadManager(options) {
_classCallCheck(this, UploadManager);
var _this2 = _possibleConstructorReturn(this, (UploadManager.__proto__ || Object.getPrototypeOf(UploadManager)).call(this));
var opt = {
url: "/upload",
chunk_size: 1024 * 8 * 10,
start_position: 0
};
if (null != options) _this2._opt = Object.assign(opt, options);else _this2._opt = opt;
_this2.uploader = {};
return _this2;
}
_createClass(UploadManager, [{
key: "setOptions",
value: function setOptions(options) {
this._opt = Object.assign(this._opt, options);
}
}, {
key: "_raise_error",
value: function _raise_error(err, kid) {
this.emit("error", err, kid);
}
}, {
key: "_onProgress",
value: function _onProgress(sn, kid) {
this.emit("progress", sn, kid);
}
}, {
key: "_onUploadComplete",
value: function _onUploadComplete(kid) {
this.emit("completed", kid);
}
}, {
key: "add",
value: function add(file, id, options) {
var _this3 = this;
if (null != this.uploader[id]) {
throw "uploader alredy exist";
}
var op = Object.assign(this._opt, options);
var kid = id;
var up = new Uploader(file, op);
up.on("completed", function () {
_this3._onUploadComplete(kid);
});
up.on("error", function (err) {
_this3._raise_error(err, kid);
});
up.on("progress", function (n) {
_this3._onProgress(n, kid);
});
this.uploader[id] = up;
return up;
}
}, {
key: "start",
value: function start(id) {
if (null != this.uploader[id]) {
throw "invalid id";
}
this.uploader[id].start();
}
}, {
key: "pause",
value: function pause(id) {
if (null != this.uploader[id]) {
throw "invalid id";
}
this.uploader[id].pause();
}
}, {
key: "resume",
value: function resume(id) {
if (null != this.uploader[id]) {
throw "invalid id";
}
this.uploader[id].resume();
}
}, {
key: "status",
value: function status(id) {
if (null != this.uploader[id]) {
throw "invalid id";
}
return this.uploader[id].status;
}
}, {
key: "selectFiles",
value: function selectFiles(e) {
var files = e.files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
var id = file.name;
id = id.replace(".", "_");
id = id.replace(" ", "_");
id = id.replace("&", "_");
this.add(file, id);
this.emit("new", id);
}
}
}]);
return UploadManager;
}(_events.EventEmitter);
//# sourceMappingURL=index.js.map