georstat/react-native-image-cache

Wrong calculation of cache size

Opened this issue · 0 comments

I think the calculation of cache size is wrong. When testing, it returned size of a directory without content.
Proper way would be:

const dirSize = async (dir: string) => {
  const files = await FileSystem.statDir(dir);

  const paths = files.map(async (file): Promise<number> => {
    const filePath = `${mainCacheDir}${file.path}`;

    if (file.type === 'directory') {
      return dirSize(filePath);
    }

    return file.size;
  });

  return (await Promise.all(paths)).flat(Infinity).reduce((i, size) => i + size, 0);
};
export async function getCacheSize(): Promise<number> {
  return dirSize(mainCacheDir);
}

I know it is used only to validate if cache dir exists, but in my case I am using it to show the button in the app with cache size and ability to clear cache.