aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2023-10-08 14:06:18 -0500
committerJP Dillingham <jp@dillingham.me>2023-10-08 14:06:18 -0500
commit5199a427c3be459c1499b640b43bb84585e0a47c (patch)
tree0c3a6826f9dac9cd150a1b3fead3b066440c76a3 /src
parent14cdb89836575741104cfcf73f446233f18c3809 (diff)
parent12e89983470a55e03f30f35002135b921183c72b (diff)
downloadslskd-5199a427c3be459c1499b640b43bb84585e0a47c.tar.gz
Merge remote-tracking branch 'origin/master' into file-retention
Diffstat (limited to 'src')
-rw-r--r--src/web/src/components/Transfers/TransferList.js21
-rw-r--r--src/web/src/lib/util.js14
-rw-r--r--src/web/src/lib/util.test.js5
3 files changed, 21 insertions, 19 deletions
diff --git a/src/web/src/components/Transfers/TransferList.js b/src/web/src/components/Transfers/TransferList.js
index c35f512f..1efe5900 100644
--- a/src/web/src/components/Transfers/TransferList.js
+++ b/src/web/src/components/Transfers/TransferList.js
@@ -4,7 +4,7 @@ import {
Checkbox,
} from 'semantic-ui-react';
-import { formatBytes, getFileName } from '../../lib/util';
+import { formatBytes, formatBytesAsUnit, getFileName } from '../../lib/util';
import {
Header,
@@ -37,23 +37,10 @@ const isRetryableState = (state) => getColor(state).color === 'red';
const isQueuedState = (state) => state.includes('Queued');
const formatBytesTransferred = ({ transferred, size }) => {
- const [t, tExt] = formatBytes(transferred).split(' ');
- const [s, sExt] = formatBytes(size).split(' ');
+ const [s, sExt] = formatBytes(size, 1).split(' ');
+ const t = formatBytesAsUnit(transferred, 1, sExt);
- const fmt = (n) => parseFloat(n).toFixed(2);
-
- // if less than 1 MB has been transferred, don't include decimals
- if (tExt === 'KB') {
- return `${t} KB/${fmt(s)} ${sExt}`;
- }
-
- // if the suffix for size and transferred doesn't match, include
- // the suffix for each
- if (tExt !== sExt) {
- return `${fmt(t)} ${tExt}/${fmt(s)} ${sExt}`;
- }
-
- return `${fmt(t)}/${fmt(s)} ${sExt}`;
+ return `${t}/${s} ${sExt}`;
};
class TransferList extends Component {
diff --git a/src/web/src/lib/util.js b/src/web/src/lib/util.js
index 974915ed..dbd8f482 100644
--- a/src/web/src/lib/util.js
+++ b/src/web/src/lib/util.js
@@ -9,12 +9,22 @@ export const formatSeconds = (seconds) =>
return date.toTimeString().replace(/.*(\d{2}:\d{2}).*/, '$1');
};
+export const formatBytesAsUnit = (bytes, decimals = 2, unit) => {
+ if (unit === 'B') return bytes + ' ' + unit;
+
+ const k = 1024;
+ const dm = decimals < 0 ? 0 : decimals;
+ const sizes = {'KB': 1, 'MB': 2, 'GB': 3, 'TB': 4, 'PB': 5, 'EB': 6, 'ZB': 7, 'YB': 8};
+
+ return parseFloat((bytes / Math.pow(k, sizes[unit])).toFixed(dm));
+};
+
export const formatBytes = (bytes, decimals = 2) => {
- if (bytes === 0) return '0 Bytes';
+ if (bytes === 0) return '0 B';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
- const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
+ const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
diff --git a/src/web/src/lib/util.test.js b/src/web/src/lib/util.test.js
new file mode 100644
index 00000000..5fe92af7
--- /dev/null
+++ b/src/web/src/lib/util.test.js
@@ -0,0 +1,5 @@
+import * as utils from './util';
+
+it('converts bytes to specified unit', () => {
+ expect(utils.formatBytesAsUnit(1234567, 2, 'MB')).toBe(1.18);
+}); \ No newline at end of file