68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
export function determineFileType(inputString: string): "file" | "folder" {
|
|
if (inputString.includes('.') && !inputString.endsWith('.')) {
|
|
return 'file';
|
|
} else {
|
|
return 'folder';
|
|
}
|
|
}
|
|
|
|
export function formatSize(size: number) {
|
|
for (const unit of [null, 'kB', 'MB', 'GB', 'TB', 'PB', 'EB']) {
|
|
if (size < 1e5) return size.toLocaleString().replace(',', '\u202F') + (unit ? `\u202F${unit}` : '')
|
|
size = Math.round(size / 1000)
|
|
}
|
|
return "huge"
|
|
}
|
|
|
|
export function formatUnixDate(t: number) {
|
|
const date = new Date(t * 1000)
|
|
const now = new Date()
|
|
const diff = date.getTime() - now.getTime()
|
|
const formatter = new Intl.RelativeTimeFormat('en', { numeric:
|
|
'auto' })
|
|
if (Math.abs(diff) <= 5000) {
|
|
return 'now'
|
|
}
|
|
if (Math.abs(diff) <= 60000) {
|
|
return formatter.format(Math.round(diff / 1000), 'second')
|
|
}
|
|
|
|
if (Math.abs(diff) <= 3600000) {
|
|
return formatter.format(Math.round(diff / 60000), 'minute')
|
|
}
|
|
|
|
if (Math.abs(diff) <= 86400000) {
|
|
return formatter.format(Math.round(diff / 3600000), 'hour')
|
|
}
|
|
|
|
if (Math.abs(diff) <= 604800000) {
|
|
return formatter.format(Math.round(diff / 86400000), 'day')
|
|
}
|
|
|
|
return date.toLocaleDateString(undefined, { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric' })
|
|
}
|
|
|
|
export function getFileExtension(filename: string) {
|
|
const parts = filename.split(".");
|
|
if (parts.length > 1) {
|
|
return parts[parts.length - 1];
|
|
} else {
|
|
return ""; // No hay extensión
|
|
}
|
|
}
|
|
export function getFileType(extension: string): string {
|
|
const videoExtensions = ["mp4", "avi", "mkv", "mov"];
|
|
const imageExtensions = ["jpg", "jpeg", "png", "gif"];
|
|
const pdfExtensions = ["pdf"];
|
|
|
|
if (videoExtensions.includes(extension)) {
|
|
return "video";
|
|
} else if (imageExtensions.includes(extension)) {
|
|
return "image";
|
|
} else if (pdfExtensions.includes(extension)) {
|
|
return "pdf";
|
|
} else {
|
|
return "unknown";
|
|
}
|
|
}
|