Use withFileTypes instead of stat'ing each file

This commit is contained in:
valadaptive 2023-12-05 18:16:49 -05:00
parent 5096e70c11
commit 66da13b1bb
1 changed files with 15 additions and 16 deletions

View File

@ -51,12 +51,12 @@ function validateAssetFileName(inputFilename) {
// Recursive function to get files // Recursive function to get files
function getFiles(dir, files = []) { function getFiles(dir, files = []) {
// Get an array of all files and directories in the passed directory using fs.readdirSync // Get an array of all files and directories in the passed directory using fs.readdirSync
const fileList = fs.readdirSync(dir); const fileList = fs.readdirSync(dir, { withFileTypes: true });
// Create the full path of the file/directory by concatenating the passed directory and file/directory name // Create the full path of the file/directory by concatenating the passed directory and file/directory name
for (const file of fileList) { for (const file of fileList) {
const name = `${dir}/${file}`; const name = path.join(dir, file.name);
// Check if the current file/directory is a directory using fs.statSync // Check if the current file/directory is a directory using fs.statSync
if (fs.statSync(name).isDirectory()) { if (file.isDirectory()) {
// If it is a directory, recursively call the getFiles function with the directory path and the files array // If it is a directory, recursively call the getFiles function with the directory path and the files array
getFiles(name, files); getFiles(name, files);
} else { } else {
@ -84,12 +84,10 @@ router.post('/get', jsonParser, async (_, response) => {
try { try {
if (fs.existsSync(folderPath) && fs.statSync(folderPath).isDirectory()) { if (fs.existsSync(folderPath) && fs.statSync(folderPath).isDirectory()) {
const folders = fs.readdirSync(folderPath) const folders = fs.readdirSync(folderPath, { withFileTypes: true })
.filter(filename => { .filter(file => file.isDirectory());
return fs.statSync(path.join(folderPath, filename)).isDirectory();
});
for (const folder of folders) { for (const { name: folder } of folders) {
if (folder == 'temp') if (folder == 'temp')
continue; continue;
@ -270,15 +268,16 @@ router.post('/character', jsonParser, async (request, response) => {
// Live2d assets // Live2d assets
if (category == 'live2d') { if (category == 'live2d') {
const folders = fs.readdirSync(folderPath); const folders = fs.readdirSync(folderPath, { withFileTypes: true });
for (let modelFolder of folders) { for (const folderInfo of folders) {
if (!folderInfo.isDirectory()) continue;
const modelFolder = folderInfo.name;
const live2dModelPath = path.join(folderPath, modelFolder); const live2dModelPath = path.join(folderPath, modelFolder);
if (fs.statSync(live2dModelPath).isDirectory()) { for (let file of fs.readdirSync(live2dModelPath)) {
for (let file of fs.readdirSync(live2dModelPath)) { //console.debug("Character live2d model found:", file)
//console.debug("Character live2d model found:", file) if (file.includes('model') && file.endsWith('.json'))
if (file.includes('model') && file.endsWith('.json')) output.push(path.join('characters', name, category, modelFolder, file));
output.push(path.join('characters', name, category, modelFolder, file));
}
} }
} }
return response.send(output); return response.send(output);