Skip to content

Commit e5d97e0

Browse files
committed
Merge #596: feat: show file size in decimal and binary units
d178207 feat: show file size in decimal and binary units (Jose Celano) Pull request description: Show file size in decimal and binary units. For example. ``` File Size: 659.55 MB/629.00 MiB ``` ACKs for top commit: josecelano: ACK d178207 Tree-SHA512: 539a13a3d22acc27916e49e4d7e2a657a941626de15fb193658d2d71e279a95e6a504372dc691efefb7d34c8c39170d2430be5a497e9222de1c53d31767c00c8
2 parents c7fa46c + d178207 commit e5d97e0

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

components/torrent/TorrentActionCard.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@
8181
<span>File Size</span>
8282
</div>
8383
<div class="flex flex-row w-1/2">
84-
<span>{{ fileSize(torrent.file_size) }}</span>
84+
<span>{{ fileSizeDecimal(torrent.file_size) }}</span>/
85+
<span>{{ fileSizeBinary(torrent.file_size) }}</span>
8586
</div>
8687
</div>
8788
<div class="flex flex-row py-2 pb-0">
@@ -143,7 +144,8 @@ import type { PropType } from "vue";
143144
import type { TorrentResponse } from "torrust-index-types-lib";
144145
import { notify } from "notiwind-ts";
145146
import {
146-
fileSize,
147+
fileSizeDecimal,
148+
fileSizeBinary,
147149
downloadTorrent,
148150
useRestApi,
149151
isUserLoggedIn,

components/torrent/TorrentFilesTab.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<tr>
2929
<th>{{ index + 1 }}</th>
3030
<td>{{ file.name }}</td>
31-
<td>{{ fileSize(file.size) }}</td>
31+
<td>{{ fileSizeDecimal(file.size) }}/{{ fileSizeBinary(file.size) }}</td>
3232
</tr>
3333
</template>
3434
</tbody>
@@ -42,7 +42,7 @@
4242
import { ChevronDownIcon } from "@heroicons/vue/24/solid";
4343
import type { PropType } from "vue";
4444
import type { TorrentResponse } from "torrust-index-types-lib";
45-
import { ref, fileSize } from "#imports";
45+
import { ref, fileSizeDecimal, fileSizeBinary } from "#imports";
4646
4747
const collapsed = ref(false);
4848

components/torrent/TorrentList.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</template>
2020
</div>
2121
<div class="flex flex-row items-start justify-start w-full mt-1 flex-nowrap">
22-
<span class="text-xs whitespace-nowrap text-neutral-content/50">{{ fileSize(torrent.file_size) }}</span>
22+
<span class="text-xs whitespace-nowrap text-neutral-content/50">{{ fileSizeDecimal(torrent.file_size) }}/{{ fileSizeBinary(torrent.file_size) }}</span>
2323
<span class="ml-2 text-xs whitespace-nowrap text-neutral-content/50">{{ new Date(torrent.date_uploaded).toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) }} ({{ timeSince(new Date(torrent.date_uploaded)) }} ago)</span>
2424
<a class="ml-2 text-xs whitespace-nowrap text-neutral-content/50">u/{{ torrent.uploader }}</a>
2525
</div>
@@ -58,7 +58,7 @@ import { ArrowDownTrayIcon, LinkIcon } from "@heroicons/vue/24/outline";
5858
import { ChevronRightIcon, ChevronDownIcon } from "@heroicons/vue/20/solid";
5959
import { type PropType, watch } from "vue";
6060
import type { TorrentListing } from "torrust-index-types-lib";
61-
import { fileSize, timeSince, ref, downloadTorrent } from "#imports";
61+
import { fileSizeDecimal, fileSizeBinary, timeSince, ref, downloadTorrent } from "#imports";
6262
6363
const props = defineProps({
6464
torrents: Array as PropType<Array<TorrentListing>>

components/torrent/TorrentTable.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<span class="duration-200 cursor-pointer hover:text-amber-500" @click.stop="$router.push(`/torrent/${torrent.info_hash}`)">{{ torrent.title }}</span>
2222
</td>
2323
<td class="px-2">
24-
{{ fileSize(torrent.file_size) }}
24+
{{ fileSizeDecimal(torrent.file_size) }}/{{ fileSizeBinary(torrent.file_size) }}
2525
</td>
2626
<td>{{ timeSince(new Date(torrent.date_uploaded)) }} ago ({{ new Date(torrent.date_uploaded).toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) }})</td>
2727
<td>{{ torrent.uploader }}</td>
@@ -59,7 +59,7 @@
5959
import { ArrowDownTrayIcon, LinkIcon, EyeIcon } from "@heroicons/vue/24/outline";
6060
import { type PropType, watch } from "vue";
6161
import type { TorrentListing } from "torrust-index-types-lib";
62-
import { fileSize, timeSince, ref, downloadTorrent } from "#imports";
62+
import { fileSizeDecimal, fileSizeBinary, timeSince, ref, downloadTorrent } from "#imports";
6363
6464
const props = defineProps({
6565
torrents: Array as PropType<Array<TorrentListing>>

composables/helpers.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,24 @@ export function downloadTorrent (infoHash: string, fileName?: string) {
4040
});
4141
}
4242

43-
export function fileSize (size: number): string {
43+
export function fileSizeDecimal (size: number): string {
4444
if (!size) { size = 0; }
4545
let sizeString = `${(size).toFixed(2)} B`;
4646

47-
if (size / 1000000000 < 1000) { sizeString = `${(size / 1000000000).toFixed(2)} GB`; }
48-
if (size / 1000000 < 1000) { sizeString = `${(size / 1000000).toFixed(2)} MB`; }
49-
if (size / 1000 < 1000) { sizeString = `${(size / 1000).toFixed(2)} KB`; }
47+
if (size / Math.pow(1000, 3) < 1000) { sizeString = `${(size / Math.pow(1000, 3)).toFixed(2)} GB`; }
48+
if (size / Math.pow(1000, 2) < 1000) { sizeString = `${(size / Math.pow(1000, 2)).toFixed(2)} MB`; }
49+
if (size / Math.pow(1000, 1) < 1000) { sizeString = `${(size / Math.pow(1000, 1)).toFixed(2)} KB`; }
50+
51+
return sizeString;
52+
}
53+
54+
export function fileSizeBinary (size: number): string {
55+
if (!size) { size = 0; }
56+
let sizeString = `${(size).toFixed(2)} B`;
57+
58+
if (size / Math.pow(1024, 3) < 1024) { sizeString = `${(size / Math.pow(1024, 3)).toFixed(2)} GiB`; }
59+
if (size / Math.pow(1024, 2) < 1024) { sizeString = `${(size / Math.pow(1024, 2)).toFixed(2)} MiB`; }
60+
if (size / Math.pow(1024, 1) < 1024) { sizeString = `${(size / Math.pow(1024, 1)).toFixed(2)} KiB`; }
5061

5162
return sizeString;
5263
}

0 commit comments

Comments
 (0)