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