API & Client - display uploaded files size in admin + minor changes

This commit is contained in:
Sam
2020-05-01 20:18:19 +02:00
parent abca323936
commit 5aa1d77553
12 changed files with 119 additions and 39 deletions

View File

@ -2,12 +2,14 @@ import { format, parse } from 'date-fns'
import { DateTime } from 'luxon'
const suffixes = ['bytes', 'KB', 'MB', 'GB', 'TB']
export const getFileSize = fileSize => {
export const getFileSize = (fileSize, asText = true) => {
const i = Math.floor(Math.log(fileSize) / Math.log(1024))
return (
(!fileSize && '0 bytes') ||
`${(fileSize / Math.pow(1024, i)).toFixed(1)}${suffixes[i]}`
)
if (!fileSize) {
return asText ? '0 bytes' : { size: 0, suffix: 'bytes' }
}
const size = (fileSize / Math.pow(1024, i)).toFixed(1)
const suffix = suffixes[i]
return asText ? `${size}${suffix}` : { size, suffix }
}
export const getFileSizeInMB = fileSize => {