Update server.js
Browse files
server.js
CHANGED
|
@@ -200,20 +200,21 @@ function formatSize(sizeBytes) {
|
|
| 200 |
}
|
| 201 |
|
| 202 |
// 获取用户存储空间占用
|
| 203 |
-
|
|
|
|
| 204 |
const userDir = path.join(CLOUD_DISK_PATH, 'users', username);
|
| 205 |
if (!fs.existsSync(userDir)) return 0;
|
| 206 |
let total = 0;
|
| 207 |
-
|
| 208 |
-
const
|
| 209 |
-
for (
|
| 210 |
-
const
|
| 211 |
-
const stat = fs.
|
| 212 |
-
if (stat.isDirectory())
|
| 213 |
else total += stat.size;
|
| 214 |
}
|
| 215 |
-
}
|
| 216 |
-
|
| 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 |
|