mirror of
https://github.com/modrinth/code.git
synced 2026-08-31 03:55:59 +00:00
fix: abort signal not processing
This commit is contained in:
@@ -97,6 +97,7 @@ export abstract class XHRUploadClient extends AbstractModrinthClient {
|
|||||||
const xhr = new XMLHttpRequest()
|
const xhr = new XMLHttpRequest()
|
||||||
const metadata = context.metadata as UploadMetadata
|
const metadata = context.metadata as UploadMetadata
|
||||||
const fallbackTotal = this.getUploadPayloadSize(metadata)
|
const fallbackTotal = this.getUploadPayloadSize(metadata)
|
||||||
|
const abortUpload = () => xhr.abort()
|
||||||
|
|
||||||
xhr.upload.addEventListener('progress', (e) => {
|
xhr.upload.addEventListener('progress', (e) => {
|
||||||
const total = e.lengthComputable ? e.total : fallbackTotal
|
const total = e.lengthComputable ? e.total : fallbackTotal
|
||||||
@@ -125,6 +126,12 @@ export abstract class XHRUploadClient extends AbstractModrinthClient {
|
|||||||
|
|
||||||
xhr.addEventListener('error', () => reject(new ModrinthApiError('Upload failed')))
|
xhr.addEventListener('error', () => reject(new ModrinthApiError('Upload failed')))
|
||||||
xhr.addEventListener('abort', () => reject(new ModrinthApiError('Upload cancelled')))
|
xhr.addEventListener('abort', () => reject(new ModrinthApiError('Upload cancelled')))
|
||||||
|
abortController.signal.addEventListener('abort', abortUpload, { once: true })
|
||||||
|
|
||||||
|
if (abortController.signal.aborted) {
|
||||||
|
reject(new ModrinthApiError('Upload cancelled'))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// build URL with params (unlike $fetch, XHR doesn't handle params automatically)
|
// build URL with params (unlike $fetch, XHR doesn't handle params automatically)
|
||||||
let url = context.url
|
let url = context.url
|
||||||
@@ -143,22 +150,21 @@ export abstract class XHRUploadClient extends AbstractModrinthClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send either FormData or file depending on what was provided
|
// Send either FormData or file depending on what was provided
|
||||||
const data = 'formData' in metadata ? metadata.formData : metadata.file
|
const data = metadata.formData instanceof FormData ? metadata.formData : metadata.file
|
||||||
xhr.send(data)
|
xhr.send(data)
|
||||||
abortController.signal.addEventListener('abort', () => xhr.abort())
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private getUploadPayloadSize(metadata: UploadMetadata): number {
|
private getUploadPayloadSize(metadata: UploadMetadata): number {
|
||||||
if ('file' in metadata) {
|
if (metadata.formData instanceof FormData) {
|
||||||
return metadata.file.size
|
let total = 0
|
||||||
|
metadata.formData.forEach((value) => {
|
||||||
|
total += value instanceof Blob ? value.size : new Blob([value]).size
|
||||||
|
})
|
||||||
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
let total = 0
|
return metadata.file instanceof Blob ? metadata.file.size : 0
|
||||||
metadata.formData.forEach((value) => {
|
|
||||||
total += value instanceof Blob ? value.size : new Blob([value]).size
|
|
||||||
})
|
|
||||||
return total
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected createUploadError(xhr: XMLHttpRequest): ModrinthApiError {
|
protected createUploadError(xhr: XMLHttpRequest): ModrinthApiError {
|
||||||
|
|||||||
@@ -733,6 +733,9 @@ function handleUploadFiles() {
|
|||||||
uploadState.value.uploadedBytes = clampedUploadedBytes
|
uploadState.value.uploadedBytes = clampedUploadedBytes
|
||||||
uploadState.value.completedFiles = completedFiles
|
uploadState.value.completedFiles = completedFiles
|
||||||
uploadState.value.currentFileName = files[currentFileIndex]?.name ?? null
|
uploadState.value.currentFileName = files[currentFileIndex]?.name ?? null
|
||||||
|
if (clampedUploadedBytes >= totalBytes) {
|
||||||
|
cancelUpload.value = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handle = client.kyros.content_v1.uploadAddonFile(wid, files, {
|
const handle = client.kyros.content_v1.uploadAddonFile(wid, files, {
|
||||||
@@ -742,6 +745,7 @@ function handleUploadFiles() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await handle.promise
|
await handle.promise
|
||||||
|
cancelUpload.value = null
|
||||||
uploadState.value.uploadedBytes = totalBytes
|
uploadState.value.uploadedBytes = totalBytes
|
||||||
uploadState.value.completedFiles = files.length
|
uploadState.value.completedFiles = files.length
|
||||||
uploadState.value.currentFileName = files[files.length - 1]?.name ?? null
|
uploadState.value.currentFileName = files[files.length - 1]?.name ?? null
|
||||||
|
|||||||
Reference in New Issue
Block a user