Add release download size to API and web interface

This commit is contained in:
rubenwardy 2023-12-15 16:33:34 +00:00
parent 0e5158704e
commit e9911e85a2
4 changed files with 30 additions and 1 deletions

@ -212,6 +212,7 @@ Supported query parameters:
* `downloads`: number of downloads
* `min_minetest_version`: dict or null, minimum supported minetest version (inclusive).
* `max_minetest_version`: dict or null, minimum supported minetest version (inclusive).
* `size`: size of zip file, in bytes.
* `package`
* `author`: author username
* `name`: technical name

@ -17,6 +17,7 @@
import datetime
import enum
import os
from flask import url_for
from flask_babel import lazy_gettext
@ -979,6 +980,23 @@ class PackageRelease(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 as_dict(self):
return {
"id": self.id,
@ -989,6 +1007,7 @@ class PackageRelease(db.Model):
"downloads": self.downloads,
"min_minetest_version": self.min_rel and self.min_rel.as_dict(),
"max_minetest_version": self.max_rel and self.max_rel.as_dict(),
"size": self.file_size_bytes,
}
def as_long_dict(self):
@ -1001,7 +1020,8 @@ class PackageRelease(db.Model):
"downloads": self.downloads,
"min_minetest_version": self.min_rel and self.min_rel.as_dict(),
"max_minetest_version": self.max_rel and self.max_rel.as_dict(),
"package": self.package.as_key_dict()
"package": self.package.as_key_dict(),
"size": self.file_size_bytes,
}
def get_edit_url(self):

@ -38,6 +38,9 @@
[MT ≤{{ rel.max_rel.name }}]
{% endif %}
</span>
<span class="text-muted ms-1">
{{ rel.file_size }}
</span>
<br />
<small style="color:#999;">
{% if rel.commit_hash %}
@ -79,6 +82,10 @@
{% endif %}
</span>
<span class="text-muted ms-1">
{{ rel.file_size }}
</span>
<br>
<small style="color:#999;">

@ -39,6 +39,7 @@
href="{{ release.get_download_url() }}">
<div>
{{ _("Download") }}
<span class="text-muted">({{ release.file_size }})</span>
</div>
{% if release and (release.min_rel or release.max_rel) %}