Remove image check when loading number of files

Checking if an image is processable when loading the directory slowed
 down the initial loading of the directories significantly.
7ms vs 180ms in a small test with about 400 images.
User should not have any other files in these folders.
Other methods still contain sanity checks.
This commit is contained in:
Stefan Forstenlechner 2024-08-15 23:33:40 +02:00
parent 77348a9323
commit 0e28646b62
1 changed files with 1 additions and 21 deletions

View File

@ -1,34 +1,14 @@
import fs from "fs"; import fs from "fs";
import * as path from "path"; import * as path from "path";
import sharp from "sharp";
import { Folders } from "./models"; import { Folders } from "./models";
import { publicPath, thumbnailPath } from "./paths"; import { publicPath, thumbnailPath } from "./paths";
import { consoleLogger } from "./logging";
const isImageProcessable = async (filePath: string): Promise<boolean> =>
sharp(filePath)
.metadata()
.then(() => true)
.catch((err) => {
consoleLogger.error(
`Reading metadata from ${filePath} produced the following error: ${err.message}`,
);
return false;
});
export const walk = async (dirPath: string): Promise<Folders> => { export const walk = async (dirPath: string): Promise<Folders> => {
const dirEnts = fs.readdirSync(path.posix.join(publicPath, dirPath), { const dirEnts = fs.readdirSync(path.posix.join(publicPath, dirPath), {
withFileTypes: true, withFileTypes: true,
}); });
const numberOfFiles = ( const numberOfFiles = dirEnts.filter((f) => f.isFile()).length;
await Promise.all(
dirEnts
.filter((f) => f.isFile())
.map((f) => path.posix.join(publicPath, dirPath, f.name))
.map(isImageProcessable),
)
).filter((a) => a).length;
const children = await Promise.all( const children = await Promise.all(
dirEnts dirEnts