home-lab/backend/lib/utils.ts

32 lines
956 B
TypeScript

import { Mime } from "mime/lite";
import standardTypes from "mime/types/standard.js";
import otherTypes from "mime/types/other.js";
export const formatBytes = (bytes: number) => {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
if (bytes === 0) return "n/a";
const i = parseInt(String(Math.floor(Math.log(bytes) / Math.log(1024))), 10);
if (i === 0) return `${bytes} ${sizes[i]}`;
return `${(bytes / 1024 ** i).toFixed(2)} ${sizes[i]}`;
};
export const secondsToTime = (seconds: number) => {
const d = Math.floor(seconds / (3600 * 24));
const h = Math.floor((seconds % (3600 * 24)) / 3600);
const m = Math.floor((seconds % 3600) / 60);
// const s = Math.floor(seconds % 60);
return `${d}d ${h}h ${m}m`;
};
export const mime = new Mime(standardTypes, otherTypes);
mime.define(
{
"video/webm": ["mkv"],
},
true
);
export const getMimeType = (path: string) => {
return mime.getType(path) || "application/octet-stream";
};