1export default class MultiPartUploader {
2async upload() {
3const promises = this.uploadParams.contentHubUploadUrlRepresentations
4.map(uploadUrlRep => this.uploadFileInParts(uploadUrlRep));
5await Promise.all(promises);
6// The upload completion process will be discussed in the next step
7this.onComplete(this.uploadParts);
8}
9
10uploadFileInParts(uploadUrlRep) {
11// Get the chunk for the part
12const start = (uploadUrlRep.partNumber - 1) * this.uploadParams.partSize;
13const end = Math.min(start + this.uploadParams.partSize, this.file.size);
14const chunk = this.file.slice(start, end);
15
16// Upload the chunk using fetch
17const uploadUrl = uploadUrlRep.partUrl.replaceAll('&', '&');
18const response = await fetch(uploadUrl, {
19method: 'PUT',
20body: chunk,
21headers: {
22'Content-Type': this.file.type
23}
24});
25
26if (response.status === 200) {
27// The upload completion process will be discussed in the next step
28this.uploadParts.push({
29partNumber: uploadUrlRep.partNumber,
30eTag: response.headers.get('ETag'),
31});
32}
33else{
34//Handle the error here
35reject();
36}
37
38resolve();
39}
40}