Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file manager not showing kb size of files correctly #146

Open
spirogg opened this issue Oct 25, 2019 · 8 comments
Open

file manager not showing kb size of files correctly #146

spirogg opened this issue Oct 25, 2019 · 8 comments

Comments

@spirogg
Copy link

spirogg commented Oct 25, 2019

in latest version of cyberpanel using litespeed enterprise edition
v 1.9 build 1

If you go to file manager the kb shows 0 in public_html

is there a min before it shows the size?
both
index.html and .htaccess show 0
also the home directory shows 0 kb see snapshots

Capture4
Capture5

@PowerChaos
Copy link
Contributor

i got the same problem
but if i upload files , then the size is correct , but if i create files with the system then it size is always 0
if you unzip a file then it also shows the correct filesize

based on my own information , it does seems it need a minimum of 1024 bytes in size before it even displays anything ( makes sense as it shows in kB instea of kb... and probaly rounded down )
kb -> kilobit
kB - > Kilobyte
x bit = x/8 byte
8 bit = 1 byte
8 kilobit = 1 kilobyte
1 kilobit = 1024 bits
1 kilobyte = 1024 bytes

also folders are not suposed to give any filesize 🤕
in my case it gives 4kB as filesize for whmcs installation ( ofcourse the folder contains more )

as long it calculates some file sizes is it fine for me, first i was thinking it dit not show any size at all , but it does work 😄

@agilesvary
Copy link

here i am also face same issue after 1 week this problem coming

@jordanlambrecht
Copy link
Contributor

Confirmed this issue still persists. It looks like the culprit is on line 205 of filemanager.py:

size = str(int(int(currentFile[4]) / float(1024)))

The file size is converted to an integer. @whattheserver any idea why and if it would break anything if I submitted a pull request to use a rounded float instead? I know rounding isn't very precise with python, but I'm not sure that it matters a whole lot since the data is purely informational/cosmetic.

This seems to work fine in testing:
currentFile = 2341234.23412351235123
size = ((currentFile / float(1024)))
rounded = str(round(size, 2)) + " kb"

Screen Shot 2021-02-05 at 7 45 07 PM

@meramsey
Copy link
Contributor

meramsey commented Feb 6, 2021

Funny you should mention this as i was tinkering with some libraries and code about this before and forgot to write it up

the first function should be more then adequate and the second should be precise and reusable everywhere from my tests

Credits to stackoverflow for both
https://stackoverflow.com/questions/1094841/get-human-readable-version-of-file-size
https://stackoverflow.com/questions/12523586/python-format-size-application-converting-b-to-kb-mb-gb-tb/63839503#63839503

import shutil
from typing import List, Union

def human_size(bytes, units=None):
    """ Returns a human readable string representation of bytes """
    if units is None:
        units = [' bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB']
    return str(bytes) + units[0] if bytes < 1024 else human_size(bytes>>10, units[1:])

