Add storage usage to releases page

This commit is contained in:
rubenwardy 2023-12-29 10:28:00 +00:00
parent e5f5313156
commit 5f658f7a1e
2 changed files with 24 additions and 0 deletions

@ -1130,6 +1130,23 @@ class PackageScreenshot(db.Model):
def file_path(self):
return self.url.replace("/uploads/", app.config["UPLOAD_DIR"])
@property
def file_size_bytes(self):
path = self.file_path
if not os.path.isfile(path):
return 0
file_stats = os.stat(path)
return file_stats.st_size
@property
def file_size(self):
size = self.file_size_bytes / 1024
if size > 1024:
return f"{round(size / 1024, 1)} MB"
else:
return f"{round(size)} KB"
def get_edit_url(self):
return url_for("packages.edit_screenshot",
author=self.package.author.username,

@ -41,4 +41,11 @@
{{ render_releases_download(package.releases, package, current_user) }}
{% endif %}
</div>
<p class="mt-5 text-muted">
{% set release_size = ((package.releases | sum(attribute="file_size_bytes")) / 1048576) | round %}
{% set screenshot_size = ((package.screenshots | sum(attribute="file_size_bytes")) / 1048576) | round %}
{{ _("Total storage usage is %(total_size)d MB (releases %(release_size)d MB, screenshots %(screenshot_size)d MB)",
release_size=release_size, screenshot_size=screenshot_size, total_size=release_size + screenshot_size) }}
</p>
{% endblock %}