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 tags.link_to(content, url)
return content return content
def readable_size(self, path): def readable_size(self, path, size=None):
# TODO: this was shamelessly copied from FormAlchemy ... # TODO: this was shamelessly copied from FormAlchemy ...
length = self.get_size(path) if size is None:
if length == 0: size = self.get_size(path)
if size == 0:
return '0 KB' return '0 KB'
if length <= 1024: if size <= 1024:
return '1 KB' return '1 KB'
if length > 1048576: if size > 1048576:
return '%0.02f MB' % (length / 1048576.0) return '%0.02f MB' % (size / 1048576.0)
return '%0.02f KB' % (length / 1024.0) return '%0.02f KB' % (size / 1024.0)
def get_size(self, path): def get_size(self, path):
try: try: