From 47e44bdaf5d3ca040590a42c9131b2a6ff2ec534 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Sun, 14 Jan 2024 00:19:53 +0100 Subject: [PATCH] feat: add download size on modal download button --- src/components/Feed/Episodes.vue | 1 + src/components/Feed/Modal.vue | 10 +++++++++- src/utils/debounce.js | 1 - src/utils/size.js | 5 +++++ 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/utils/size.js diff --git a/src/components/Feed/Episodes.vue b/src/components/Feed/Episodes.vue index 742ab05..9d8ecb8 100644 --- a/src/components/Feed/Episodes.vue +++ b/src/components/Feed/Episodes.vue @@ -40,6 +40,7 @@ :image="modalEpisode.image" :link="modalEpisode.link" :name="modalEpisode.name" + :size="modalEpisode.size" :title="title" :url="modalEpisode.url" /> diff --git a/src/components/Feed/Modal.vue b/src/components/Feed/Modal.vue index aa8de8e..7770231 100644 --- a/src/components/Feed/Modal.vue +++ b/src/components/Feed/Modal.vue @@ -22,7 +22,7 @@ - {{ t('repod', 'Download') }} + {{ t('repod', 'Download') }} {{ size ? `(${humanFileSize(size)})` : '' }} @@ -32,6 +32,7 @@ import { NcAvatar, NcButton } from '@nextcloud/vue' import Download from 'vue-material-design-icons/Download.vue' import OpenInNew from 'vue-material-design-icons/OpenInNew.vue' +import { humanFileSize } from '../../utils/size.js' export default { name: 'Modal', @@ -66,6 +67,10 @@ export default { type: String, required: true, }, + size: { + type: Number, + default: null, + }, }, computed: { strippedDescription() { @@ -75,6 +80,9 @@ export default { return strippedDescription.replace(/\n/g, '
') }, }, + methods: { + humanFileSize, + }, } diff --git a/src/utils/debounce.js b/src/utils/debounce.js index c63c724..6c4aa17 100644 --- a/src/utils/debounce.js +++ b/src/utils/debounce.js @@ -1,5 +1,4 @@ // https://stackoverflow.com/a/53486112 - export const debounce = (fn, delay) => { let timeoutID = null return function() { diff --git a/src/utils/size.js b/src/utils/size.js new file mode 100644 index 0000000..8e56f5a --- /dev/null +++ b/src/utils/size.js @@ -0,0 +1,5 @@ +// https://stackoverflow.com/a/20732091 +export const humanFileSize = (size) => { + const i = size === 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024)) + return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i] +}