WACATW commited on
Commit
7ce3dfa
·
verified ·
1 Parent(s): 1d4b698

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +10 -9
server.js CHANGED
@@ -200,20 +200,21 @@ function formatSize(sizeBytes) {
200
  }
201
 
202
  // 获取用户存储空间占用
203
- function getUserTotalSize(username) {
 
204
  const userDir = path.join(CLOUD_DISK_PATH, 'users', username);
205
  if (!fs.existsSync(userDir)) return 0;
206
  let total = 0;
207
- function calcDir(dir) {
208
- const list = fs.readdirSync(dir);
209
- for (let f of list) {
210
- const full = path.join(dir, f);
211
- const stat = fs.statSync(full);
212
- if (stat.isDirectory()) calcDir(full);
213
  else total += stat.size;
214
  }
215
- }
216
- calcDir(userDir);
217
  return total;
218
  }
219
 
 
200
  }
201
 
202
  // 获取用户存储空间占用
203
+ // 异步计算存储空间,不会卡死服务
204
+ async function getUserTotalSize(username) {
205
  const userDir = path.join(CLOUD_DISK_PATH, 'users', username);
206
  if (!fs.existsSync(userDir)) return 0;
207
  let total = 0;
208
+ const scanDir = async (dir) => {
209
+ const files = await fs.promises.readdir(dir);
210
+ for (const name of files) {
211
+ const fullPath = path.join(dir, name);
212
+ const stat = await fs.promises.stat(fullPath);
213
+ if (stat.isDirectory()) await scanDir(fullPath);
214
  else total += stat.size;
215
  }
216
+ };
217
+ await scanDir(userDir);
218
  return total;
219
  }
220