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 24 kB

4 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
4 years ago
2 years ago
2 years ago
3 years ago
3 years ago
4 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
4 years ago
2 years ago
3 years ago
2 years ago
3 years ago
4 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
4 years ago
2 years ago
4 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
4 years ago
2 years ago
2 years ago
2 years ago
2 years ago
4 years ago
4 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
4 years ago
4 years ago
2 years ago
4 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
4 years ago
2 years ago
3 years ago
4 years ago
4 years ago
4 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <template>
  2. <div class="dropzone-wrapper dataset-files">
  3. <div id="dataset" class="dropzone">
  4. <div
  5. class="maxfilesize ui red message"
  6. style="display: none; margin: 2.5rem"
  7. ></div>
  8. </div>
  9. <el-button
  10. style="background-color: #21ba45; margin-top: 2rem"
  11. type="success"
  12. :disabled="btnFlag"
  13. @click="startUpload"
  14. >{{ upload }}</el-button
  15. >
  16. <el-button type="info" @click="cancelDataset">{{ cancel }}</el-button>
  17. <div style="margin-top: 2rem; position: relative">
  18. <label
  19. class="el-form-item__label"
  20. style="width: 140px; position: absolute; left: -140px"
  21. >{{ upload_status }}:</label
  22. >
  23. <div v-for="(item, index) in uploadFiles" class="datast-upload-progress">
  24. <span class="dataset-name nowrap" :title="item.name">{{
  25. item.name
  26. }}</span>
  27. <div class="dataset-progress">
  28. <el-progress
  29. :text-inside="true"
  30. :stroke-width="14"
  31. :percentage="uploadProgressList[index].progress"
  32. ></el-progress>
  33. </div>
  34. <div class="dataset-status nowrap">
  35. <div class="status-flex">
  36. <i
  37. v-if="
  38. uploadProgressList[index].infoCode === 1 ||
  39. uploadProgressList[index].infoCode === 2
  40. "
  41. class="ri-close-circle-line failed"
  42. ></i>
  43. <i
  44. v-if="uploadProgressList[index].infoCode === 0"
  45. class="ri-checkbox-circle-line success"
  46. >
  47. </i>
  48. <span>{{ uploadProgressList[index].status }}</span>
  49. <el-tooltip
  50. v-if="uploadProgressList[index].infoCode === 1"
  51. class="item"
  52. effect="dark"
  53. placement="top"
  54. >
  55. <div slot="content">
  56. {{ uploadProgressList[index].failedInfo }}
  57. </div>
  58. <i
  59. style="font-size: 16px; margin-left: 0.5rem; cursor: pointer"
  60. class="ri-question-fill"
  61. ></i>
  62. </el-tooltip>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. </template>
  69. <script>
  70. import SparkMD5 from "spark-md5";
  71. import axios from "axios";
  72. import qs from "qs";
  73. import createDropzone from "../features/dropzone.js";
  74. const { _AppSubUrl, _StaticUrlPrefix, csrf } = window.config;
  75. const chunkSize = 1024 * 1024 * 64;
  76. const md5ChunkSize = 1024 * 1024 * 1;
  77. export default {
  78. props: {
  79. uploadtype: {
  80. type: Number,
  81. required: true,
  82. },
  83. desc: {
  84. type: String,
  85. default: "",
  86. },
  87. },
  88. data() {
  89. return {
  90. dropzoneUploader: null,
  91. maxFiles: 10,
  92. maxFilesize: 200,
  93. acceptedFiles: "*/*",
  94. progress: 0,
  95. status: "",
  96. dropzoneParams: {},
  97. file_status_text: "",
  98. file: {},
  99. repoPath: "",
  100. btnFlag: false,
  101. cancel: "",
  102. upload: "",
  103. upload_status: "",
  104. uploadFiles: [],
  105. uploadFilesAddId: [],
  106. // allUploadFiles: [],
  107. uploadLength: 0,
  108. allUploadLength: 0,
  109. uploadProgressList: [],
  110. };
  111. },
  112. async mounted() {
  113. this.dropzoneParams = $("div#minioUploader-params");
  114. this.file_status_text = this.dropzoneParams.data("file-status");
  115. this.status = this.dropzoneParams.data("file-init-status");
  116. this.repoPath = this.dropzoneParams.data("repopath");
  117. this.cancel = this.dropzoneParams.data("cancel");
  118. this.upload = this.dropzoneParams.data("upload");
  119. this.upload_status = this.dropzoneParams.data("upload-status");
  120. let previewTemplate = `
  121. <div class="dz-preview dz-file-preview">
  122. <div class="dz-image">
  123. <img data-dz-thumbnail />
  124. </div>
  125. <div class="dz-details">
  126. <div class="dz-size"><span data-dz-size></span></div>
  127. <div class="dz-filename"><span data-dz-name></span></div>
  128. </div>
  129. <div style="opacity:0" class="dz-progress"><span class="dz-upload" data-dz-uploadprogress></span></div>
  130. <div class="dz-error-message" style="line-height: 1.5;"><span data-dz-errormessage></span></div>
  131. <div class="dz-success-mark"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="54" height="54"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10-4.477 10-10 10zm0-2a8 8 0 1 0 0-16 8 8 0 0 0 0 16zm-.997-4L6.76 11.757l1.414-1.414 2.829 2.829 5.656-5.657 1.415 1.414L11.003 16z" fill="rgba(47,204,113,1)"/></svg></div>
  132. <div class="dz-error-mark"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="54" height="54"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10-4.477 10-10 10zm0-2a8 8 0 1 0 0-16 8 8 0 0 0 0 16zm0-9.414l2.828-2.829 1.415 1.415L13.414 12l2.829 2.828-1.415 1.415L12 13.414l-2.828 2.829-1.415-1.415L10.586 12 7.757 9.172l1.415-1.415L12 10.586z" fill="rgba(231,76,60,1)"/></svg></div>
  133. </div> `;
  134. const $dropzone = $("div#dataset");
  135. const dropzoneUploader = await createDropzone($dropzone[0], {
  136. url: "/todouploader",
  137. maxFiles: this.maxFiles,
  138. maxFilesize: 1024 * 200,
  139. filesizeBase: 1024,
  140. parallelUploads: this.maxFiles,
  141. timeout: 0,
  142. addRemoveLinks: true,
  143. // autoQueue: false,
  144. autoProcessQueue: false, //自动上传
  145. dictDefaultMessage: this.dropzoneParams.data("default-message"),
  146. dictInvalidFileType: this.dropzoneParams.data("invalid-input-type"),
  147. dictFileTooBig: this.dropzoneParams.data("file-too-big"),
  148. dictRemoveFile: this.dropzoneParams.data("remove-file"),
  149. previewTemplate: previewTemplate,
  150. });
  151. dropzoneUploader.on("addedfile", (file) => {
  152. if (file.size / (1024 * 1024) > dropzoneUploader.options.maxFilesize) {
  153. dropzoneUploader.removeFile(file);
  154. $(".maxfilesize.ui.red.message").text(
  155. "单次最多上传10个文件,单个文件不超过200G"
  156. );
  157. $(".maxfilesize.ui.red.message").css("display", "block");
  158. } else {
  159. this.file = file;
  160. $(".maxfilesize.ui.red.message").css("display", "none");
  161. }
  162. });
  163. dropzoneUploader.on("removedfile", (file) => {
  164. $(".maxfilesize.ui.red.message").css("display", "none");
  165. });
  166. dropzoneUploader.on("maxfilesexceeded", function (file) {
  167. dropzoneUploader.removeFile(file);
  168. $(".maxfilesize.ui.red.message").text(
  169. "单次最多上传10个文件,单个文件不超过200G"
  170. );
  171. $(".maxfilesize.ui.red.message").css("display", "block");
  172. });
  173. this.dropzoneUploader = dropzoneUploader;
  174. },
  175. watch: {
  176. allUploadLength(len) {
  177. if (len === this.uploadFiles.length) {
  178. setTimeout(() => {
  179. this.dropzoneUploader.removeAllFiles(true);
  180. this.btnFlag = false;
  181. this.$emit("setcluster", this.btnFlag);
  182. }, 2000);
  183. }
  184. },
  185. },
  186. methods: {
  187. startUpload() {
  188. this.uploadFiles = this.dropzoneUploader.getQueuedFiles();
  189. if (this.uploadFiles.length === 0) {
  190. return;
  191. }
  192. this.resetStatus();
  193. $(".dz-remove").remove();
  194. $(".maxfilesize.ui.red.message").css("display", "none");
  195. this.btnFlag = true;
  196. this.$emit("setcluster", this.btnFlag);
  197. this.uploadFiles.forEach((element) => {
  198. element.datasetId = document
  199. .getElementById("datasetId")
  200. .getAttribute("datasetId");
  201. this.computeMD5(element);
  202. });
  203. },
  204. cancelDataset() {
  205. location.href = this.repoPath;
  206. this.dropzoneUploader.removeAllFiles(true);
  207. },
  208. resetStatus() {
  209. this.uploadLength = 0;
  210. this.allUploadLength = 0;
  211. // this.allUploadFiles = [];
  212. this.uploadProgressList = [];
  213. },
  214. updateProgress(file, status, progress, infoCode, failedInfo = "") {
  215. console.log("this.uploadProgressList:", this.uploadProgressList);
  216. // file.previewTemplate.querySelector(
  217. // ".dz-upload"
  218. // ).style.width = `${progress}%`;
  219. // file.previewTemplate.querySelector(".dz-upload").style.background =
  220. // "#409eff";
  221. // file.previewTemplate.querySelector(".dz-upload").style.opacity = 0;
  222. this.uploadProgressList.forEach((item, index) => {
  223. if (item.name === file.name) {
  224. this.uploadProgressList[index].status = status;
  225. this.uploadProgressList[index].progress = progress;
  226. this.uploadProgressList[index].infoCode = infoCode;
  227. this.uploadProgressList[index].failedInfo = failedInfo;
  228. }
  229. });
  230. },
  231. uploadError(file, info) {
  232. file.previewTemplate.querySelector(".dz-error-mark").style.opacity = 1;
  233. file.previewTemplate.querySelector(".dz-progress").style.opacity = 0;
  234. file.previewTemplate.querySelector(".dz-error-message span").innerHTML =
  235. info;
  236. file.previewTemplate.querySelector(".dz-error-message").style.display =
  237. "block";
  238. file.previewTemplate.querySelector(".dz-details").onmouseover =
  239. function () {
  240. file.previewTemplate.querySelector(
  241. ".dz-error-message"
  242. ).style.opacity = 1;
  243. };
  244. file.previewTemplate.querySelector(".dz-details").onmouseout =
  245. function () {
  246. file.previewTemplate.querySelector(
  247. ".dz-error-message"
  248. ).style.opacity = 0;
  249. };
  250. },
  251. emitDropzoneSuccess(file) {
  252. file.status = "success";
  253. this.dropzoneUploader.emit("success", file);
  254. this.dropzoneUploader.emit("complete", file);
  255. },
  256. emitDropzoneFailed(file) {
  257. this.status = this.dropzoneParams.data("falied");
  258. file.status = "error";
  259. this.dropzoneUploader.emit("error", file);
  260. // this.dropzoneUploader.emit('complete', file);
  261. },
  262. finishUpload(file) {
  263. console.log("finish", file);
  264. file.previewTemplate.querySelector(".dz-success-mark").style.opacity = 1;
  265. file.previewTemplate.querySelector(".dz-progress").style.opacity = 0;
  266. if (this.uploadLength === this.uploadFiles.length) {
  267. setTimeout(() => {
  268. window.location.href = this.repoPath;
  269. }, 1000);
  270. }
  271. },
  272. computeMD5(file) {
  273. const blobSlice =
  274. File.prototype.slice ||
  275. File.prototype.mozSlice ||
  276. File.prototype.webkitSlice,
  277. chunks = Math.ceil(file.size / chunkSize),
  278. spark = new SparkMD5.ArrayBuffer(),
  279. fileReader = new FileReader();
  280. let currentChunk = 0;
  281. const time = new Date().getTime();
  282. this.status = this.dropzoneParams.data("md5-computing");
  283. this.uploadProgressList.push({
  284. name: file.name,
  285. status: this.dropzoneParams.data("md5-computing"),
  286. progress: 0,
  287. infoCode: 3,
  288. });
  289. file.totalChunkCounts = chunks;
  290. if (file.size == 0) {
  291. file.totalChunkCounts = 1;
  292. }
  293. loadMd5Next();
  294. fileReader.onload = (e) => {
  295. fileLoaded.call(this, e);
  296. };
  297. fileReader.onerror = (err) => {
  298. console.warn("oops, something went wrong.", err);
  299. file.cancel();
  300. };
  301. function fileLoaded(e) {
  302. spark.append(e.target.result); // Append array buffer
  303. currentChunk++;
  304. if (currentChunk < chunks) {
  305. this.status = `${this.dropzoneParams.data("loading-file")} ${(
  306. (currentChunk / chunks) *
  307. 100
  308. ).toFixed(2)}% (${currentChunk}/${chunks})`;
  309. console.log("-=-=-=-=--=", this.status);
  310. // this.updateProgress(
  311. // file,
  312. // this.dropzoneParams.data("md5-computing"),
  313. // Number(((currentChunk / chunks) * 100).toFixed(1))
  314. // );
  315. loadMd5Next();
  316. return;
  317. }
  318. const md5 = spark.end();
  319. console.log(
  320. `MD5计算完成:${file.name} \nMD5:${md5} \n分片:${chunks} 大小:${
  321. file.size
  322. } 用时:${(new Date().getTime() - time) / 1000} s`
  323. );
  324. // this.updateProgress(
  325. // file,
  326. // this.dropzoneParams.data("md5-computing"),
  327. // 100
  328. // );
  329. spark.destroy(); // 释放缓存
  330. file.uniqueIdentifier = md5; // 将文件md5赋值给文件唯一标识
  331. file.cmd5 = false; // 取消计算md5状态
  332. this.computeMD5Success(file);
  333. }
  334. function loadNext() {
  335. const start = currentChunk * chunkSize;
  336. const end =
  337. start + chunkSize >= file.size ? file.size : start + chunkSize;
  338. fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
  339. }
  340. function loadMd5Next() {
  341. const start = currentChunk * chunkSize;
  342. const end =
  343. start + md5ChunkSize >= file.size ? file.size : start + md5ChunkSize;
  344. fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
  345. }
  346. },
  347. async computeMD5Success(md5edFile) {
  348. const file = await this.getSuccessChunks(md5edFile);
  349. console.log(file);
  350. try {
  351. if (file.uploadID == "" || file.uuid == "") {
  352. // 未上传过
  353. await this.newMultiUpload(file);
  354. if (file.uploadID != "" && file.uuid != "") {
  355. file.chunks = "";
  356. this.multipartUpload(file);
  357. } else {
  358. // 失败如何处理
  359. let info = "上传失败";
  360. this.allUploadLength++;
  361. this.uploadError(file, info);
  362. this.updateProgress(file, "上传失败", 0, 2);
  363. // this.allUploadFiles.push({
  364. // name: file.name,
  365. // status: 2,
  366. // info: info,
  367. // });
  368. return;
  369. }
  370. return;
  371. }
  372. if (file.uploaded == "1") {
  373. // 已上传成功
  374. // 秒传
  375. if (file.attachID == "0") {
  376. // 删除数据集记录,未删除文件
  377. await addAttachment(file);
  378. }
  379. //不同数据集上传同一个文件
  380. if (file.datasetID != "") {
  381. if (file.datasetName != "" && file.realName != "") {
  382. let info = `该文件已上传在数据集: ${file.datasetName}`;
  383. this.uploadError(file, info);
  384. this.allUploadLength++;
  385. this.updateProgress(file, "上传失败", 0, 1, info);
  386. // this.allUploadFiles.push({
  387. // name: file.name,
  388. // status: 1,
  389. // info: info,
  390. // });
  391. }
  392. }
  393. console.log("文件已上传完成");
  394. this.progress = 100;
  395. this.status = this.dropzoneParams.data("upload-complete");
  396. // this.finishUpload(file);
  397. } else {
  398. // 断点续传
  399. this.multipartUpload(file);
  400. }
  401. } catch (error) {
  402. this.emitDropzoneFailed(file);
  403. console.log(error);
  404. }
  405. async function addAttachment(file) {
  406. return await axios.post(
  407. "/attachments/add",
  408. qs.stringify({
  409. uuid: file.uuid,
  410. file_name: file.name,
  411. size: file.size,
  412. dataset_id: file.datasetId,
  413. type: this.uploadtype,
  414. _csrf: csrf,
  415. })
  416. );
  417. }
  418. },
  419. async getSuccessChunks(file) {
  420. const params = {
  421. params: {
  422. md5: file.uniqueIdentifier,
  423. type: this.uploadtype,
  424. file_name: file.name,
  425. _csrf: csrf,
  426. },
  427. };
  428. try {
  429. const response = await axios.get("/attachments/get_chunks", params);
  430. file.uploadID = response.data.uploadID;
  431. file.uuid = response.data.uuid;
  432. file.uploaded = response.data.uploaded;
  433. file.chunks = response.data.chunks;
  434. file.attachID = response.data.attachID;
  435. file.datasetID = response.data.datasetID;
  436. file.datasetName = response.data.datasetName;
  437. file.realName = response.data.fileName;
  438. return file;
  439. } catch (error) {
  440. this.emitDropzoneFailed(file);
  441. console.log("getSuccessChunks catch: ", error);
  442. return null;
  443. }
  444. },
  445. async newMultiUpload(file) {
  446. const res = await axios.get("/attachments/new_multipart", {
  447. params: {
  448. totalChunkCounts: file.totalChunkCounts,
  449. md5: file.uniqueIdentifier,
  450. size: file.size,
  451. fileType: file.type,
  452. type: this.uploadtype,
  453. file_name: file.name,
  454. _csrf: csrf,
  455. },
  456. });
  457. file.uploadID = res.data.uploadID;
  458. file.uuid = res.data.uuid;
  459. },
  460. multipartUpload(file) {
  461. const blobSlice =
  462. File.prototype.slice ||
  463. File.prototype.mozSlice ||
  464. File.prototype.webkitSlice,
  465. chunks = Math.ceil(file.size / chunkSize),
  466. fileReader = new FileReader(),
  467. time = new Date().getTime();
  468. let currentChunk = 0;
  469. let _this = this;
  470. function loadNext() {
  471. const start = currentChunk * chunkSize;
  472. const end =
  473. start + chunkSize >= file.size ? file.size : start + chunkSize;
  474. fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
  475. }
  476. function checkSuccessChunks() {
  477. const index = successChunks.indexOf((currentChunk + 1).toString());
  478. if (index == -1) {
  479. return false;
  480. }
  481. return true;
  482. }
  483. async function getUploadChunkUrl(currentChunk, partSize) {
  484. const res = await axios.get("/attachments/get_multipart_url", {
  485. params: {
  486. uuid: file.uuid,
  487. uploadID: file.uploadID,
  488. size: partSize,
  489. chunkNumber: currentChunk + 1,
  490. type: _this.uploadtype,
  491. file_name: file.name,
  492. _csrf: csrf,
  493. },
  494. });
  495. urls[currentChunk] = res.data.url;
  496. }
  497. async function uploadMinio(url, e) {
  498. const res = await axios.put(url, e.target.result);
  499. delete e.target.result;
  500. etags[currentChunk] = res.headers.etag;
  501. }
  502. async function uploadMinioNewMethod(url, e) {
  503. var xhr = new XMLHttpRequest();
  504. xhr.open("PUT", url, false);
  505. if (_this.uploadtype === 0) {
  506. xhr.setRequestHeader("Content-Type", "text/plain");
  507. xhr.send(e.target.result);
  508. var etagValue = xhr.getResponseHeader("etag");
  509. etags[currentChunk] = etagValue;
  510. } else if (_this.uploadtype === 1) {
  511. xhr.setRequestHeader("Content-Type", "");
  512. xhr.send(e.target.result);
  513. var etagValue = xhr.getResponseHeader("ETag");
  514. //console.log(etagValue);
  515. etags[currentChunk] = etagValue;
  516. }
  517. }
  518. async function updateChunk(currentChunk) {
  519. await axios.post(
  520. "/attachments/update_chunk",
  521. qs.stringify({
  522. uuid: file.uuid,
  523. chunkNumber: currentChunk + 1,
  524. etag: etags[currentChunk],
  525. _csrf: csrf,
  526. })
  527. );
  528. }
  529. async function uploadChunk(e) {
  530. try {
  531. if (!checkSuccessChunks()) {
  532. const start = currentChunk * chunkSize;
  533. const partSize =
  534. start + chunkSize >= file.size ? file.size - start : chunkSize;
  535. // 获取分片上传url
  536. await getUploadChunkUrl(currentChunk, partSize);
  537. if (urls[currentChunk] != "") {
  538. // 上传到minio
  539. //await uploadMinio(urls[currentChunk], e);
  540. await uploadMinioNewMethod(urls[currentChunk], e);
  541. if (etags[currentChunk] != "") {
  542. // 更新数据库:分片上传结果
  543. //await updateChunk(currentChunk);
  544. } else {
  545. console.log(
  546. "上传到minio uploadChunk etags[currentChunk] == ''"
  547. ); // TODO
  548. }
  549. } else {
  550. console.log("uploadChunk urls[currentChunk] != ''"); // TODO
  551. }
  552. }
  553. } catch (error) {
  554. // this.emitDropzoneFailed(file);
  555. console.log(error);
  556. }
  557. }
  558. async function completeUpload() {
  559. return await axios.post(
  560. "/attachments/complete_multipart",
  561. qs.stringify({
  562. uuid: file.uuid,
  563. uploadID: file.uploadID,
  564. file_name: file.name,
  565. size: file.size,
  566. dataset_id: file.datasetId,
  567. type: _this.uploadtype,
  568. _csrf: csrf,
  569. description: _this.desc,
  570. })
  571. );
  572. }
  573. const successChunks = [];
  574. let successParts = [];
  575. successParts = file.chunks.split(",");
  576. for (let i = 0; i < successParts.length; i++) {
  577. successChunks[i] = successParts[i].split("-")[0];
  578. }
  579. const urls = []; // TODO const ?
  580. const etags = [];
  581. console.log("上传分片...");
  582. this.status = this.dropzoneParams.data("uploading");
  583. loadNext();
  584. fileReader.onload = async (e) => {
  585. try {
  586. await uploadChunk(e);
  587. } catch (err) {
  588. console.log(err);
  589. }
  590. fileReader.abort();
  591. currentChunk++;
  592. if (currentChunk < chunks) {
  593. console.log(
  594. `第${currentChunk}个分片上传完成, 开始第${
  595. currentChunk + 1
  596. }/${chunks}个分片上传`
  597. );
  598. this.progress = Math.ceil((currentChunk / chunks) * 100);
  599. this.updateProgress(
  600. file,
  601. this.dropzoneParams.data("uploading"),
  602. Number(((currentChunk / chunks) * 100).toFixed(2)),
  603. 3
  604. );
  605. this.status = `${this.dropzoneParams.data("uploading")} ${(
  606. (currentChunk / chunks) *
  607. 100
  608. ).toFixed(2)}%`;
  609. await loadNext();
  610. } else {
  611. try {
  612. await completeUpload();
  613. } catch (err) {
  614. let info = "上传失败";
  615. this.allUploadLength++;
  616. this.uploadError(file, info);
  617. this.updateProgress(
  618. file,
  619. info,
  620. Number(((currentChunk / chunks) * 100).toFixed(2)) - 1,
  621. 2
  622. );
  623. // this.allUploadFiles.push({
  624. // name: file.name,
  625. // status: 2,
  626. // info: info,
  627. // });
  628. if (err) {
  629. return;
  630. }
  631. }
  632. console.log(
  633. `文件上传完成:${file.name} \n分片:${chunks} 大小:${
  634. file.size
  635. } 用时:${(new Date().getTime() - time) / 1000} s`
  636. );
  637. this.uploadLength++;
  638. this.allUploadLength++;
  639. // this.allUploadFiles.push({
  640. // name: file.name,
  641. // status: 0,
  642. // info: "上传成功",
  643. // });
  644. this.updateProgress(file, "上传成功", 100, 0);
  645. this.progress = 100;
  646. this.status = this.dropzoneParams.data("upload-complete");
  647. this.finishUpload(file);
  648. }
  649. };
  650. },
  651. },
  652. };
  653. </script>
  654. <style scoped>
  655. .dropzone-wrapper {
  656. margin: 0;
  657. }
  658. .ui .dropzone {
  659. border: 2px dashed #0087f5;
  660. box-shadow: none !important;
  661. padding: 0;
  662. min-height: 5rem;
  663. border-radius: 4px;
  664. }
  665. .dataset .dataset-files #dataset .dz-preview.dz-file-preview,
  666. .dataset .dataset-files #dataset .dz-preview.dz-processing {
  667. display: flex;
  668. align-items: center;
  669. }
  670. .dataset .dataset-files #dataset .dz-preview {
  671. border-bottom: 1px solid #dadce0;
  672. min-height: 0;
  673. }
  674. .upload-info {
  675. margin-top: 1em;
  676. margin-bottom: 3em;
  677. }
  678. .success {
  679. color: #21ba45;
  680. font-size: 16px;
  681. margin-right: 0.5rem;
  682. }
  683. .failed {
  684. color: red;
  685. font-size: 16px;
  686. margin-right: 0.5rem;
  687. }
  688. .datast-upload-progress {
  689. display: flex;
  690. align-items: center;
  691. }
  692. .datast-upload-progress .dataset-name {
  693. text-align: right;
  694. width: 200px;
  695. margin-right: 1rem;
  696. }
  697. .datast-upload-progress .dataset-progress {
  698. flex: 1;
  699. }
  700. .datast-upload-progress .dataset-status {
  701. width: 100px;
  702. margin-left: 1rem;
  703. }
  704. .datast-upload-progress .dataset-status .status-flex {
  705. display: flex;
  706. align-items: center;
  707. }
  708. /deep/ .el-progress-bar__inner {
  709. background-color: #21ba45;
  710. }
  711. </style>