From b50c06bff250cb641a78f99cf4c745350e4040ab Mon Sep 17 00:00:00 2001 From: qwqVictor Date: Tue, 7 Nov 2023 17:06:20 +0800 Subject: [PATCH] feat(mirrorlist): new sizeString shim --- src/components/MirrorList.vue | 4 ++-- src/types/MirrorItem.d.ts | 6 ++++-- src/utils/shims.ts | 9 ++++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/components/MirrorList.vue b/src/components/MirrorList.vue index f9aa2f1..74a40ea 100644 --- a/src/components/MirrorList.vue +++ b/src/components/MirrorList.vue @@ -15,7 +15,7 @@ :name="item.alias" :last-update="item.lastUpdateTimeString" :status="item.status" - :size="item.size" + :size="item.sizeString" :url="item.url" :description="item.desc" :is-shrinked="isShrinked" /> @@ -42,7 +42,7 @@ :name="item.alias" :last-update="item.lastUpdateTimeString" :status="item.status" - :size="item.size" + :size="item.sizeString" :url="item.url" :description="item.desc" :is-shrinked="isShrinked" /> diff --git a/src/types/MirrorItem.d.ts b/src/types/MirrorItem.d.ts index d799da9..6999651 100644 --- a/src/types/MirrorItem.d.ts +++ b/src/types/MirrorItem.d.ts @@ -9,7 +9,8 @@ export type MirrorItem = { lastEnded?: number, nextSchedule?: number, upstream?: string, - size?: string, + size?: number, + sizeStr?: string, url?: string, type: 'mirror' | 'proxy' | 'git', errorMsg?: string, @@ -23,5 +24,6 @@ export type MirrorViewItem = MirrorItem & { lastEndedTimeString?: string, nextScheduleTimeString?: string, lastOnlineTimeString?: string, - lastRegisterTimeString?: string + lastRegisterTimeString?: string, + sizeString?: string } diff --git a/src/utils/shims.ts b/src/utils/shims.ts index 2b25376..524d0e6 100644 --- a/src/utils/shims.ts +++ b/src/utils/shims.ts @@ -7,6 +7,12 @@ const dateStringFromMirrorItem = (d?: number): string | undefined => { return d && d > 0 ? dateFormat(new Date(d * 1000)) : undefined } +const convertSizeString = (bytes: number) => { + const exp = Math.floor(Math.log(bytes) / Math.log(1024)) + const result = (bytes / Math.pow(1024, exp)).toFixed(2) + return result + ' ' + (exp === 0 ? 'B' : 'KMGTPEZY'[exp - 1] + 'B') +} + const mirrorViewShim = (mirrorItem: MirrorItem): MirrorViewItem => { return { ...mirrorItem, lastUpdateTimeString: dateStringFromMirrorItem(mirrorItem.lastUpdate), @@ -14,7 +20,8 @@ const mirrorViewShim = (mirrorItem: MirrorItem): MirrorViewItem => { lastEndedTimeString: dateStringFromMirrorItem(mirrorItem.lastEnded), nextScheduleTimeString: dateStringFromMirrorItem(mirrorItem.nextSchedule), lastOnlineTimeString: dateStringFromMirrorItem(mirrorItem.lastOnline), - lastRegisterTimeString: dateStringFromMirrorItem(mirrorItem.lastRegister) + lastRegisterTimeString: dateStringFromMirrorItem(mirrorItem.lastRegister), + sizeString: mirrorItem.sizeStr || convertSizeString(mirrorItem.size ?? 0) } }