minio分片上传

This commit is contained in:
none
2023-03-06 18:22:04 +08:00
parent 6649aa8cfa
commit 709027df50
3 changed files with 112 additions and 45 deletions

View File

@@ -0,0 +1,80 @@
import axios, { Axios } from "axios";
import { minioMerge, minioPreSignUrl } from "../api/upload";
export class UploadChunk {
client: Axios;
file: File;
progress: number;
chunkNumber: number;
isStop: boolean;
chunkSize: number;
chunkIndex: number;
uploadId: string;
filename: string;
onError: ((err: string) => void | undefined) | undefined;
onSuccess: (() => void | undefined) | undefined;
onProgress: ((progress: number) => void) | undefined;
constructor(file: File, uploadId: string, filename: string) {
this.client = axios.create({
timeout: 15000,
withCredentials: false,
});
this.file = file;
this.progress = 0;
this.isStop = false;
this.chunkIndex = 1;
this.chunkSize = 6 * 1024 * 1024;
this.chunkNumber = Math.ceil(file.size / this.chunkSize);
this.uploadId = uploadId;
this.filename = filename;
}
start() {
if (this.isStop) {
return;
}
let start = (this.chunkIndex - 1) * this.chunkSize;
if (start > this.file.size) {
//上传完成
minioMerge(this.filename, this.uploadId)
.then((res) => {
console.log("合并成功", res);
})
.catch((e) => {
console.error("合并失败", e);
});
return;
}
const chunkData = this.file.slice(start, start + this.chunkSize);
const boolname = this.file.name + "-" + this.chunkIndex;
const tmpFile = new File([chunkData], boolname);
minioPreSignUrl(this.uploadId, this.filename, this.chunkIndex)
.then((res: any) => {
return this.client.put(res.data.url, tmpFile, {
headers: {
"Content-Type": "multipart/form-data",
},
});
})
.then(() => {
this.chunkIndex += 1;
this.start();
})
.catch((e) => {
console.error("获取签名url失败", e);
});
}
stop() {
this.isStop = true;
}
resume() {
this.isStop = false;
this.start();
}
}