Skip to content

[Feature] show folder files count and file size #306

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

Open
yzqdev opened this issue Mar 31, 2025 · 0 comments
Open

[Feature] show folder files count and file size #306

yzqdev opened this issue Mar 31, 2025 · 0 comments

Comments

@yzqdev
Copy link

yzqdev commented Mar 31, 2025

I just add some features showing the file count in folder and file size, downloaded zip size and git clone size.I don't know whether those data is redundant.

Image

it's easy to implement, when the node type is tree,calculate all the node count of this tree recusively. when the node type if blob, humanFilesize the node.size varible

zip size is the size when you click download as zip button, recusively add up all the node.size under root node

the clone size is size one repo takes in github server(not you run git clone all the files size), you could see you own repo size in https://github.com/settings/repositories. this size comes from https://api.github.com/repos/OWNER/REPO api

some of the format size code

//one file size formatter
export function humanFileSize(bytes:number, si=true, dp=1) {
  if (bytes===null||bytes===undefined ) {
    return ''
  }
  const thresh = si ? 1000 : 1024;

  if (Math.abs(bytes) < thresh) {
    return bytes + ' B';
  }

  const units = si 
    ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] 
    : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
  let u = -1;
  const r = 10**dp;

  do {
    bytes /= thresh;
    ++u;
  } while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);


  return bytes.toFixed(dp) + ' ' + units[u];
}

// get the repo's zip size
export function calculateTotalSize(data) {
  let total = 0;

 
  const traverse = (nodes) => {
    for (const node of nodes) {
      if (node.type === 'blob' && typeof node.size === 'number') {
        total += node.size;  
      } else if (node.type === 'tree' && node.contents) {
        traverse(node.contents);  
      }
    }
  };

  traverse(data.nodes); 
 
  return total;
}

//format the size through `https://api.github.com/repos/OWNER/REPO` api
export function formatKiloFileSize(kilobytes) {
  const units = ['KB', 'MB', 'GB', 'TB', 'PB'];
  let size = kilobytes;
  let unitIndex = 0;
 
  while (size >= 1024 && unitIndex < units.length - 1) {
    size /= 1024;
    unitIndex++;
  }
 
  const formattedSize = size.toFixed(2).replace(/\.?0+$/, '');
  return `${formattedSize} ${units[unitIndex]}`;
}


 
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

1 participant