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

Show total folder size in replay browser #269

Merged
merged 2 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/common/exists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Based on https://github.com/wilsonzlin/edgesearch/blob/d03816dd4b18d3d2eb6d08cb1ae14f96f046141d/demo/wiki/client/src/util/util.ts

// Ensures value is not null or undefined.
// != does no type validation so we don't need to explcitly check for undefined.
export function exists<T>(value: T | null | undefined): value is T {
return value != null;
}
6 changes: 5 additions & 1 deletion src/renderer/containers/ReplayBrowser/ReplayBrowser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Typography from "@material-ui/core/Typography";
import FolderIcon from "@material-ui/icons/Folder";
import SearchIcon from "@material-ui/icons/Search";
import { colors } from "common/colors";
import { exists } from "common/exists";
import { shell } from "electron";
import React from "react";
import { useToasts } from "react-toast-notifications";
Expand All @@ -22,6 +23,7 @@ import { useDolphin } from "@/lib/hooks/useDolphin";
import { useReplayBrowserList, useReplayBrowserNavigation } from "@/lib/hooks/useReplayBrowserList";
import { useReplayFilter } from "@/lib/hooks/useReplayFilter";
import { useReplays, useReplaySelection } from "@/lib/hooks/useReplays";
import { humanReadableBytes } from "@/lib/utils";

import { FileList } from "./FileList";
import { FileSelectionToolbar } from "./FileSelectionToolbar";
Expand All @@ -41,6 +43,7 @@ export const ReplayBrowser: React.FC = () => {
const netplaySlpFolder = useReplays((store) => store.netplaySlpFolder);
const extraFolders = useReplays((store) => store.extraFolders);
const selectedFiles = useReplays((store) => store.selectedFiles);
const totalBytes = useReplays((store) => store.totalBytes);
const fileSelection = useReplaySelection();
const fileErrorCount = useReplays((store) => store.fileErrorCount);
const { addToast } = useToasts();
Expand Down Expand Up @@ -202,7 +205,8 @@ export const ReplayBrowser: React.FC = () => {
</div>
<div style={{ textAlign: "right" }}>
{filteredFiles.length} files found. {hiddenFileCount} files filtered.{" "}
{fileErrorCount > 0 ? `${fileErrorCount} files had errors.` : ""}
{fileErrorCount > 0 ? `${fileErrorCount} files had errors. ` : ""}
{exists(totalBytes) ? `Total size: ${humanReadableBytes(totalBytes)}` : ""}
</div>
</Footer>
</Outer>
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/lib/hooks/useReplays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type StoreState = {
progress: Progress | null;
files: FileResult[];
netplaySlpFolder: FolderResult | null;
totalBytes: number | null;
extraFolders: FolderResult[];
currentRoot: string | null;
currentFolder: string;
Expand Down Expand Up @@ -45,6 +46,7 @@ const initialState: StoreState = {
loading: false,
progress: null,
files: [],
totalBytes: null,
netplaySlpFolder: null,
extraFolders: [],
currentRoot: null,
Expand Down Expand Up @@ -145,6 +147,7 @@ export const useReplays = create<StoreState & StoreReducers>((set, get) => ({
files: result.files,
loading: false,
fileErrorCount: result.fileErrorCount,
totalBytes: result.totalBytes,
});
} catch (err) {
set({ loading: false, progress: null });
Expand Down
13 changes: 13 additions & 0 deletions src/renderer/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ export const toOrdinal = (i: number): string => {
}
return i + "th";
};

// Converts number of bytes into a human readable format.
// Based on code available from:
// https://coderrocketfuel.com/article/get-the-total-size-of-all-files-in-a-directory-using-node-js
export const humanReadableBytes = (bytes: number): string => {
const sizes = ["bytes", "KB", "MB", "GB", "TB"];
if (bytes > 0) {
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return `${(bytes / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`;
}

return `0 ${sizes[0]}`;
};
38 changes: 24 additions & 14 deletions src/replays/loadFolder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { exists } from "common/exists";
import * as fs from "fs-extra";
import path from "path";

Expand All @@ -13,14 +14,16 @@ export async function loadFolder(
return {
files: [],
fileErrorCount: 0,
totalBytes: 0,
};
}

const results = await fs.readdir(folder, { withFileTypes: true });
const slpFiles = results.filter((dirent) => dirent.isFile() && path.extname(dirent.name) === ".slp");
const total = slpFiles.length;
const fullSlpPaths = results
.filter((dirent) => dirent.isFile() && path.extname(dirent.name) === ".slp")
.map((dirent) => path.resolve(folder, dirent.name));
const total = fullSlpPaths.length;

let fileErrorCount = 0;
let fileValidCount = 0;
callback(0, total);

Expand All @@ -33,27 +36,34 @@ export async function loadFolder(
callback(fileValidCount, total);
resolve(res);
} catch (err) {
fileErrorCount += 1;
resolve(null);
}
});
});
};

const slpGames = (
await Promise.all(
slpFiles.map((dirent) => {
const fullPath = path.resolve(folder, dirent.name);
return process(fullPath);
}),
)
).filter((g) => g !== null) as FileResult[];
const slpGamesPromise = Promise.all(
fullSlpPaths.map((fullPath) => {
return process(fullPath);
}),
);
const fileSizesPromise = Promise.all(
fullSlpPaths.map(
async (fullPath): Promise<number> => {
const stat = await fs.stat(fullPath);
return stat.size;
},
),
);

const [slpGames, fileSizes] = await Promise.all([slpGamesPromise, fileSizesPromise]);

// Indicate that loading is complete
callback(total, total);

return {
files: slpGames,
fileErrorCount,
files: slpGames.filter(exists),
fileErrorCount: total - fileValidCount,
totalBytes: fileSizes.reduce((acc, size) => acc + size, 0),
};
}
1 change: 1 addition & 0 deletions src/replays/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface FolderResult {

export interface FileLoadResult {
files: FileResult[];
totalBytes: number;
fileErrorCount: number;
}

Expand Down