Improved uploader
This commit is contained in:
parent
8cb44131cf
commit
d9ff1513b8
@ -1,6 +1,6 @@
|
||||
import AnimeNotifier from "../AnimeNotifier"
|
||||
import StatusMessage from "../StatusMessage"
|
||||
import { fetchWithProgress } from "../Utils/fetchWithProgress"
|
||||
import { bytesHumanReadable, fetchWithProgress } from "../Utils"
|
||||
|
||||
// Select file
|
||||
export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) {
|
||||
@ -56,19 +56,13 @@ function uploadFile(file: File, fileType: string, endpoint: string, arn: AnimeNo
|
||||
|
||||
reader.onloadend = async () => {
|
||||
let fileSize = reader.result.byteLength
|
||||
let unit = "bytes"
|
||||
|
||||
if(fileSize >= 1024) {
|
||||
fileSize /= 1024
|
||||
unit = "KB"
|
||||
|
||||
if(fileSize >= 1024) {
|
||||
fileSize /= 1024
|
||||
unit = "MB"
|
||||
}
|
||||
if(fileSize === 0) {
|
||||
arn.statusMessage.showError("File is empty")
|
||||
return
|
||||
}
|
||||
|
||||
arn.statusMessage.showInfo(`Uploading ${fileType}...${fileSize.toFixed(0)} ${unit}`, -1)
|
||||
arn.statusMessage.showInfo(`Preparing to upload ${fileType} (${bytesHumanReadable(fileSize)})`, -1)
|
||||
|
||||
try {
|
||||
let responseText = await fetchWithProgress(endpoint, {
|
||||
@ -79,11 +73,7 @@ function uploadFile(file: File, fileType: string, endpoint: string, arn: AnimeNo
|
||||
},
|
||||
body: reader.result
|
||||
}, e => {
|
||||
if(!e.lengthComputable) {
|
||||
return
|
||||
}
|
||||
|
||||
let progress = e.loaded / e.total * 100
|
||||
let progress = e.loaded / (e.lengthComputable ? e.total : fileSize) * 100
|
||||
arn.statusMessage.showInfo(`Uploading ${fileType}...${progress.toFixed(1)}%`, -1)
|
||||
})
|
||||
|
||||
|
15
scripts/Utils/bytesHumanReadable.ts
Normal file
15
scripts/Utils/bytesHumanReadable.ts
Normal file
@ -0,0 +1,15 @@
|
||||
export function bytesHumanReadable(fileSize: number): string {
|
||||
let unit = "bytes"
|
||||
|
||||
if(fileSize >= 1024) {
|
||||
fileSize /= 1024
|
||||
unit = "KB"
|
||||
|
||||
if(fileSize >= 1024) {
|
||||
fileSize /= 1024
|
||||
unit = "MB"
|
||||
}
|
||||
}
|
||||
|
||||
return `${fileSize.toFixed(0)} ${unit}`
|
||||
}
|
@ -3,4 +3,6 @@ export * from "./delay"
|
||||
export * from "./findAll"
|
||||
export * from "./plural"
|
||||
export * from "./requestIdleCallback"
|
||||
export * from "./swapElements"
|
||||
export * from "./swapElements"
|
||||
export * from "./fetchWithProgress"
|
||||
export * from "./bytesHumanReadable"
|
Loading…
Reference in New Issue
Block a user