From 5bbebe418ba8a880a802a48a0d6a78574eecbca4 Mon Sep 17 00:00:00 2001 From: Michael Ramsey Date: Thu, 23 Nov 2023 11:42:15 -0500 Subject: [PATCH 1/4] Update filemanager.py to fix filesize issues Fix filesize issues https://github.com/usmannasir/cyberpanel/issues/146 --- filemanager/filemanager.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/filemanager/filemanager.py b/filemanager/filemanager.py index 0608d55f1..be7508b06 100755 --- a/filemanager/filemanager.py +++ b/filemanager/filemanager.py @@ -173,6 +173,15 @@ class FileManager: except: print("Permisson not changed") + + def bytes_to_human_readable(num, suffix='B'): + for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']: + if abs(num) < 1024.0: + return "%3.1f%s%s" % (num, unit, suffix) + num /= 1024.0 + return "%.1f%s%s" % (num, 'Yi', suffix) + + def listForTable(self): try: finalData = {} @@ -221,7 +230,7 @@ class FileManager: if currentFile[0][0] == 'd': dirCheck = 1 - size = str(int(int(currentFile[4]) / float(1024))) + size = bytes_to_human_readable(int(currentFile[4])) lastModified = currentFile[5] + ' ' + currentFile[6] + ' ' + currentFile[7] finalData[str(counter)] = [currentFile[-1], currentFile[-1], lastModified, size, currentFile[0], dirCheck] From b260a456c36085c1237f6294c5681579b92781bd Mon Sep 17 00:00:00 2001 From: Michael Ramsey Date: Thu, 23 Nov 2023 11:44:44 -0500 Subject: [PATCH 2/4] Update index.html to not have hardcoded KB size --- filemanager/templates/filemanager/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/filemanager/templates/filemanager/index.html b/filemanager/templates/filemanager/index.html index 42ccc22bd..464a61a4e 100755 --- a/filemanager/templates/filemanager/index.html +++ b/filemanager/templates/filemanager/index.html @@ -153,7 +153,7 @@ {% trans "File Name" %} - {% trans "Size (KB)" %} + {% trans "Size" %} {% trans "Last Modified" %} {% trans "Permissions" %} @@ -738,4 +738,4 @@ - \ No newline at end of file + From 739fcdf7151427c7d3815042f86bfe8a07f815d2 Mon Sep 17 00:00:00 2001 From: Michael Ramsey Date: Thu, 23 Nov 2023 12:43:19 -0500 Subject: [PATCH 3/4] Update filemanager.py revert python side changes --- filemanager/filemanager.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/filemanager/filemanager.py b/filemanager/filemanager.py index be7508b06..9fff1f51f 100755 --- a/filemanager/filemanager.py +++ b/filemanager/filemanager.py @@ -172,14 +172,6 @@ class FileManager: ProcessUtilities.executioner(command, website.externalApp) except: print("Permisson not changed") - - - def bytes_to_human_readable(num, suffix='B'): - for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']: - if abs(num) < 1024.0: - return "%3.1f%s%s" % (num, unit, suffix) - num /= 1024.0 - return "%.1f%s%s" % (num, 'Yi', suffix) def listForTable(self): @@ -230,7 +222,7 @@ class FileManager: if currentFile[0][0] == 'd': dirCheck = 1 - size = bytes_to_human_readable(int(currentFile[4])) + size = str(int(int(currentFile[4]) / float(1024))) lastModified = currentFile[5] + ' ' + currentFile[6] + ' ' + currentFile[7] finalData[str(counter)] = [currentFile[-1], currentFile[-1], lastModified, size, currentFile[0], dirCheck] From 1f6dd3c808834a8d6c66d72d54a2f5e615ba3e7b Mon Sep 17 00:00:00 2001 From: Michael Ramsey Date: Thu, 23 Nov 2023 12:44:36 -0500 Subject: [PATCH 4/4] Update fileManager.js Switched logic to js side where it works --- .../static/filemanager/js/fileManager.js | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/filemanager/static/filemanager/js/fileManager.js b/filemanager/static/filemanager/js/fileManager.js index 37c2f47ed..e73bed091 100755 --- a/filemanager/static/filemanager/js/fileManager.js +++ b/filemanager/static/filemanager/js/fileManager.js @@ -14,6 +14,28 @@ function getCookie(name) { return cookieValue; } +// JavaScript function to convert bytes to a human-readable format +function bytesToHumanReadable(bytes, suffix = 'B') { + let units = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']; + let i = 0; + while (Math.abs(bytes) >= 1024 && i < units.length - 1) { + bytes /= 1024; + ++i; + } + return bytes.toFixed(1) + units[i] + suffix; +} + +// JavaScript function to convert kilobytes to a human-readable format +function kilobytesToHumanReadable(kilobytes, suffix = 'KB') { + let units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + let i = 0; + while (Math.abs(kilobytes) >= 1024 && i < units.length - 1) { + kilobytes /= 1024; + ++i; + } + return kilobytes.toFixed(2) + ' ' + units[i]; +} + var fileManager = angular.module('fileManager', ['angularFileUpload']); fileManager.config(['$interpolateProvider', function ($interpolateProvider) { @@ -721,7 +743,8 @@ fileManager.controller('fileManagerCtrl', function ($scope, $http, FileUploader, } else { var fileName = filesData[keys[i]][0]; var lastModified = filesData[keys[i]][2]; - var fileSize = filesData[keys[i]][3]; + var fileSizeBytes = parseInt(filesData[keys[i]][3], 10); // Assuming this is the size in kilobytes + var fileSize = kilobytesToHumanReadable(fileSizeBytes); // Convert to human-readable format var permissions = filesData[keys[i]][4]; var dirCheck = filesData[keys[i]][5]; // console.log(fileName);