From ad859d4bef691a7d577408d5d2830234f4cb4d77 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 4 Jan 2021 13:22:44 -0600 Subject: [PATCH] Allow specifying the size of a file, for `readable_size()` method sometimes the file bytes are stored in DB instead of on disk --- tailbone/views/master.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tailbone/views/master.py b/tailbone/views/master.py index 15734212..ea911bd0 100644 --- a/tailbone/views/master.py +++ b/tailbone/views/master.py @@ -3772,16 +3772,17 @@ class MasterView(View): return tags.link_to(content, url) return content - def readable_size(self, path): + def readable_size(self, path, size=None): # TODO: this was shamelessly copied from FormAlchemy ... - length = self.get_size(path) - if length == 0: + if size is None: + size = self.get_size(path) + if size == 0: return '0 KB' - if length <= 1024: + if size <= 1024: return '1 KB' - if length > 1048576: - return '%0.02f MB' % (length / 1048576.0) - return '%0.02f KB' % (length / 1024.0) + if size > 1048576: + return '%0.02f MB' % (size / 1048576.0) + return '%0.02f KB' % (size / 1024.0) def get_size(self, path): try: