Skip to content

Commit d503e72

Browse files
committed
frobidden header and progress bar fixes
1 parent 49b2f23 commit d503e72

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

src/files/infra/clients/DirectUploadClient.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ export class DirectUploadClient implements IDirectUploadClient {
1919
private filesRepository: IFilesRepository
2020
private maxMultipartRetries: number
2121

22-
private readonly progressAfterUrlGeneration: number = 10
23-
private readonly progressAfterFileUpload: number = 100
24-
2522
private readonly fileUploadTimeoutMs: number = 60_000
2623

2724
constructor(filesRepository: IFilesRepository, maxMultipartRetries = 5) {
@@ -43,14 +40,12 @@ export class DirectUploadClient implements IDirectUploadClient {
4340
throw new UrlGenerationError(file.name, datasetId, error.message)
4441
})
4542
}
46-
progress(this.progressAfterUrlGeneration)
4743

4844
if (destination.urls.length === 1) {
49-
await this.uploadSinglepartFile(datasetId, file, destination, abortController)
45+
await this.uploadSinglepartFile(datasetId, file, destination, progress, abortController)
5046
} else {
5147
await this.uploadMultipartFile(datasetId, file, destination, progress, abortController)
5248
}
53-
progress(this.progressAfterFileUpload)
5449

5550
return destination.storageId
5651
}
@@ -59,18 +54,20 @@ export class DirectUploadClient implements IDirectUploadClient {
5954
datasetId: number | string,
6055
file: File,
6156
destination: FileUploadDestination,
57+
progress: (now: number) => void,
6258
abortController: AbortController
6359
): Promise<void> {
6460
try {
6561
const arrayBuffer = await file.arrayBuffer()
6662
await axios.put(destination.urls[0], arrayBuffer, {
6763
headers: {
6864
'Content-Type': 'application/octet-stream',
69-
'Content-Length': file.size.toString(),
7065
'x-amz-tagging': 'dv-state=temp'
7166
},
7267
timeout: this.fileUploadTimeoutMs,
73-
signal: abortController.signal
68+
signal: abortController.signal,
69+
onUploadProgress: (progressEvent) =>
70+
progress(Math.round((progressEvent.loaded * 100) / file.size))
7471
})
7572
} catch (error) {
7673
if (axios.isCancel(error)) {
@@ -92,8 +89,6 @@ export class DirectUploadClient implements IDirectUploadClient {
9289
const maxRetries = this.maxMultipartRetries
9390
const limitConcurrency = pLimit(1)
9491

95-
const progressPartSize = 80 / destination.urls.length
96-
9792
const uploadPart = async (
9893
destinationUrl: string,
9994
index: number,
@@ -106,17 +101,17 @@ export class DirectUploadClient implements IDirectUploadClient {
106101
try {
107102
const response = await axios.put(destinationUrl, fileSlice, {
108103
headers: {
109-
'Content-Type': 'application/octet-stream',
110-
'Content-Length': fileSlice.size
104+
'Content-Type': 'application/octet-stream'
111105
},
112106
maxBodyLength: Infinity,
113107
maxContentLength: Infinity,
114108
timeout: this.fileUploadTimeoutMs,
115-
signal: abortController.signal
109+
signal: abortController.signal,
110+
onUploadProgress: (progressEvent) =>
111+
progress(Math.round(((index * partSize + progressEvent.loaded) * 100) / file.size))
116112
})
117113
const eTag = response.headers['etag'].replace(/"/g, '')
118114
eTags[`${index + 1}`] = eTag
119-
progress(Math.round(this.progressAfterUrlGeneration + progressPartSize * (index + 1)))
120115
} catch (error) {
121116
if (axios.isCancel(error)) {
122117
await this.abortMultipartUpload(file.name, datasetId, destination.abortEndpoint)

0 commit comments

Comments
 (0)