diff --git a/scripts/Actions/Upload.ts b/scripts/Actions/Upload.ts index 56e3075d..dee082a1 100644 --- a/scripts/Actions/Upload.ts +++ b/scripts/Actions/Upload.ts @@ -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) }) diff --git a/scripts/Utils/bytesHumanReadable.ts b/scripts/Utils/bytesHumanReadable.ts new file mode 100644 index 00000000..554dc495 --- /dev/null +++ b/scripts/Utils/bytesHumanReadable.ts @@ -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}` +} \ No newline at end of file diff --git a/scripts/Utils/index.ts b/scripts/Utils/index.ts index 4eb261ca..b7c009a5 100644 --- a/scripts/Utils/index.ts +++ b/scripts/Utils/index.ts @@ -3,4 +3,6 @@ export * from "./delay" export * from "./findAll" export * from "./plural" export * from "./requestIdleCallback" -export * from "./swapElements" \ No newline at end of file +export * from "./swapElements" +export * from "./fetchWithProgress" +export * from "./bytesHumanReadable" \ No newline at end of file