Allow specifying the size of a file, for readable_size() method

sometimes the file bytes are stored in DB instead of on disk
This commit is contained in:
Lance Edgar 2021-01-04 13:22:44 -06:00
parent 483a47ed43
commit ad859d4bef

View file

@ -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: