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