videoUpload.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. (function (global) {
  2. var AdminUpload = {
  3. config: {
  4. file: null,
  5. token: '',
  6. accessKeyId: '',
  7. accessKeySecret: "",
  8. bucketName: '',
  9. region: "",
  10. domain:'',
  11. uploadIng: function (res) {
  12. }
  13. },
  14. uploadName: function () {
  15. var data =
  16. ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
  17. "s", "t", "u", "v", "w", "x", "y", "z"], nums = "";
  18. for (var i = 0; i < 32; i++) {
  19. var r = parseInt(Math.random() * 35);
  20. nums += data[r];
  21. }
  22. return nums;
  23. }
  24. };
  25. /**
  26. * QINIU 七牛上传
  27. * @return {Promise<any>}
  28. * @constructor
  29. */
  30. AdminUpload.QINIU = function () {
  31. var putExtra = {
  32. fname: '',
  33. params: {},
  34. mimeType: null,
  35. }, config = {
  36. useCdnDomain: true, //使用cdn加速
  37. }, that = this;
  38. return new Promise(function (resolve, reject) {
  39. var observable = qiniu.upload(that.config.file, that.uploadName(), that.config.token, putExtra, config);
  40. observable.subscribe({
  41. next: (result) => {
  42. // 主要用来展示进度
  43. that.config.uploadIng(result.total.percent.toFixed(2));
  44. },
  45. error: (err) => {
  46. reject(err);
  47. },
  48. complete: (res) => {
  49. resolve({url: that.config.domain + "/" + res.key, type: 2});
  50. },
  51. });
  52. })
  53. }
  54. /**
  55. * cos 上传
  56. * @return {Promise<any>}
  57. * @constructor
  58. */
  59. AdminUpload.COS = function () {
  60. var that = this;
  61. return new Promise(function (resolve, reject) {
  62. console.log(that.config);
  63. var client = new COS({
  64. SecretId: that.config.accessKeyId,
  65. SecretKey: that.config.accessKeySecret
  66. });
  67. client.putObject({
  68. Bucket: that.config.bucketName,
  69. Region: that.config.region,
  70. Key: that.uploadName(),
  71. StorageClass: 'STANDARD',
  72. Body: that.config.file,
  73. onProgress: function (progressData) {
  74. that.config.uploadIng(parseInt(progressData.percent * 100));
  75. }
  76. }, function (err, data) {
  77. if (err) {
  78. reject(err);
  79. } else {
  80. resolve({url: "http://" + data.Location, type: 4});
  81. }
  82. });
  83. })
  84. }
  85. /**
  86. * oss 上传
  87. * @return {Promise<any>}
  88. * @constructor
  89. */
  90. AdminUpload.OSS = function () {
  91. var that = this, file = that.config.file, suffix = file.name.substr(file.name.indexOf(".")),
  92. storeAs = this.uploadName() + suffix;
  93. return new Promise(function (resolve, reject) {
  94. var client = new OSS.Wrapper({
  95. region: that.config.region,
  96. accessKeyId: that.config.accessKeyId,
  97. accessKeySecret: that.config.accessKeySecret,
  98. bucket: that.config.bucketName,
  99. });
  100. var options = {
  101. progress: async function (p, k, i) {
  102. that.config.uploadIng(parseInt(p.toFixed(2) * 100));
  103. },
  104. partSize: 1000 * 1024,//设置分片大小
  105. timeout: 120000,//设置超时时间
  106. }
  107. client.multipartUpload(storeAs, file, options).then(function (result) {
  108. var url = result.res.requestUrls[0];
  109. var length = url.lastIndexOf('?');
  110. var imgUrl = url.substr(0, length);
  111. resolve({url: imgUrl, type: 3});
  112. }).catch(function (err) {
  113. reject(err);
  114. });
  115. });
  116. }
  117. /**
  118. * 执行上传
  119. * @param driver
  120. * @param opt
  121. * @return {*}
  122. */
  123. AdminUpload.upload = function (driver, opt) {
  124. if (typeof opt !== 'object') {
  125. opt = {};
  126. }
  127. Object.assign(this.config, opt);
  128. var suffix = this.config.file.name.substr(this.config.file.name.indexOf("."));
  129. if (suffix != '.mp4') {
  130. return new Promise(function (resolve, reject) {
  131. reject('只能上传MP4文件');
  132. })
  133. }
  134. if (this[driver]) {
  135. return this[driver]();
  136. } else {
  137. return new Promise(function (resolve, reject) {
  138. reject('上传句柄不存在');
  139. })
  140. }
  141. }
  142. global.AdminUpload = AdminUpload;
  143. return AdminUpload;
  144. }(this));