123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- 'use strict';
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = UploadSdk;
-
- var _contants = require('./contants');
-
- var _utils = require('./utils');
-
- var _utils2 = _interopRequireDefault(_utils);
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- function FileFactory(file) {
- this.offset = 0; //用于断点续传,默认为 0
- this.BYTES_PER_CHUNK = 1024 * 1024;
- this.file = file;
- this.fileSize = file.size;
- this.fileType = file.name.split('.')[1].toLowerCase();
- this.chunkNum = this.BYTES_PER_CHUNK > this.fileSize ? Math.ceil(this.fileSize / this.BYTES_PER_CHUNK) : 1;
- this.chunkSize = this.BYTES_PER_CHUNK > this.fileSize ? this.fileSize : this.BYTES_PER_CHUNK;
- }
-
- FileFactory.prototype.setOffset = function (offset) {
- this.offset = offset;
- };
-
- function UploadSdk(host, origin, token, file) {
-
- this.handlers = []; //用于处理出触发事件
- this.host = host.indexOf('http:') > -1 ? 'http:' + host : host;
- this.origin = origin;
- this.token = token;
- this.file = file;
-
- this.generateMd5 = function (callback) {
- (0, _utils2.default)(this.file).then(function (result) {
- callback(result);
- });
- };
-
- this.imageUploadAction = function () {
- var _this = this;
-
- return new Promise(function (resolve, reject) {
- var fileFactory = new FileFactory(_this.file);
- if (!fileFactory.fileType.match('[(jpg)|(png)|(gif)]+$')) {
- reject(_contants.Constants.IMAGE_TYPE_ERROR);
- }
- _this.generateMd5(function (md5) {
- _this.md5 = md5;
- _this.postImage(_this.md5, fileFactory).then(function (res) {
- resolve(res);
- }).catch(function (err) {
- reject(err);
- });
- });
- });
- };
-
- this.videoUploadAction = function () {
- var _this2 = this;
-
- return new Promise(function (resolve, reject) {
- if (!_this2.file.type.match('[(mp4)|(rmvb)|(flv)|(mpeg)|(avi)]+$')) {
- reject(_contants.Constants.VIDOE_TYPE_ERROR);
- }
- _this2.postVideo().then(function (res) {
- resolve(res);
- }).catch(function (err) {
- reject(err);
- });
- });
- };
-
- this.postVideo = function () {
- var _this3 = this;
-
- var self = this;
- return new Promise(function (resolve, reject) {
- var xhr = new XMLHttpRequest();
- xhr.open('POST', _this3.host + _contants.Constants.VIDEO_MULTIPART + _this3.origin, true);
- xhr.withCredentials = true;
- xhr.setRequestHeader('Authorization', 'Bearer ' + _this3.token);
- xhr.setRequestHeader('Content-type', 'application/json');
- xhr.upload.addEventListener("progress", function updateProgress(oEvent) {
- if (oEvent.lengthComputable) {
- var percentComplete = oEvent.loaded / oEvent.total * 100;
- self.emitUpdateProgress(percentComplete);
- }
- }, false);
- var fd = new FormData();
- fd.append('file', _this3.file);
- fd.append('code_rate', 'ld,sd,hd');
- xhr.send(fd);
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4) {
- var obj = JSON.parse(xhr.responseText);
- if (xhr.status === 304 || xhr.status >= 200 && xhr.status < 300) {
- resolve({
- code: _contants.Constants.UPLOAD_SUCCESS_CODE,
- url: obj.url,
- msg: 'success'
- });
- } else {
- reject({
- code: _contants.Constants.UPLOAD_FAILED_CODE,
- url: '',
- msg: obj.message
- });
- }
- }
- };
- });
- };
-
- this.postImage = function (md5, fileFactory) {
- var _this4 = this;
-
- var self = this;
- return new Promise(function (resolve, reject) {
- var xhr = new XMLHttpRequest();
- xhr.upload.addEventListener("progress", function updateProgress(oEvent) {
- if (oEvent.lengthComputable) {
- var percentComplete = oEvent.loaded / oEvent.total * 100;
- self.emitUpdateProgress(percentComplete);
- }
- }, false);
- xhr.open('POSt', _this4.host + _contants.Constants.IMAGE_MULTIPART + _this4.origin, true);
- xhr.withCredentials = true;
- xhr.setRequestHeader('Authorization', 'Bearer ' + _this4.token);
- xhr.setRequestHeader('X-Upload-File-Size', fileFactory.fileSize);
- xhr.setRequestHeader('X-Upload-Chunk-Index', 1 + '');
- xhr.setRequestHeader('X-Upload-Chunk-Num', fileFactory.chunkNum);
- xhr.setRequestHeader('X-Upload-Chunk-Size', fileFactory.chunkSize);
- xhr.setRequestHeader('X-Upload-Offset', fileFactory.offset + '');
- xhr.setRequestHeader('X-Upload-File-Md5', md5);
- xhr.setRequestHeader('X-Upload-File-Type', fileFactory.fileType);
- var fd = new FormData();
- fd.append('file', _this4.file);
- xhr.send(fd);
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4) {
- var obj = JSON.parse(xhr.responseText);
- if (xhr.status === 304 || xhr.status >= 200 && xhr.status < 300) {
-
- if (obj.status === 1) {
- resolve({
- code: _contants.Constants.UPLOAD_SUCCESS_CODE,
- url: obj.url,
- msg: 'success'
- });
- } else if (obj.status === 0) {
- fileFactory.setOffset(obj.msg);
- this.getAllResponseHeaders.postImage(md5, fileFactory);
- }
- } else {
- reject({
- code: _contants.Constants.UPLOAD_FAILED_CODE,
- url: '',
- msg: obj.message
- });
- }
- }
- };
- });
- };
- }
-
- UploadSdk.prototype = {
-
- onUpdateProgress: function onUpdateProgress(subscriber) {
- var isExist = this.handlers.some(function (item) {
- return item == subscriber;
- });
- if (!isExist) {
- this.handlers.push(subscriber);
- }
- return this;
- },
-
- emitUpdateProgress: function emitUpdateProgress(data) {
- this.handlers.forEach(function (fn) {
- fn(data);
- });
- return this;
- }
- };
|