mirror of
https://github.com/marcogll/TaxHacker_s23.git
synced 2026-01-13 13:25:18 +00:00
feat: calculate used storage
This commit is contained in:
@@ -17,6 +17,7 @@ export type UserProfile = {
|
||||
name: string
|
||||
email: string
|
||||
avatar?: string
|
||||
storageUsed?: number
|
||||
}
|
||||
|
||||
export const auth = betterAuth({
|
||||
|
||||
20
lib/files.ts
20
lib/files.ts
@@ -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
|
||||
}
|
||||
|
||||
12
lib/utils.ts
12
lib/utils.ts
@@ -16,6 +16,18 @@ export function formatCurrency(total: number, currency: string) {
|
||||
}).format(total / 100)
|
||||
}
|
||||
|
||||
export function formatBytes(bytes: number) {
|
||||
if (bytes === 0) return "0 Bytes"
|
||||
|
||||
const sizes = ["Bytes", "KB", "MB", "GB"]
|
||||
const maxIndex = sizes.length - 1
|
||||
|
||||
const i = Math.min(Math.floor(Math.log10(bytes) / Math.log10(1024)), maxIndex)
|
||||
const value = bytes / Math.pow(1024, i)
|
||||
|
||||
return `${parseFloat(value.toFixed(2))} ${sizes[i]}`
|
||||
}
|
||||
|
||||
export function codeFromName(name: string, maxLength: number = 16) {
|
||||
const code = slugify(name, {
|
||||
replacement: "_",
|
||||
|
||||
Reference in New Issue
Block a user