release: v1.1.0 — S3 multi-backend storage and admin UI
ci / ci (22, ubuntu-latest) (push) Canceled after 0s
Publish Docker image / build (linux/amd64, ubuntu-latest, amd64) (push) Canceled after 0s
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm, arm64) (push) Canceled after 0s
Publish Docker image / merge (push) Canceled after 0s
GitHub Release / release (push) Canceled after 0s
ci / ci (22, ubuntu-latest) (push) Canceled after 0s
Publish Docker image / build (linux/amd64, ubuntu-latest, amd64) (push) Canceled after 0s
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm, arm64) (push) Canceled after 0s
Publish Docker image / merge (push) Canceled after 0s
GitHub Release / release (push) Canceled after 0s
This commit is contained in:
1 parent
7c45a78572
commit
acc64751ef
46 files changed
+4005
-480
No files matched your search
@@ -1,10 +1,55 @@
|
||||
export function useFileSize() {
|
||||
function formatFileSize(bytes: number): string {
|
||||
if (!Number.isFinite(bytes) || bytes < 0) return '-'
|
||||
if (bytes < 1024) return `${bytes} B`
|
||||
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
|
||||
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`
|
||||
export const QUOTA_UNITS = ['KB', 'MB', 'GB', 'TB'] as const
|
||||
export type QuotaUnit = typeof QUOTA_UNITS[number]
|
||||
|
||||
export function formatFileSize(bytes: number): string {
|
||||
if (!Number.isFinite(bytes) || bytes < 0) return '-'
|
||||
if (bytes === 0) return '0 B'
|
||||
if (bytes < 1024) return `${bytes} B`
|
||||
|
||||
const units = ['KB', 'MB', 'GB', 'TB'] as const
|
||||
let value = bytes / 1024
|
||||
let unitIndex = 0
|
||||
|
||||
while (unitIndex < units.length - 1 && value >= 1024) {
|
||||
value /= 1024
|
||||
unitIndex++
|
||||
}
|
||||
|
||||
return { formatFileSize }
|
||||
const decimals = value >= 100 ? 0 : value >= 10 ? 1 : 2
|
||||
return `${value.toFixed(decimals)} ${units[unitIndex]}`
|
||||
}
|
||||
|
||||
export function bytesToQuotaInput(bytes: number | null): { value: string, unit: QuotaUnit } {
|
||||
if (!bytes || bytes <= 0) {
|
||||
return { value: '', unit: 'GB' }
|
||||
}
|
||||
|
||||
let value = bytes / 1024
|
||||
let unitIndex = 0
|
||||
|
||||
while (unitIndex < QUOTA_UNITS.length - 1 && value >= 1024) {
|
||||
value /= 1024
|
||||
unitIndex++
|
||||
}
|
||||
|
||||
const decimals = value >= 100 ? 0 : value >= 10 ? 1 : 2
|
||||
const rounded = Number(value.toFixed(decimals))
|
||||
const unit: QuotaUnit = QUOTA_UNITS[unitIndex] ?? 'GB'
|
||||
return { value: String(rounded), unit }
|
||||
}
|
||||
|
||||
export function quotaToBytes(value: string | number, unit: QuotaUnit): number | null {
|
||||
const trimmed = String(value ?? '').trim()
|
||||
if (!trimmed) return null
|
||||
const amount = Number(trimmed)
|
||||
if (!Number.isFinite(amount) || amount <= 0) return null
|
||||
|
||||
const unitIndex = QUOTA_UNITS.indexOf(unit)
|
||||
if (unitIndex < 0) return null
|
||||
|
||||
return Math.round(amount * 1024 ** (unitIndex + 1))
|
||||
}
|
||||
|
||||
export function useFileSize() {
|
||||
return { formatFileSize, bytesToQuotaInput, quotaToBytes, QUOTA_UNITS }
|
||||
}
|
||||
Reference in new issue
Block a user