import { DocumentBlockFile } from '@gitbook/api'; import { getSimplifiedContentType } from '@/lib/files'; import { tcls } from '@/lib/tailwind'; import { BlockProps } from './Block'; import { FileIcon } from './FileIcon'; export async function File(props: BlockProps) { const { block, context, style } = props; const contentRef = await context.resolveContentRef(block.data.ref); const file = contentRef?.file; if (!file) { return null; } const contentType = getSimplifiedContentType(file.contentType); return (
{getHumanFileSize(file.size)}
{file.name}
{contentType}
); } const ONE_KB = 1024; const ONE_MB = ONE_KB * 1024; /** * Return a file size as human readable formatted string. */ function getHumanFileSize(size: number): string { if (size > ONE_MB) { const mbSize = size / ONE_MB; return `${mbSize.toFixed(0)}MB`; } if (size > ONE_KB) { const kbSize = size / ONE_KB; return `${kbSize.toFixed(0)}KB`; } return `${size}B`; }