web/ProcessingQueueItem: format file size to be readable

This commit is contained in:
wukko 2025-01-25 02:06:50 +06:00
parent 44a99bdb3a
commit 1e6b1cb201
No known key found for this signature in database
GPG Key ID: 3E30B3F26C7B4AA2
2 changed files with 20 additions and 4 deletions

View File

@ -1,4 +1,5 @@
<script lang="ts"> <script lang="ts">
import { formatFileSize } from "$lib/util";
import { downloadFile } from "$lib/download"; import { downloadFile } from "$lib/download";
import { removeItem } from "$lib/state/queen-bee/queue"; import { removeItem } from "$lib/state/queen-bee/queue";
@ -27,6 +28,7 @@
$: state = info.state; $: state = info.state;
$: progress = runningWorker?.progress; $: progress = runningWorker?.progress;
$: size = formatFileSize(runningWorker?.progress?.size);
const download = (file: File) => const download = (file: File) =>
downloadFile({ downloadFile({
@ -58,12 +60,12 @@
{/if} {/if}
<div class="file-status"> <div class="file-status">
{#if info.state === "done"} {#if info.state === "done"}
done: {info.resultFile?.size} bytes done: {formatFileSize(info.resultFile?.size)}
{:else if info.state === "running"} {:else if info.state === "running"}
{#if progress && progress.percentage} {#if progress && progress.percentage}
processing: {Math.ceil(progress.percentage)}%, {progress.size} bytes processing: {Math.ceil(progress.percentage)}%, {size}
{:else if progress && progress.size} {:else if progress && size}
processing: {progress.size} processing: {size}
{:else} {:else}
processing... processing...
{/if} {/if}

14
web/src/lib/util.ts Normal file
View File

@ -0,0 +1,14 @@
export const formatFileSize = (size: number | undefined) => {
size ||= 0;
// gigabyte, megabyte, kilobyte, byte
const units = ['G', 'M', 'K', ''];
while (size >= 1024 && units.length > 1) {
size /= 1024;
units.pop();
}
const roundedSize = parseFloat(size.toFixed(2));
const unit = units[units.length - 1] + "B";
return `${roundedSize} ${unit}`;
}