Skip to content

Commit

Permalink
Merge branch 'feature/more-precise-file-size' into 'devel'
Browse files Browse the repository at this point in the history
Display more precise file size

Closes #1153

See merge request sds-dev/sd-connect/swift-browser-ui!207
  • Loading branch information
Joonatan Mäkinen committed Nov 14, 2023
2 parents 4fcb900 + 293298a commit 575558b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- (GL #1105) Upload modal restricted to uploading to current container, or creating a new one in main container view
- (GL #1028) Disallow uploading files with size 0
- Use common base class for database connections
- (GL #1153) Display decimals with single digit file sizes

### Fixed

Expand Down
46 changes: 37 additions & 9 deletions swift_browser_ui_frontend/src/common/conv.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,46 @@ export async function syncContainerACLs(store) {
return amount;
}

export function getHumanReadableSize(val) {
export function getHumanReadableSize(val, locale) {
// Get a human readable version of the size, which is returned from the
// API as bytes, flooring to the most significant size without decimals.
// API as bytes

// As JS doesn't allow us to natively handle 64 bit integers, ditch all
// unnecessary stuff from the value, we only need the significant part.
let byteval = val > 4294967296 ? parseInt(val / 1073741824) : val;
let count = val > 4294967296 ? 3 : 0;
const isLargeVal = val > 4294967296;
let largeNum;
let byteval = val;
let count = 0;

let human = shiftSizeDivision([byteval, count]);
let ret = human[0].toString();
switch (human[1]) {
if (isLargeVal) {
largeNum = val / 1073741824;
byteval = parseInt(largeNum);
count = 3;
}

const result = shiftSizeDivision([byteval, count]);
let ret = result[0].toString();

if (ret.length === 1 && ret !== "0") {
//display single digit results with one decimal rounded up
let decimal;
let retNum = result[0];

for (let i=count; i < result[1]; i++) {
retNum = retNum << 10;
}
if (isLargeVal) {
decimal = largeNum - retNum;
}
else {
let remainder = val ^ retNum;
decimal = remainder / Math.pow(1024, result[1]);
}
ret = (result[0] + decimal).toFixed(1).toString();

if (locale === "fi") {
ret = ret.replace(".", ",");
}
}
switch (result[1]) {
case 0:
ret += " B";
break;
Expand Down
2 changes: 1 addition & 1 deletion swift_browser_ui_frontend/src/components/CObjectTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export default {
} : {}),
},
size: {
value: getHumanReadableSize(item.bytes),
value: getHumanReadableSize(item.bytes, this.locale),
},
last_modified: {
value: this.showTimestamp? parseDateTime(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export default {
value: item.count,
},
size: {
value: getHumanReadableSize(item.bytes),
value: getHumanReadableSize(item.bytes, this.locale),
},
...(this.hideTags ? {} : {
tags: {
Expand Down
6 changes: 0 additions & 6 deletions swift_browser_ui_frontend/src/components/ObjectTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@

<script>
import {
getHumanReadableSize,
truncate,
parseDateTime,
} from "@/common/conv";
Expand Down Expand Up @@ -511,11 +510,6 @@ export default {
parseInt(this.$route.query.page) :
1;
},
// Make human readable translation functions available in instance
// namespace
localHumanReadableSize: function ( size ) {
return getHumanReadableSize( size );
},
getPrefix: function () {
// Get current pseudofolder prefix
if (this.$route.query.prefix == undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</span>
<span>
<b>{{ $t('message.search.size') }}: </b>
<span> {{ getHumanReadableSize(item.bytes) }}</span>
<span> {{ getHumanReadableSize(item.bytes, locale) }}</span>
</span>
<span v-if="item.count">
<br>
Expand All @@ -62,6 +62,11 @@ export default {
"searchArray",
"route",
],
computed :{
locale () {
return this.$i18n.locale;
},
},
methods: {
getHumanReadableSize,
isSubfolder: function() {
Expand Down
11 changes: 5 additions & 6 deletions swift_browser_ui_frontend/src/components/UploadModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ export default {
active() {
return this.$store.state.active;
},
locale() {
return this.$i18n.locale;
},
pubkey() {
return this.$store.state.pubkey;
},
Expand Down Expand Up @@ -501,6 +504,7 @@ export default {
}
},
methods: {
getHumanReadableSize,
checkPage(event, isKey) {
const page = checkIfItemIsLastOnPage(
{
Expand Down Expand Up @@ -555,7 +559,7 @@ export default {
return {
name: { value: file.name || truncate(100) },
type: { value: file.type },
size: { value: this.localHumanReadableSize(file.size) },
size: { value: getHumanReadableSize(file.size, this.locale) },
relativePath: {
value: file.relativePath || truncate(100),
},
Expand Down Expand Up @@ -699,11 +703,6 @@ export default {
}
}
},
// Make human readable translation functions available in instance
// namespace
localHumanReadableSize: function (size) {
return getHumanReadableSize(size);
},
dragHandler: function (e) {
e.preventDefault();
let dt = e.dataTransfer;
Expand Down

0 comments on commit 575558b

Please sign in to comment.