class HumanBytes:
    METRIC_LABELS: List[str] = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
    BINARY_LABELS: List[str] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
    PRECISION_OFFSETS: List[float] = [0.5, 0.05, 0.005, 0.0005] # PREDEFINED FOR SPEED.
    PRECISION_FORMATS: List[str] = ["{}{:.0f} {}", "{}{:.1f} {}", "{}{:.2f} {}", "{}{:.3f} {}"] # PREDEFINED FOR SPEED.

    @staticmethod
    def format(num: Union[int, float], metric: bool=False, precision: int=1) -> str:
        """
        Human-readable formatting of bytes, using binary (powers of 1024)
        or metric (powers of 1000) representation.
        """

        assert isinstance(num, (int, float)), "num must be an int or float"
        assert isinstance(metric, bool), "metric must be a bool"
        assert isinstance(precision, int) and 0 <= precision <= 3, "precision must be an int (range 0-3)"

        unit_labels = HumanBytes.METRIC_LABELS if metric else HumanBytes.BINARY_LABELS
        last_label = unit_labels[-1]
        unit_step = 1000 if metric else 1024
        unit_step_thresh = unit_step - HumanBytes.PRECISION_OFFSETS[precision]

        is_negative = num < 0
        if is_negative: # Faster than ternary assignment or always running abs().
            num = abs(num)

        for unit in unit_labels:
            if num < unit_step_thresh:
                # VERY IMPORTANT:
                # Only accepts the CURRENT unit if we're BELOW the threshold where
                # float rounding behavior would place us into the NEXT unit: F.ex.
                # when rounding a float to 1 decimal, any number ">= 1023.95" will
                # be rounded to "1024.0". Obviously we don't want ugly output such
                # as "1024.0 KiB", since the proper term for that is "1.0 MiB".
                break
            if unit != last_label:
                # We only shrink the number if we HAVEN'T reached the last unit.
                # NOTE: These looped divisions accumulate floating point rounding
                # errors, but each new division pushes the rounding errors further
                # and further down in the decimals, so it doesn't matter at all.
                num /= unit_step

        return HumanBytes.PRECISION_FORMATS[precision].format("-" if is_negative else "", num, unit)

# print(HumanBytes.format(2251799813685247)) # 2 pebibytes
# print(HumanBytes.format(1023, precision=2)) # 2 petabytes
# print(HumanBytes.format(1099511627776)) # 1 tebibyte
# print(HumanBytes.format(1000000000000, True)) # 1 terabyte
# print(HumanBytes.format(1000000000, True)) # 1 gigabyte
# print(HumanBytes.format(4318498233, precision=3)) # 4.022 gibibytes
# print(HumanBytes.format(4318498233, True, 3)) # 4.318 gigabytes
# print(HumanBytes.format(-4318498233, precision=2)) # -4.02 gibibytes
# print()
# print()
# print()
#
# print(human_size(2251799813685247)) # 2 pebibytes
# print(human_size(2000000000000000)) # 2 petabytes
# print(human_size(1099511627776)) # 1 tebibyte
# print(human_size(1000000000000)) # 1 terabyte
# print(human_size(1000000000)) # 1 gigabyte
# print(human_size(4318498233)) # 4.022 gibibytes
# print(human_size(4318498233)) # 4.318 gigabytes
# print(human_size(-4318498233)) # -4.02 gibibytes

path = '/home/cooluser/Downloads'
print(shutil.disk_usage(path).used)

print(HumanBytes.format(142329023253, True))

@usmannasir @aonsyed any thoughts on possibly adding this function or class to a utility as its bound to be handy to use to a nice rounded amount for the filemanager in closest human readable size

Although it looks like both old and django has a tool perfectly suited for this built in we could possibly use?

https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#filesizeformat
https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#filesizeformat

@PorkStew
Copy link

PorkStew commented Nov 8, 2021

I would like to follow up on this issue.

When viewing created websites, it shows that they are using 0mb of disk space and 0MB of bandwidth. This is totally incorrect as I have a full website installed. This is the same in file manager, it reports that the files are only a few KB but these files are more than that. This is such an important feature that needs to be fixed because users are not able to tell the amount of storage space they are using.

Specs:
CyberPanel: v2.1.1
OS: Ubuntu 20.04
Kernel: 5.4.0-77-generic

@Lvl4Sword
Copy link
Contributor

@meramsey @jordanlambrecht If either of you ( or whoever else ) wanted to file a PR for this, this still looks to be an issue since it doesn't look to have changed:

size = str(int(int(currentFile[4]) / float(1024)))

meramsey added a commit to meramsey/cyberpanel that referenced this issue Nov 23, 2023
@meramsey
Copy link
Contributor

see this fix @Lvl4Sword
#1160

Can plop in the one .js file if you don't care about the column header saying KB no matter what

@Lvl4Sword
Copy link
Contributor

@spirogg Can you close this? Looks to be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants