// lib/filePreview.ts:由 lib/utils.ts 按职责拆分(实现未改) import { getToken } from '/@/utils/auth/index'; import type { AttachedFile } from '/@/views/ventAI/manageAssistent/types/resource'; import { isImageFile, isPdfFile, isWordFile, isExcelFile } from './fileIcon'; export const fetchPreviewFile = async (url: string, filename: string): Promise => { const resp = await fetch(url, { headers: { 'X-Access-Token': getToken() } }); if (!resp.ok) throw new Error(`HTTP ${resp.status}`); const ext = filename.split('.').pop()?.toLowerCase() || ''; const mimeMap: Record = { md: 'text/markdown', txt: 'text/plain', json: 'application/json', xml: 'text/xml', yaml: 'text/yaml', yml: 'text/yaml', csv: 'text/csv', // .dxf 是明文,按文本取回后由预览面板本地解析渲染 dxf: 'text/plain', }; const id = `report-${url}`; const uploadTime = new Date().toISOString(); if (mimeMap[ext]) { const content = await resp.text(); return { id, name: filename, size: content.length, type: mimeMap[ext], content, uploadTime }; } if (isWordFile(filename) || isExcelFile(filename)) { const arrayBuffer = await resp.arrayBuffer(); return { id, name: filename, size: arrayBuffer.byteLength, type: 'application/octet-stream', arrayBuffer, uploadTime }; } if (isImageFile(filename) || isPdfFile(filename)) { const blob = await resp.blob(); const dataUrl = await new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => resolve(reader.result as string); reader.onerror = () => reject(reader.error); reader.readAsDataURL(blob); }); return { id, name: filename, size: blob.size, type: blob.type, preview: dataUrl, uploadTime }; } return null; };