Skip to content

Commit

Permalink
Streamlines storage of space to bytes in the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
akolson committed Oct 13, 2023
1 parent 7af2822 commit 8427a53
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
6 changes: 1 addition & 5 deletions kolibri/core/content/utils/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,14 @@ def get_free_space_for_downloads(completed_size=0):
from kolibri.core.device.utils import get_device_setting
from kolibri.utils.conf import OPTIONS

from kolibri.utils.data import bytes_from_humans
from kolibri.utils.system import get_free_space

free_space = get_free_space(OPTIONS["Paths"]["CONTENT_DIR"])

# if a limit is set, subtract the total content storage size from the limit
if get_device_setting("set_limit_for_autodownload"):
# compute total space used by automatic and learner initiated downloads
# convert limit_for_autodownload from GB to bytes
auto_download_limit = bytes_from_humans(
str(get_device_setting("limit_for_autodownload") or "0") + "GB"
)
auto_download_limit = get_device_setting("limit_for_autodownload") or 0
# returning smallest argument as to not exceed the space available on disk
free_space = min(free_space, auto_download_limit - completed_size)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,16 @@
>
<KTextbox
ref="autoDownloadLimit"
v-model="limitForAutodownload"
v-model="limitForAutodownloadInput"
class="download-limit-textbox"
:disabled="notEnoughFreeSpace || isRemoteContent"
type="number"
:label="$tr('sizeInGigabytesLabel')"
:min="0"
:max="freeSpace"
:max="toGigabytes(freeSpace)"
:invalid="notEnoughFreeSpace"
:invalidText="$tr('notEnoughFreeSpace')"
@input="updateLimitForAutodownload"
/>
<div class="slider-section">
<input
Expand All @@ -235,13 +236,14 @@
min="0"
:max="freeSpace"
step="1"
@input="updateLimitForAutodownloadInput"
>
<div class="slider-constraints">
<p class="slider-min-max">
0
</p>
<p class="slider-min-max">
{{ freeSpace }}
{{ toGigabytes(freeSpace) }}
</p>
</div>
</div>
Expand Down Expand Up @@ -554,6 +556,14 @@
}
return this.$tr('alertDisabledOptions');
},
limitForAutodownloadInput: {
get() {
return this.toGigabytes(this.limitForAutodownload);
},
set(value) {
this.limitForAutodownload = this.toBytes(value);
},
},
},
created() {
this.setDeviceURLs();
Expand Down Expand Up @@ -696,7 +706,7 @@
},
setFreeSpace() {
return getFreeSpaceOnServer().then(({ freeSpace }) => {
this.freeSpace = parseInt(bytesForHumans(freeSpace).substring(0, 3));
this.freeSpace = freeSpace;
});
},
handleLandingPageChange(option) {
Expand Down Expand Up @@ -872,6 +882,18 @@
}
return '';
},
updateLimitForAutodownload() {
this.limitForAutodownload = this.toBytes(this.limitForAutodownloadInput);
},
updateLimitForAutodownloadInput() {
this.limitForAutodownloadInput = this.toGigabytes(this.limitForAutodownload);
},
toBytes(gigabytes) {
return parseInt(Math.round(gigabytes * 10 ** 9));
},
toGigabytes(bytes) {
return parseInt(bytesForHumans(bytes).replace(/[^0-9.]/g, ''));
},
},
$trs: {
browserDefaultLanguage: {
Expand Down

0 comments on commit 8427a53

Please sign in to comment.