From 8d13b1a47b3ea67b5919ead956b5562961aa71ba Mon Sep 17 00:00:00 2001 From: Alin Voinea Date: Mon, 17 Aug 2020 15:43:44 +0300 Subject: [PATCH] FileWidget: Fix size format --- src/components/theme/Widgets/FileWidget.jsx | 6 +++--- src/components/theme/Widgets/ImageWidget.jsx | 4 ++-- .../theme/Widgets/__snapshots__/FileWidget.test.js.snap | 6 +++--- .../theme/Widgets/__snapshots__/ImageWidget.test.js.snap | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/components/theme/Widgets/FileWidget.jsx b/src/components/theme/Widgets/FileWidget.jsx index 4a7295f..d1b23d0 100644 --- a/src/components/theme/Widgets/FileWidget.jsx +++ b/src/components/theme/Widgets/FileWidget.jsx @@ -5,10 +5,10 @@ import { flattenToAppURL } from '@plone/volto/helpers'; const niceBytes = (bytes) => { bytes = Number(bytes); - const divider = 1000; + const divider = 1024; const magnitude = (Math.log(bytes) / Math.log(divider)) | 0; const result = bytes / Math.pow(divider, magnitude); - const fixed = result.toFixed(2); + const fixed = result.toFixed(0); const suffix = magnitude ? 'kMGTPEZY'[magnitude - 1] + 'B' : 'B'; @@ -22,7 +22,7 @@ export const FileWidget = ({ value, children, className }) => { const url = flattenToAppURL(value.download || value.filename || value); const filename = value.filename || url; - const size = value.size || 0; + const size = value.data ? value.data.length * 0.75 : value.size || 0; const ctype = value['content-type'] || ''; return ( { bytes = Number(bytes); - const divider = 1000; + const divider = 1024; const magnitude = (Math.log(bytes) / Math.log(divider)) | 0; const result = bytes / Math.pow(divider, magnitude); - const fixed = result.toFixed(2); + const fixed = result.toFixed(0); const suffix = magnitude ? 'kMGTPEZY'[magnitude - 1] + 'B' : 'B'; diff --git a/src/components/theme/Widgets/__snapshots__/FileWidget.test.js.snap b/src/components/theme/Widgets/__snapshots__/FileWidget.test.js.snap index a572fc0..cc26c3c 100644 --- a/src/components/theme/Widgets/__snapshots__/FileWidget.test.js.snap +++ b/src/components/theme/Widgets/__snapshots__/FileWidget.test.js.snap @@ -5,7 +5,7 @@ exports[`FileWidget renders a file view widget component 1`] = ` className="metadata file widget" data-content-type="" data-size={0} - data-size-fmt="0.00B" + data-size-fmt="0B" href="/foo-bar.pdf" title="/foo-bar.pdf" > @@ -18,7 +18,7 @@ exports[`FileWidget renders a file view widget component with children 1`] = ` className="metadata file widget" data-content-type="application/x-pdf" data-size={123456} - data-size-fmt="123.46kB" + data-size-fmt="121kB" href="/foo-bar.pdf" title="application/x-pdf" > @@ -33,7 +33,7 @@ exports[`FileWidget renders a simple file view widget component 1`] = ` className="metadata file widget" data-content-type="" data-size={0} - data-size-fmt="0.00B" + data-size-fmt="0B" href="/foo-bar.pdf" title="/foo-bar.pdf" > diff --git a/src/components/theme/Widgets/__snapshots__/ImageWidget.test.js.snap b/src/components/theme/Widgets/__snapshots__/ImageWidget.test.js.snap index 96fc65d..7d2e6d4 100644 --- a/src/components/theme/Widgets/__snapshots__/ImageWidget.test.js.snap +++ b/src/components/theme/Widgets/__snapshots__/ImageWidget.test.js.snap @@ -10,7 +10,7 @@ exports[`ImageWidget renders an image view widget component 1`] = ` alt="" data-content-type="" data-size={0} - data-size-fmt="0.00B" + data-size-fmt="0B" src="/foo-bar.png" /> @@ -24,7 +24,7 @@ exports[`ImageWidget renders an image view widget component with children 1`] = alt="foo-bar.png" data-content-type="" data-size={0} - data-size-fmt="0.00B" + data-size-fmt="0B" src="/foo-bar.png" />