export function downloadUrl(item) { return new URL(item.url, window.location.origin).href; } export function forcedDownloadUrl(item) { const url = new URL(downloadUrl(item)); url.searchParams.set('dl', '1'); return url.href; } export function displayName(item) { return item.downloadName || item.originalName || 'file'; } export function fileExtension(item) { const name = displayName(item); const dot = name.lastIndexOf('.'); if (dot > 0 && dot < name.length - 1) return name.slice(dot + 1).toUpperCase(); return ''; } export function linkLabel(item) { return item.originalName || item.downloadName || 'file'; } export function buildLinkFormats(item) { const url = downloadUrl(item); const name = linkLabel(item); const mime = item.mimeType || ''; const isImage = mime.startsWith('image/') && mime !== 'image/svg+xml'; let html; let bbcode; let markdown; if (isImage) { html = `${name}`; bbcode = `[img]${url}[/img]`; markdown = `![${name}](${url})`; } else { html = `${name}`; bbcode = `[url=${url}]${name}[/url]`; markdown = `[${name}](${url})`; } return [ { label: 'Direct', value: url }, { label: 'HTML', value: html }, { label: 'BBCode', value: bbcode }, { label: 'Markdown', value: markdown } ]; } export function formatBytes(bytes) { if (!Number.isFinite(bytes) || bytes < 0) return '-'; if (bytes < 1024) return `${bytes} B`; const units = ['KB', 'MB', 'GB', 'TB']; let value = bytes; let i = -1; do { value /= 1024; i += 1; } while (value >= 1024 && i < units.length - 1); return `${value.toFixed(value >= 100 ? 0 : 1)} ${units[i]}`; } export function formatDate(iso) { try { return new Date(iso).toLocaleString(); } catch { return ''; } } export async function copyText(text) { const fallback = () => { const ta = document.createElement('textarea'); ta.value = text; ta.style.position = 'fixed'; ta.style.opacity = '0'; document.body.appendChild(ta); ta.select(); let ok = false; try { ok = document.execCommand('copy'); } catch { ok = false; } ta.remove(); return ok; }; if (navigator.clipboard && window.isSecureContext) { try { await navigator.clipboard.writeText(text); return true; } catch { return fallback(); } } return fallback(); }