diff --git a/components/torrent/TorrentActionCard.vue b/components/torrent/TorrentActionCard.vue index cc76663..93377c4 100644 --- a/components/torrent/TorrentActionCard.vue +++ b/components/torrent/TorrentActionCard.vue @@ -81,7 +81,8 @@ File Size
- {{ fileSize(torrent.file_size) }} + {{ fileSizeDecimal(torrent.file_size) }}/ + {{ fileSizeBinary(torrent.file_size) }}
@@ -143,7 +144,8 @@ import type { PropType } from "vue"; import type { TorrentResponse } from "torrust-index-types-lib"; import { notify } from "notiwind-ts"; import { - fileSize, + fileSizeDecimal, + fileSizeBinary, downloadTorrent, useRestApi, isUserLoggedIn, diff --git a/components/torrent/TorrentFilesTab.vue b/components/torrent/TorrentFilesTab.vue index 8409117..cb01f30 100644 --- a/components/torrent/TorrentFilesTab.vue +++ b/components/torrent/TorrentFilesTab.vue @@ -28,7 +28,7 @@ {{ index + 1 }} {{ file.name }} - {{ fileSize(file.size) }} + {{ fileSizeDecimal(file.size) }}/{{ fileSizeBinary(file.size) }} @@ -42,7 +42,7 @@ import { ChevronDownIcon } from "@heroicons/vue/24/solid"; import type { PropType } from "vue"; import type { TorrentResponse } from "torrust-index-types-lib"; -import { ref, fileSize } from "#imports"; +import { ref, fileSizeDecimal, fileSizeBinary } from "#imports"; const collapsed = ref(false); diff --git a/components/torrent/TorrentList.vue b/components/torrent/TorrentList.vue index 6fdb683..5aa9922 100644 --- a/components/torrent/TorrentList.vue +++ b/components/torrent/TorrentList.vue @@ -19,7 +19,7 @@
- {{ fileSize(torrent.file_size) }} + {{ fileSizeDecimal(torrent.file_size) }}/{{ fileSizeBinary(torrent.file_size) }} {{ new Date(torrent.date_uploaded).toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) }} ({{ timeSince(new Date(torrent.date_uploaded)) }} ago) u/{{ torrent.uploader }}
@@ -58,7 +58,7 @@ import { ArrowDownTrayIcon, LinkIcon } from "@heroicons/vue/24/outline"; import { ChevronRightIcon, ChevronDownIcon } from "@heroicons/vue/20/solid"; import { type PropType, watch } from "vue"; import type { TorrentListing } from "torrust-index-types-lib"; -import { fileSize, timeSince, ref, downloadTorrent } from "#imports"; +import { fileSizeDecimal, fileSizeBinary, timeSince, ref, downloadTorrent } from "#imports"; const props = defineProps({ torrents: Array as PropType> diff --git a/components/torrent/TorrentTable.vue b/components/torrent/TorrentTable.vue index bc961b2..800a3f7 100644 --- a/components/torrent/TorrentTable.vue +++ b/components/torrent/TorrentTable.vue @@ -21,7 +21,7 @@ {{ torrent.title }} - {{ fileSize(torrent.file_size) }} + {{ fileSizeDecimal(torrent.file_size) }}/{{ fileSizeBinary(torrent.file_size) }} {{ timeSince(new Date(torrent.date_uploaded)) }} ago ({{ new Date(torrent.date_uploaded).toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) }}) {{ torrent.uploader }} @@ -59,7 +59,7 @@ import { ArrowDownTrayIcon, LinkIcon, EyeIcon } from "@heroicons/vue/24/outline"; import { type PropType, watch } from "vue"; import type { TorrentListing } from "torrust-index-types-lib"; -import { fileSize, timeSince, ref, downloadTorrent } from "#imports"; +import { fileSizeDecimal, fileSizeBinary, timeSince, ref, downloadTorrent } from "#imports"; const props = defineProps({ torrents: Array as PropType> diff --git a/composables/helpers.ts b/composables/helpers.ts index 83e4e26..abf77d7 100644 --- a/composables/helpers.ts +++ b/composables/helpers.ts @@ -40,13 +40,24 @@ export function downloadTorrent (infoHash: string, fileName?: string) { }); } -export function fileSize (size: number): string { +export function fileSizeDecimal (size: number): string { if (!size) { size = 0; } let sizeString = `${(size).toFixed(2)} B`; - if (size / 1000000000 < 1000) { sizeString = `${(size / 1000000000).toFixed(2)} GB`; } - if (size / 1000000 < 1000) { sizeString = `${(size / 1000000).toFixed(2)} MB`; } - if (size / 1000 < 1000) { sizeString = `${(size / 1000).toFixed(2)} KB`; } + if (size / Math.pow(1000, 3) < 1000) { sizeString = `${(size / Math.pow(1000, 3)).toFixed(2)} GB`; } + if (size / Math.pow(1000, 2) < 1000) { sizeString = `${(size / Math.pow(1000, 2)).toFixed(2)} MB`; } + if (size / Math.pow(1000, 1) < 1000) { sizeString = `${(size / Math.pow(1000, 1)).toFixed(2)} KB`; } + + return sizeString; +} + +export function fileSizeBinary (size: number): string { + if (!size) { size = 0; } + let sizeString = `${(size).toFixed(2)} B`; + + if (size / Math.pow(1024, 3) < 1024) { sizeString = `${(size / Math.pow(1024, 3)).toFixed(2)} GiB`; } + if (size / Math.pow(1024, 2) < 1024) { sizeString = `${(size / Math.pow(1024, 2)).toFixed(2)} MiB`; } + if (size / Math.pow(1024, 1) < 1024) { sizeString = `${(size / Math.pow(1024, 1)).toFixed(2)} KiB`; } return sizeString; }