mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-03 19:11:22 +01:00
refactor: use opendir instead of loading all files
This commit is contained in:
@@ -20,13 +20,28 @@ module.exports = {
|
|||||||
await mkdirp(folder);
|
await mkdirp(folder);
|
||||||
const userPicRegex = /^\d+-profile/;
|
const userPicRegex = /^\d+-profile/;
|
||||||
|
|
||||||
const files = (await fs.promises.readdir(folder, { withFileTypes: true }))
|
const dir = await fs.promises.opendir(folder);
|
||||||
.filter(item => !item.isDirectory() && String(item.name).match(userPicRegex))
|
|
||||||
.map(item => item.name);
|
|
||||||
|
|
||||||
progress.total = files.length;
|
let batchBuffer = [];
|
||||||
await batch.processArray(files, async (files) => {
|
const BATCH_SIZE = 500;
|
||||||
progress.incr(files.length);
|
|
||||||
|
for await (const entry of dir) {
|
||||||
|
if (!entry.isDirectory() && userPicRegex.test(entry.name)) {
|
||||||
|
batchBuffer.push(entry.name);
|
||||||
|
}
|
||||||
|
if (batchBuffer.length >= BATCH_SIZE) {
|
||||||
|
await processBatch(batchBuffer);
|
||||||
|
progress.incr(batchBuffer.length);
|
||||||
|
batchBuffer = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (batchBuffer.length > 0) {
|
||||||
|
await processBatch(batchBuffer);
|
||||||
|
progress.incr(batchBuffer.length);
|
||||||
|
batchBuffer = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function processBatch(files) {
|
||||||
await Promise.all(files.map(async (file) => {
|
await Promise.all(files.map(async (file) => {
|
||||||
const uid = file.split('-')[0];
|
const uid = file.split('-')[0];
|
||||||
if (parseInt(uid, 10) > 0) {
|
if (parseInt(uid, 10) > 0) {
|
||||||
@@ -37,9 +52,7 @@ module.exports = {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}, {
|
}
|
||||||
batch: 500,
|
|
||||||
});
|
|
||||||
|
|
||||||
await batch.processSortedSet('users:joindate', async (uids) => {
|
await batch.processSortedSet('users:joindate', async (uids) => {
|
||||||
progress.incr(uids.length);
|
progress.incr(uids.length);
|
||||||
|
|||||||
Reference in New Issue
Block a user