ali_oss.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. const AliOss = (function (){
  2. const setting = {
  3. hint: true
  4. };
  5. let client, uploadInfo;
  6. const downloadFile = function (url, filename) {
  7. axios.get(url, {responseType: 'blob' }).then(res => {
  8. saveAs(res.data, filename);
  9. });
  10. };
  11. const downloadFileSync = function(url) {
  12. return new Promise((resolve, reject) => {
  13. axios.get(url, {responseType: 'blob'}).then(res => {
  14. resolve(res.data);
  15. }).catch(err => {
  16. reject(err);
  17. });
  18. })
  19. };
  20. const zipFiles = function (files, filename = '打包.zip', successCallback, errorCallback) {
  21. const zip = new JSZip();
  22. const download = [], fails = [];
  23. files.forEach(f => {
  24. download.push(downloadFileSync(f.filepath).then(data => {
  25. if (setting.hint) {
  26. toastr.success(`文件 “${f.filename + f.fileext}” 下载成功...`);
  27. }
  28. zip.file(f.filename + f.fileext, data, {binary: true});
  29. }).catch(err => {
  30. fails.push(f);
  31. }));
  32. });
  33. Promise.all(download).then(() => {
  34. toastr.clear();
  35. if (fails.length < files.length) {
  36. if (setting.hint) {
  37. toastr.success('所有文件下载成功,压缩中...');
  38. }
  39. zip.generateAsync({ type: "blob" }).then(content => {
  40. saveAs(content, filename);
  41. successCallback && successCallback(fails);
  42. });
  43. } else {
  44. errorCallback && errorCallback(fails);
  45. }
  46. })
  47. };
  48. const setSetting = function(data) {
  49. if (!data) return;
  50. setting.hint = data.hint || setting.hint;
  51. };
  52. const stopUpload = async function() {
  53. if (!client) toastr.warning('当前没有上传中的大文件');
  54. client.cancel();
  55. uploadInfo.isStop = true;
  56. if (uploadInfo.stopObj) uploadInfo.stopObj.attr('disabled', 'disabled');
  57. };
  58. const resumeUpload = async function() {
  59. if (!client || !uploadInfo.abortCheckpoint) return;
  60. if (uploadInfo.stopObj) uploadInfo.stopObj.removeAttr('disabled');
  61. if (uploadInfo.resumeObj) uploadInfo.resumeObj.attr('disabled', 'disabled');
  62. try {
  63. const result = await client.multipartUpload(uploadInfo.filepath, uploadInfo.file, {
  64. checkpoint: uploadInfo.abortCheckpoint,
  65. progress: (p, cpt, res) => {
  66. uploadInfo.abortCheckpoint = cpt;
  67. // 刷新进度条
  68. if (uploadInfo.progressObj) {
  69. uploadInfo.progressObj.attr('aria-valuenow', p * 100);
  70. uploadInfo.progressObj.width((p * 100) + '%');
  71. }
  72. }
  73. });
  74. return await endUploadBigFile();
  75. } catch (e) {
  76. if (uploadInfo.resumeObj) uploadInfo.resumeObj.removeAttr('disabled');
  77. }
  78. };
  79. const resetRelaObj = function() {
  80. if (uploadInfo.progressObj) {
  81. uploadInfo.progressObj.attr('aria-valuenow', 0);
  82. uploadInfo.progressObj.width('0%');
  83. }
  84. if (uploadInfo.stopObj) {
  85. uploadInfo.stopObj.show();
  86. uploadInfo.stopObj.removeAttr('disabled');
  87. uploadInfo.stopObj.bind('click', function() {
  88. stopUpload();
  89. });
  90. }
  91. if (uploadInfo.resumeObj) {
  92. uploadInfo.resumeObj.show();
  93. uploadInfo.resumeObj.attr('disabled', 'disabled');
  94. uploadInfo.resumeObj.bind('click', function() {
  95. resumeUpload();
  96. });
  97. }
  98. };
  99. const clearRelaObj = function() {
  100. if (uploadInfo.stopObj) {
  101. uploadInfo.stopObj.attr('disabled', 'disabled');
  102. uploadInfo.stopObj.unbind('click');
  103. }
  104. if (uploadInfo.resumeObj) {
  105. uploadInfo.resumeObj.attr('disabled', 'disabled');
  106. uploadInfo.resumeObj.unbind('click');
  107. }
  108. };
  109. const beginUploadBigFile = async function () {
  110. const beingData = JSON.parse(JSON.stringify(uploadInfo.defaultData));
  111. beingData.type = 'begin';
  112. beingData.fileInfo = uploadInfo.fileInfo;
  113. const info = await postDataAsync(uploadInfo.url, beingData, false);
  114. uploadInfo.filepath = info.filepath;
  115. uploadInfo.filename = info.filename;
  116. if (!info.oss) throw '授权信息错误';
  117. uploadInfo.oss = info.oss;
  118. };
  119. const endUploadBigFile = async function() {
  120. client = null;
  121. const endData = JSON.parse(JSON.stringify(uploadInfo.defaultData));
  122. endData.type = 'end';
  123. endData.filepath = uploadInfo.filename;
  124. endData.fileInfo = uploadInfo.fileInfo;
  125. clearRelaObj();
  126. const uploadRes = await postDataAsync(uploadInfo.url, endData, false);
  127. if (uploadInfo.callback) {
  128. uploadInfo.callback(uploadRes);
  129. }
  130. uploadInfo = {};
  131. return uploadRes;
  132. };
  133. const uploadBigFile = async function (file, url, data, relaObj, callback) {
  134. uploadInfo = { url, file, ...relaObj, callback };
  135. uploadInfo.fileInfo = { filename: file.name, filesize: file.size, type: file.type, lastModified: file.lastModified };
  136. uploadInfo.defaultData = data;
  137. await beginUploadBigFile();
  138. try {
  139. client = new OSS(uploadInfo.oss);
  140. resetRelaObj();
  141. const result = await client.multipartUpload(uploadInfo.filepath, uploadInfo.file, {
  142. progress: (p, cpt, res) => {
  143. uploadInfo.abortCheckpoint = cpt;
  144. // 刷新进度条
  145. if (uploadInfo.progressObj) {
  146. uploadInfo.progressObj.attr('aria-valuenow', p * 100);
  147. uploadInfo.progressObj.width((p * 100) + '%');
  148. }
  149. }
  150. });
  151. return await endUploadBigFile();
  152. } catch (err) {
  153. if (uploadInfo.resumeObj) uploadInfo.resumeObj.removeAttr('disabled');
  154. if (uploadInfo.stopObj) uploadInfo.stopObj.attr('disabled', 'disabled');
  155. }
  156. };
  157. return { downloadFile, downloadFileSync, zipFiles, setSetting, uploadBigFile, resumeUpload }
  158. })();