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.

MinioUploader.vue 14 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 SparkMD5 from 'spark-md5';
  18. import axios from 'axios';
  19. import qs from 'qs';
  20. import createDropzone from '../features/dropzone.js';
  21. const {_AppSubUrl, _StaticUrlPrefix, csrf} = window.config;
  22. export default {
  23. data() {
  24. return {
  25. dropzoneUploader: null,
  26. maxFiles: 1,
  27. maxFilesize: 1 * 1024 * 1024 * 1024 * 1024,
  28. acceptedFiles: '*/*',
  29. progress: 0,
  30. status: '',
  31. dropzoneParams: {},
  32. file_status_text: ''
  33. };
  34. },
  35. async mounted() {
  36. this.dropzoneParams = $('div#minioUploader-params');
  37. this.file_status_text = this.dropzoneParams.data('file-status');
  38. this.status = this.dropzoneParams.data('file-init-status');
  39. let previewTemplate = '';
  40. previewTemplate += '<div class="dz-preview dz-file-preview">\n ';
  41. previewTemplate += ' <div class="dz-details">\n ';
  42. previewTemplate += ' <div class="dz-filename">';
  43. previewTemplate +=
  44. ' <span data-dz-name data-dz-thumbnail></span>';
  45. previewTemplate += ' </div>\n ';
  46. previewTemplate += ' <div class="dz-size" data-dz-size></div>\n ';
  47. previewTemplate += ' </div>\n ';
  48. previewTemplate += ' <div class="dz-progress ui active progress">';
  49. previewTemplate +=
  50. ' <div class="dz-upload bar" data-dz-uploadprogress><div class="progress"></div></div>\n ';
  51. previewTemplate += ' </div>\n ';
  52. previewTemplate += ' <div class="dz-success-mark">';
  53. previewTemplate += ' <span>上传成功</span>';
  54. previewTemplate += ' </div>\n ';
  55. previewTemplate += ' <div class="dz-error-mark">';
  56. previewTemplate += ' <span>上传失败</span>';
  57. previewTemplate += ' </div>\n ';
  58. previewTemplate += ' <div class="dz-error-message">';
  59. previewTemplate += ' <span data-dz-errormessage></span>';
  60. previewTemplate += ' </div>\n';
  61. previewTemplate += '</div>';
  62. const $dropzone = $('div#dataset');
  63. console.log('createDropzone');
  64. const dropzoneUploader = await createDropzone($dropzone[0], {
  65. url: '/todouploader',
  66. maxFiles: this.maxFiles,
  67. maxFilesize: this.maxFileSize,
  68. timeout: 0,
  69. autoQueue: false,
  70. dictDefaultMessage: this.dropzoneParams.data('default-message'),
  71. dictInvalidFileType: this.dropzoneParams.data('invalid-input-type'),
  72. dictFileTooBig: this.dropzoneParams.data('file-too-big'),
  73. dictRemoveFile: this.dropzoneParams.data('remove-file'),
  74. previewTemplate
  75. });
  76. dropzoneUploader.on('addedfile', (file) => {
  77. setTimeout(() => {
  78. // eslint-disable-next-line no-unused-expressions
  79. file.accepted && this.onFileAdded(file);
  80. }, 200);
  81. });
  82. dropzoneUploader.on('maxfilesexceeded', function (file) {
  83. if (this.files[0].status !== 'success') {
  84. alert(this.dropzoneParams.data('waitting-uploading'));
  85. this.removeFile(file);
  86. return;
  87. }
  88. this.removeAllFiles();
  89. this.addFile(file);
  90. });
  91. this.dropzoneUploader = dropzoneUploader;
  92. },
  93. methods: {
  94. resetStatus() {
  95. this.progress = 0;
  96. this.status = '';
  97. },
  98. updateProgress(file, progress) {
  99. file.previewTemplate.querySelector(
  100. '.dz-upload'
  101. ).style.width = `${progress}%`;
  102. },
  103. emitDropzoneSuccess(file) {
  104. file.status = 'success';
  105. this.dropzoneUploader.emit('success', file);
  106. this.dropzoneUploader.emit('complete', file);
  107. },
  108. emitDropzoneFailed(file) {
  109. this.status = this.dropzoneParams.data('falied');
  110. file.status = 'error';
  111. this.dropzoneUploader.emit('error', file);
  112. // this.dropzoneUploader.emit('complete', file);
  113. },
  114. onFileAdded(file) {
  115. file.datasetId = document
  116. .getElementById('datasetId')
  117. .getAttribute('datasetId');
  118. this.resetStatus();
  119. this.computeMD5(file);
  120. },
  121. finishUpload(file) {
  122. this.emitDropzoneSuccess(file);
  123. setTimeout(() => {
  124. window.location.reload();
  125. }, 1000);
  126. },
  127. computeMD5(file) {
  128. this.resetStatus();
  129. const blobSlice =
  130. File.prototype.slice ||
  131. File.prototype.mozSlice ||
  132. File.prototype.webkitSlice,
  133. chunkSize = 1024 * 1024 * 64,
  134. chunks = Math.ceil(file.size / chunkSize),
  135. spark = new SparkMD5.ArrayBuffer(),
  136. fileReader = new FileReader();
  137. let currentChunk = 0;
  138. const time = new Date().getTime();
  139. // console.log('计算MD5...')
  140. this.status = this.dropzoneParams.data('md5-computing');
  141. file.totalChunkCounts = chunks;
  142. loadNext();
  143. fileReader.onload = (e) => {
  144. fileLoaded.call(this, e);
  145. };
  146. fileReader.onerror = (err) => {
  147. console.warn('oops, something went wrong.', err);
  148. file.cancel();
  149. };
  150. function fileLoaded(e) {
  151. spark.append(e.target.result); // Append array buffer
  152. currentChunk++;
  153. if (currentChunk < chunks) {
  154. // console.log(`第${currentChunk}分片解析完成, 开始第${currentChunk +1}/${chunks}分片解析`);
  155. this.status = `${this.dropzoneParams.data('loading-file')} ${(
  156. (currentChunk / chunks) *
  157. 100
  158. ).toFixed(2)}% (${currentChunk}/${chunks})`;
  159. this.updateProgress(file, ((currentChunk / chunks) * 100).toFixed(2));
  160. loadNext();
  161. return;
  162. }
  163. const md5 = spark.end();
  164. console.log(
  165. `MD5计算完成:${file.name} \nMD5:${md5} \n分片:${chunks} 大小:${
  166. file.size
  167. } 用时:${(new Date().getTime() - time) / 1000} s`
  168. );
  169. spark.destroy(); // 释放缓存
  170. file.uniqueIdentifier = md5; // 将文件md5赋值给文件唯一标识
  171. file.cmd5 = false; // 取消计算md5状态
  172. this.computeMD5Success(file);
  173. }
  174. function loadNext() {
  175. const start = currentChunk * chunkSize;
  176. const end =
  177. start + chunkSize >= file.size ? file.size : start + chunkSize;
  178. fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
  179. }
  180. },
  181. async computeMD5Success(md5edFile) {
  182. const file = await this.getSuccessChunks(md5edFile);
  183. try {
  184. if (file.uploadID == '' || file.uuid == '') {
  185. // 未上传过
  186. await this.newMultiUpload(file);
  187. if (file.uploadID != '' && file.uuid != '') {
  188. file.chunks = '';
  189. this.multipartUpload(file);
  190. } else {
  191. // 失败如何处理
  192. return;
  193. }
  194. return;
  195. }
  196. if (file.uploaded == '1') {
  197. // 已上传成功
  198. // 秒传
  199. if (file.attachID == '0') {
  200. // 删除数据集记录,未删除文件
  201. await addAttachment(file);
  202. }
  203. //不同数据集上传同一个文件
  204. if (file.datasetID != '') {
  205. if (Number(file.datasetID) != file.datasetId) {
  206. var info = "该文件已上传,对应数据集(" + file.datasetName + ")-文件(" + file.realName + ")";
  207. window.alert(info);
  208. window.location.reload();
  209. }
  210. }
  211. console.log('文件已上传完成');
  212. this.progress = 100;
  213. this.status = this.dropzoneParams.data('upload-complete');
  214. this.finishUpload(file);
  215. } else {
  216. // 断点续传
  217. this.multipartUpload(file);
  218. }
  219. } catch (error) {
  220. this.emitDropzoneFailed(file);
  221. console.log(error);
  222. }
  223. async function addAttachment(file) {
  224. return await axios.post(
  225. '/attachments/add',
  226. qs.stringify({
  227. uuid: file.uuid,
  228. file_name: file.name,
  229. size: file.size,
  230. dataset_id: file.datasetId,
  231. _csrf: csrf
  232. })
  233. );
  234. }
  235. },
  236. async getSuccessChunks(file) {
  237. const params = {
  238. params: {
  239. md5: file.uniqueIdentifier,
  240. _csrf: csrf
  241. }
  242. };
  243. try {
  244. const response = await axios.get('/attachments/get_chunks', params);
  245. file.uploadID = response.data.uploadID;
  246. file.uuid = response.data.uuid;
  247. file.uploaded = response.data.uploaded;
  248. file.chunks = response.data.chunks;
  249. file.attachID = response.data.attachID;
  250. file.datasetID = response.data.datasetID;
  251. file.datasetName = response.data.datasetName;
  252. file.realName = response.data.fileName;
  253. return file;
  254. } catch (error) {
  255. this.emitDropzoneFailed(file);
  256. console.log('getSuccessChunks catch: ', error);
  257. return null;
  258. }
  259. },
  260. async newMultiUpload(file) {
  261. const res = await axios.get('/attachments/new_multipart', {
  262. params: {
  263. totalChunkCounts: file.totalChunkCounts,
  264. md5: file.uniqueIdentifier,
  265. size: file.size,
  266. fileType: file.type,
  267. _csrf: csrf
  268. }
  269. });
  270. file.uploadID = res.data.uploadID;
  271. file.uuid = res.data.uuid;
  272. },
  273. multipartUpload(file) {
  274. const blobSlice =
  275. File.prototype.slice ||
  276. File.prototype.mozSlice ||
  277. File.prototype.webkitSlice,
  278. chunkSize = 1024 * 1024 * 64,
  279. chunks = Math.ceil(file.size / chunkSize),
  280. fileReader = new FileReader(),
  281. time = new Date().getTime();
  282. let currentChunk = 0;
  283. function loadNext() {
  284. const start = currentChunk * chunkSize;
  285. const end =
  286. start + chunkSize >= file.size ? file.size : start + chunkSize;
  287. fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
  288. }
  289. function checkSuccessChunks() {
  290. const index = successChunks.indexOf((currentChunk + 1).toString());
  291. if (index == -1) {
  292. return false;
  293. }
  294. return true;
  295. }
  296. async function getUploadChunkUrl(currentChunk, partSize) {
  297. const res = await axios.get('/attachments/get_multipart_url', {
  298. params: {
  299. uuid: file.uuid,
  300. uploadID: file.uploadID,
  301. size: partSize,
  302. chunkNumber: currentChunk + 1,
  303. _csrf: csrf
  304. }
  305. });
  306. urls[currentChunk] = res.data.url;
  307. }
  308. async function uploadMinio(url, e) {
  309. const res = await axios.put(url, e.target.result);
  310. etags[currentChunk] = res.headers.etag;
  311. }
  312. async function updateChunk(currentChunk) {
  313. await axios.post(
  314. '/attachments/update_chunk',
  315. qs.stringify({
  316. uuid: file.uuid,
  317. chunkNumber: currentChunk + 1,
  318. etag: etags[currentChunk],
  319. _csrf: csrf
  320. })
  321. );
  322. }
  323. async function uploadChunk(e) {
  324. try {
  325. if (!checkSuccessChunks()) {
  326. const start = currentChunk * chunkSize;
  327. const partSize =
  328. start + chunkSize >= file.size ? file.size - start : chunkSize;
  329. // 获取分片上传url
  330. await getUploadChunkUrl(currentChunk, partSize);
  331. if (urls[currentChunk] != '') {
  332. // 上传到minio
  333. await uploadMinio(urls[currentChunk], e);
  334. if (etags[currentChunk] != '') {
  335. // 更新数据库:分片上传结果
  336. //await updateChunk(currentChunk);
  337. } else {
  338. console.log("上传到minio uploadChunk etags[currentChunk] == ''");// TODO
  339. }
  340. } else {
  341. console.log("uploadChunk urls[currentChunk] != ''");// TODO
  342. }
  343. }
  344. } catch (error) {
  345. this.emitDropzoneFailed(file);
  346. console.log(error);
  347. }
  348. }
  349. async function completeUpload() {
  350. return await axios.post(
  351. '/attachments/complete_multipart',
  352. qs.stringify({
  353. uuid: file.uuid,
  354. uploadID: file.uploadID,
  355. file_name: file.name,
  356. size: file.size,
  357. dataset_id: file.datasetId,
  358. _csrf: csrf
  359. })
  360. );
  361. }
  362. const successChunks = [];
  363. let successParts = [];
  364. successParts = file.chunks.split(',');
  365. for (let i = 0; i < successParts.length; i++) {
  366. successChunks[i] = successParts[i].split('-')[0];
  367. }
  368. const urls = []; // TODO const ?
  369. const etags = [];
  370. console.log('上传分片...');
  371. this.status = this.dropzoneParams.data('uploading');
  372. loadNext();
  373. fileReader.onload = async (e) => {
  374. await uploadChunk(e);
  375. fileReader.abort();
  376. currentChunk++;
  377. if (currentChunk < chunks) {
  378. console.log(
  379. `第${currentChunk}个分片上传完成, 开始第${currentChunk +
  380. 1}/${chunks}个分片上传`
  381. );
  382. this.progress = Math.ceil((currentChunk / chunks) * 100);
  383. this.updateProgress(file, ((currentChunk / chunks) * 100).toFixed(2));
  384. this.status = `${this.dropzoneParams.data('uploading')} ${(
  385. (currentChunk / chunks) *
  386. 100
  387. ).toFixed(2)}%`;
  388. await loadNext();
  389. } else {
  390. await completeUpload();
  391. console.log(
  392. `文件上传完成:${file.name} \n分片:${chunks} 大小:${
  393. file.size
  394. } 用时:${(new Date().getTime() - time) / 1000} s`
  395. );
  396. this.progress = 100;
  397. this.status = this.dropzoneParams.data('upload-complete');
  398. this.finishUpload(file);
  399. }
  400. };
  401. }
  402. }
  403. };
  404. </script>
  405. <style>
  406. .dropzone-wrapper {
  407. margin: 2em auto;
  408. }
  409. .ui .dropzone {
  410. border: 2px dashed #0087f5;
  411. box-shadow: none !important;
  412. padding: 0;
  413. min-height: 5rem;
  414. border-radius: 4px;
  415. }
  416. .dataset .dataset-files #dataset .dz-preview.dz-file-preview,
  417. .dataset .dataset-files #dataset .dz-preview.dz-processing {
  418. display: flex;
  419. align-items: center;
  420. }
  421. .dataset .dataset-files #dataset .dz-preview {
  422. border-bottom: 1px solid #dadce0;
  423. min-height: 0;
  424. }
  425. </style>