You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObsUploader.vue 9.5 kB

4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <template>
  2. <div class="dropzone-wrapper dataset-files">
  3. <div
  4. id="dataset"
  5. class="dropzone"
  6. />
  7. <p class="upload-info">
  8. {{ file_status_text }}
  9. <span class="success">{{ status }}</span>
  10. </p>
  11. </div>
  12. </template>
  13. <script>
  14. /* eslint-disable eqeqeq */
  15. // import Dropzone from 'dropzone/dist/dropzone.js';
  16. // import 'dropzone/dist/dropzone.css'
  17. import createDropzone from '../features/dropzone.js';
  18. import ObsClient from 'esdk-obs-browserjs';
  19. const {_AppSubUrl, _StaticUrlPrefix, csrf} = window.config;
  20. export default {
  21. data() {
  22. return {
  23. dropzoneUploader: null,
  24. maxFiles: 1,
  25. maxFilesize: 1 * 1024 * 1024 * 1024 * 1024,
  26. acceptedFiles: '*/*',
  27. progress: 0,
  28. status: '',
  29. dropzoneParams: {},
  30. file_status_text: ''
  31. };
  32. },
  33. async mounted() {
  34. this.dropzoneParams = $('div#minioUploader-params');
  35. this.file_status_text = this.dropzoneParams.data('file-status');
  36. this.status = this.dropzoneParams.data('file-init-status');
  37. let previewTemplate = '';
  38. previewTemplate += '<div class="dz-preview dz-file-preview">\n ';
  39. previewTemplate += ' <div class="dz-details">\n ';
  40. previewTemplate += ' <div class="dz-filename">';
  41. previewTemplate +=
  42. ' <span data-dz-name data-dz-thumbnail></span>';
  43. previewTemplate += ' </div>\n ';
  44. previewTemplate += ' <div class="dz-size" data-dz-size></div>\n ';
  45. previewTemplate += ' </div>\n ';
  46. previewTemplate += ' <div class="dz-progress ui active progress">';
  47. previewTemplate +=
  48. ' <div class="dz-upload bar" data-dz-uploadprogress><div class="progress"></div></div>\n ';
  49. previewTemplate += ' </div>\n ';
  50. previewTemplate += ' <div class="dz-success-mark">';
  51. previewTemplate += ' <span>上传成功</span>';
  52. previewTemplate += ' </div>\n ';
  53. previewTemplate += ' <div class="dz-error-mark">';
  54. previewTemplate += ' <span>上传失败</span>';
  55. previewTemplate += ' </div>\n ';
  56. previewTemplate += ' <div class="dz-error-message">';
  57. previewTemplate += ' <span data-dz-errormessage></span>';
  58. previewTemplate += ' </div>\n';
  59. previewTemplate += '</div>';
  60. const $dropzone = $('div#dataset');
  61. console.log('createDropzone');
  62. const dropzoneUploader = await createDropzone($dropzone[0], {
  63. url: '/todouploader',
  64. maxFiles: this.maxFiles,
  65. maxFilesize: this.maxFileSize,
  66. timeout: 0,
  67. autoQueue: false,
  68. dictDefaultMessage: this.dropzoneParams.data('default-message'),
  69. dictInvalidFileType: this.dropzoneParams.data('invalid-input-type'),
  70. dictFileTooBig: this.dropzoneParams.data('file-too-big'),
  71. dictRemoveFile: this.dropzoneParams.data('remove-file'),
  72. previewTemplate,
  73. });
  74. // 将文件加入文件列表
  75. dropzoneUploader.on('addedfile', (file) => {
  76. if(file.status == 'added'){
  77. this.onFileAdded(file)
  78. }
  79. });
  80. dropzoneUploader.on('maxfilesexceeded', function (file) {
  81. if (this.files[0].status !== 'success') {
  82. alert(this.dropzoneParams.data('waitting-uploading'));
  83. this.removeFile(file);
  84. return;
  85. }
  86. this.removeAllFiles();
  87. this.addFile(file);
  88. });
  89. this.dropzoneUploader = dropzoneUploader;
  90. },
  91. methods: {
  92. resetStatus() {
  93. this.progress = 0;
  94. this.status = '';
  95. },
  96. updateProgress(file, progress) {
  97. file.previewTemplate.querySelector(
  98. '.dz-upload'
  99. ).style.width = `${progress}%`;
  100. },
  101. emitDropzoneSuccess(file) {
  102. file.status = 'success';
  103. this.dropzoneUploader.emit('success', file);
  104. this.dropzoneUploader.emit('complete', file);
  105. },
  106. emitDropzoneFailed(file) {
  107. this.status = this.dropzoneParams.data('falied');
  108. file.status = 'error';
  109. this.dropzoneUploader.emit('error', file);
  110. },
  111. onFileAdded(file) {
  112. this.resetStatus();
  113. this.status = this.dropzoneParams.data('obs-connecting');
  114. this.do_multi_uploader(file)
  115. },
  116. // 获取key, uuid
  117. get_result(){
  118. var res
  119. $.ajax({
  120. url: '/attachments/get_obs_key',
  121. type: 'GET',
  122. async: false,
  123. success: function(result){
  124. res = result
  125. }
  126. });
  127. return res
  128. },
  129. // 构建ObsClient
  130. getObsClient(result){
  131. return new ObsClient({
  132. access_key_id: result.access_key_id,
  133. secret_access_key: result.secret_access_key,
  134. server : result.server
  135. });
  136. },
  137. // 断点续传
  138. do_multi_uploader(file){
  139. const result = this.get_result()
  140. const upload_datasetId = document
  141. .getElementById('datasetId')
  142. .getAttribute('datasetId');
  143. const obsClient = this.getObsClient(result)
  144. const _this = this
  145. var cp;
  146. var hook;
  147. obsClient.uploadFile({
  148. Bucket : result.bucket,
  149. Key : result.key,
  150. SourceFile : file,
  151. PartSize : 64 * 1024 * 1024,
  152. // 更新进度条
  153. ProgressCallback : function(transferredAmount, totalAmount, totalSeconds){
  154. _this.updateProgress(file, ((transferredAmount / totalAmount) * 100).toFixed(2))
  155. _this.status = `${_this.dropzoneParams.data('uploading')} ${(
  156. (transferredAmount / totalAmount) *
  157. 100
  158. ).toFixed(2)}%`;
  159. },
  160. // 监听文件上传结果
  161. EventCallback : function(eventType, eventParam, eventResult){
  162. console.log("eventType1= ", eventType)
  163. console.log("eventParam1= ", eventParam)
  164. console.log("eventResult1= ", eventResult)
  165. // 文件上传成功
  166. if(eventType == 'completeMultipartUploadSucceed'){
  167. console.log("file = ", file)
  168. $.ajax({
  169. url: '/attachments/add',
  170. type: 'POST',
  171. data: {
  172. 'uuid': result.uuid,
  173. 'file_name': file.name,
  174. 'size': file.size,
  175. 'dataset_id': upload_datasetId,
  176. '_csrf': csrf,
  177. 'type': 1
  178. },
  179. async: false,
  180. success: function (data) {
  181. _this.progress = 100;
  182. _this.status = _this.dropzoneParams.data('upload-complete');
  183. _this.emitDropzoneSuccess(file)
  184. setTimeout(() => {
  185. window.location.reload();
  186. }, 1000);
  187. },
  188. error: function(){
  189. _this.emitDropzoneFailed(file)
  190. }
  191. });
  192. }
  193. },
  194. ResumeCallback : function(resumeHook, uploadCheckpoint){
  195. hook = resumeHook;
  196. cp = uploadCheckpoint;
  197. }
  198. }, function(err, result){
  199. // 出现错误,再次调用断点续传接口,继续上传任务
  200. if(err){
  201. obsClient.uploadFile({
  202. UploadCheckpoint : cp,
  203. // 断点续传后继续更新进度条
  204. ProgressCallback : function(transferredAmount, totalAmount, totalSeconds){
  205. _this.updateProgress(file, ((transferredAmount / totalAmount) * 100).toFixed(2))
  206. _this.status = `${_this.dropzoneParams.data('uploading')} ${(
  207. (transferredAmount / totalAmount) *
  208. 100
  209. ).toFixed(2)}%`;
  210. },
  211. // 监听断点续传的结果
  212. EventCallback : function(eventType, eventParam, eventResult){
  213. console.log("eventType2= ", eventType)
  214. console.log("eventParam2= ", eventParam)
  215. console.log("eventResult2= ", eventResult)
  216. // 文件断点续传成功
  217. if(eventType == 'completeMultipartUploadSucceed'){
  218. $.ajax({
  219. url: '/attachments/add',
  220. type: 'POST',
  221. data: {
  222. 'uuid': result.uuid,
  223. 'file_name': file.name,
  224. 'size': file.size,
  225. 'dataset_id': upload_datasetId,
  226. '_csrf': csrf,
  227. 'type': 1
  228. },
  229. async: false,
  230. success: function (data) {
  231. _this.progress = 100;
  232. _this.status = _this.dropzoneParams.data('upload-complete');
  233. _this.emitDropzoneSuccess(file)
  234. setTimeout(() => {
  235. window.location.reload();
  236. }, 1000);
  237. console.log(data)
  238. },
  239. error: function(){
  240. _this.emitDropzoneFailed(file)
  241. }
  242. });
  243. }
  244. if (eventType == 'uploadPartFailed'){
  245. _this.emitDropzoneFailed(file)
  246. }
  247. }
  248. });
  249. }
  250. });
  251. },
  252. }
  253. };
  254. </script>
  255. <style>
  256. .dropzone-wrapper {
  257. margin: 2em auto;
  258. }
  259. .ui .dropzone {
  260. border: 2px dashed #0087f5;
  261. box-shadow: none !important;
  262. padding: 0;
  263. min-height: 5rem;
  264. border-radius: 4px;
  265. }
  266. .dataset .dataset-files #dataset .dz-preview.dz-file-preview,
  267. .dataset .dataset-files #dataset .dz-preview.dz-processing {
  268. display: flex;
  269. align-items: center;
  270. }
  271. .dataset .dataset-files #dataset .dz-preview {
  272. border-bottom: 1px solid #dadce0;
  273. min-height: 0;
  274. }
  275. </style>