Skip to content

Commit

Permalink
proper file size display #48
Browse files Browse the repository at this point in the history
  • Loading branch information
FlatErikk committed Apr 15, 2024
1 parent e7fe839 commit c3bff03
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion framework/climate/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def get(self, request):

dir_content_element = []
dir_content_element.append(f)
dir_content_element.append(str(round(file_stats.st_size / (1024 * 1024), 4)) + " MB")
dir_content_element.append(self.sizeof_fmt(file_stats.st_size))
creation_date = None

try:
Expand All @@ -136,6 +136,14 @@ def get(self, request):
response = JsonResponse({"content": dir_content})
return response

def sizeof_fmt(self, num, suffix="B"):
for unit in ("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"):
if abs(num) < 1024.0:
return f"{num:3.1f} {unit}{suffix}"
num /= 1024.0

return f"{num:.1f}Yi{suffix}"


# returns a single file (if it is present in the specified directory ('TESTCONTENT_DIR' rn)
class GetFileView(APIView):
Expand Down

0 comments on commit c3bff03

Please sign in to comment.