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

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