feat: calculate used storage

This commit is contained in:
Vasily Zubarev
2025-04-10 15:14:54 +02:00
parent ea8a3295e9
commit 62bad46e58
13 changed files with 87 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
import { File, Transaction, User } from "@prisma/client"
import { access, constants } from "fs/promises"
import { access, constants, readdir, stat } from "fs/promises"
import path from "path"
export const FILE_UPLOAD_PATH = path.resolve(process.env.UPLOAD_PATH || "./uploads")
@@ -52,3 +52,21 @@ export async function fileExists(filePath: string) {
return false
}
}
export async function getDirectorySize(directoryPath: string) {
let totalSize = 0
async function calculateSize(dir: string) {
const files = await readdir(dir, { withFileTypes: true })
for (const file of files) {
const fullPath = path.join(dir, file.name)
if (file.isDirectory()) {
await calculateSize(fullPath)
} else if (file.isFile()) {
const stats = await stat(fullPath)
totalSize += stats.size
}
}
}
await calculateSize(directoryPath)
return totalSize
}