diff --git a/app/flatpages/help/api.md b/app/flatpages/help/api.md index b71f2486..6c13005a 100644 --- a/app/flatpages/help/api.md +++ b/app/flatpages/help/api.md @@ -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 diff --git a/app/models/packages.py b/app/models/packages.py index dbe571f0..376366a2 100644 --- a/app/models/packages.py +++ b/app/models/packages.py @@ -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): diff --git a/app/templates/macros/releases.html b/app/templates/macros/releases.html index 2483764d..24ca48f9 100644 --- a/app/templates/macros/releases.html +++ b/app/templates/macros/releases.html @@ -38,6 +38,9 @@ [MT ≤{{ rel.max_rel.name }}] {% endif %} + + {{ rel.file_size }} +
{% if rel.commit_hash %} @@ -79,6 +82,10 @@ {% endif %} + + {{ rel.file_size }} + +
diff --git a/app/templates/packages/view.html b/app/templates/packages/view.html index f584dde8..da5e5529 100644 --- a/app/templates/packages/view.html +++ b/app/templates/packages/view.html @@ -39,6 +39,7 @@ href="{{ release.get_download_url() }}">
{{ _("Download") }} + ({{ release.file_size }})
{% if release and (release.min_rel or release.max_rel) %}