2026-01-15 06:01:34 +08:00
|
|
|
import {
|
|
|
|
|
FileTextFilled,
|
|
|
|
|
FileMarkdownFilled,
|
|
|
|
|
FilePdfFilled,
|
|
|
|
|
FileWordFilled,
|
|
|
|
|
FileExcelFilled,
|
|
|
|
|
FileImageFilled,
|
2026-05-17 18:06:25 +08:00
|
|
|
FileFilled,
|
2026-01-27 15:42:48 +08:00
|
|
|
FilePptFilled,
|
2026-05-17 18:06:25 +08:00
|
|
|
FileZipFilled,
|
2026-05-21 23:06:33 +08:00
|
|
|
LinkOutlined,
|
2026-05-17 18:06:25 +08:00
|
|
|
CodeFilled
|
2026-01-15 06:01:34 +08:00
|
|
|
} from '@ant-design/icons-vue'
|
2026-05-24 00:46:35 +08:00
|
|
|
import { getPreviewFileExtension, getPreviewTypeByPath } from '@/utils/file_preview'
|
2026-01-15 06:01:34 +08:00
|
|
|
import { formatRelative, parseToShanghai } from '@/utils/time'
|
2025-08-05 18:00:45 +08:00
|
|
|
|
2026-05-17 18:06:25 +08:00
|
|
|
const DEFAULT_FILE_ICON = { icon: FileFilled, color: 'var(--gray-600)' }
|
|
|
|
|
const LINK_FILE_ICON = { icon: LinkOutlined, color: 'var(--color-info-500)' }
|
|
|
|
|
|
|
|
|
|
const FILE_ICON_CONFIG = {
|
|
|
|
|
txt: { icon: FileTextFilled, color: 'var(--color-info-500)' },
|
|
|
|
|
text: { icon: FileTextFilled, color: 'var(--color-info-500)' },
|
|
|
|
|
log: { icon: FileTextFilled, color: 'var(--color-info-500)' },
|
|
|
|
|
md: { icon: FileMarkdownFilled, color: 'var(--gray-700)' },
|
|
|
|
|
markdown: { icon: FileMarkdownFilled, color: 'var(--gray-700)' },
|
|
|
|
|
pdf: { icon: FilePdfFilled, color: 'var(--color-error-500)' },
|
|
|
|
|
doc: { icon: FileWordFilled, color: 'var(--color-info-700)' },
|
|
|
|
|
docx: { icon: FileWordFilled, color: 'var(--color-info-700)' },
|
|
|
|
|
xls: { icon: FileExcelFilled, color: 'var(--color-success-500)' },
|
|
|
|
|
xlsx: { icon: FileExcelFilled, color: 'var(--color-success-500)' },
|
|
|
|
|
csv: { icon: FileExcelFilled, color: 'var(--color-success-500)' },
|
|
|
|
|
ppt: { icon: FilePptFilled, color: 'var(--color-warning-700)' },
|
|
|
|
|
pptx: { icon: FilePptFilled, color: 'var(--color-warning-700)' },
|
2026-05-24 00:46:35 +08:00
|
|
|
apng: { icon: FileImageFilled, color: 'var(--color-accent-700)' },
|
|
|
|
|
avif: { icon: FileImageFilled, color: 'var(--color-accent-700)' },
|
2026-05-17 18:06:25 +08:00
|
|
|
jpg: { icon: FileImageFilled, color: 'var(--color-accent-700)' },
|
|
|
|
|
jpeg: { icon: FileImageFilled, color: 'var(--color-accent-700)' },
|
|
|
|
|
png: { icon: FileImageFilled, color: 'var(--color-accent-700)' },
|
|
|
|
|
gif: { icon: FileImageFilled, color: 'var(--color-accent-700)' },
|
|
|
|
|
bmp: { icon: FileImageFilled, color: 'var(--color-accent-700)' },
|
|
|
|
|
svg: { icon: FileImageFilled, color: 'var(--color-accent-700)' },
|
|
|
|
|
webp: { icon: FileImageFilled, color: 'var(--color-accent-700)' },
|
|
|
|
|
zip: { icon: FileZipFilled, color: 'var(--gray-700)' },
|
|
|
|
|
rar: { icon: FileZipFilled, color: 'var(--gray-700)' },
|
|
|
|
|
'7z': { icon: FileZipFilled, color: 'var(--gray-700)' },
|
|
|
|
|
tar: { icon: FileZipFilled, color: 'var(--gray-700)' },
|
|
|
|
|
gz: { icon: FileZipFilled, color: 'var(--gray-700)' },
|
|
|
|
|
py: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
js: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
jsx: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
ts: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
tsx: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
vue: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
sh: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
go: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
rs: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
cpp: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
c: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
h: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
java: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
html: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
htm: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
css: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
less: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
scss: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
sql: { icon: CodeFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
json: { icon: FileTextFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
yaml: { icon: FileTextFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
yml: { icon: FileTextFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
toml: { icon: FileTextFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
ini: { icon: FileTextFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
conf: { icon: FileTextFilled, color: 'var(--color-primary-700)' },
|
|
|
|
|
env: { icon: FileTextFilled, color: 'var(--color-primary-700)' }
|
2025-08-05 18:00:45 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-17 18:06:25 +08:00
|
|
|
const getFileIconConfig = (filename) => {
|
2026-05-31 13:40:15 +08:00
|
|
|
const normalizedName = String(filename || '')
|
|
|
|
|
.trim()
|
|
|
|
|
.toLowerCase()
|
2026-05-17 18:06:25 +08:00
|
|
|
if (!normalizedName) return DEFAULT_FILE_ICON
|
2026-05-31 13:40:15 +08:00
|
|
|
if (normalizedName.startsWith('http://') || normalizedName.startsWith('https://'))
|
|
|
|
|
return LINK_FILE_ICON
|
2025-08-05 18:00:45 +08:00
|
|
|
|
2026-05-17 18:06:25 +08:00
|
|
|
const cleanName = normalizedName.split(/[?#]/)[0]
|
|
|
|
|
const extension = cleanName.includes('.') ? cleanName.split('.').pop() : cleanName
|
|
|
|
|
return FILE_ICON_CONFIG[extension] || DEFAULT_FILE_ICON
|
|
|
|
|
}
|
2026-05-21 23:06:33 +08:00
|
|
|
|
2026-05-17 18:06:25 +08:00
|
|
|
export const getFileIcon = (filename) => getFileIconConfig(filename).icon
|
2025-08-05 18:00:45 +08:00
|
|
|
|
2026-05-17 18:06:25 +08:00
|
|
|
export const getFileIconColor = (filename) => getFileIconConfig(filename).color
|
2025-08-05 18:00:45 +08:00
|
|
|
|
2025-10-13 15:08:54 +08:00
|
|
|
export const formatRelativeTime = (value) => formatRelative(value)
|
2025-08-05 18:00:45 +08:00
|
|
|
|
2025-10-13 15:08:54 +08:00
|
|
|
export const formatStandardTime = (value) => {
|
|
|
|
|
const parsed = parseToShanghai(value)
|
|
|
|
|
if (!parsed) return '-'
|
|
|
|
|
return parsed.format('YYYY年MM月DD日 HH:mm:ss')
|
2025-08-05 18:00:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getStatusText = (status) => {
|
|
|
|
|
const statusMap = {
|
2026-01-15 06:01:34 +08:00
|
|
|
done: '处理完成',
|
|
|
|
|
failed: '处理失败',
|
|
|
|
|
processing: '处理中',
|
|
|
|
|
waiting: '等待处理'
|
2025-08-05 18:00:45 +08:00
|
|
|
}
|
2026-03-26 13:53:35 +08:00
|
|
|
return statusMap[status] || status
|
2026-01-04 21:35:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const formatFileSize = (bytes) => {
|
2026-01-15 06:01:34 +08:00
|
|
|
if (bytes === 0 || bytes === '0') return '0 B'
|
|
|
|
|
if (!bytes) return '-'
|
|
|
|
|
const k = 1024
|
|
|
|
|
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
|
|
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
|
2025-10-13 15:08:54 +08:00
|
|
|
}
|
2026-05-24 00:46:35 +08:00
|
|
|
|
|
|
|
|
export const getDisplayFileName = (pathOrName, fallback = '文件') => {
|
|
|
|
|
const value = String(pathOrName || '').trim()
|
|
|
|
|
if (!value) return fallback
|
|
|
|
|
return value.split('/').pop() || value || fallback
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getFileExtensionLabel = (pathOrName) => {
|
|
|
|
|
const extension = getPreviewFileExtension(pathOrName).replace(/^\./, '')
|
|
|
|
|
return extension ? extension.toUpperCase() : ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getMimeSubtypeLabel = (mimeType) => {
|
|
|
|
|
const subtype = String(mimeType || '')
|
|
|
|
|
.split('/')
|
|
|
|
|
.pop()
|
|
|
|
|
?.trim()
|
|
|
|
|
return subtype ? subtype.toUpperCase() : ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const normalizeAttachmentPreview = (attachment) => {
|
|
|
|
|
const name = getDisplayFileName(
|
|
|
|
|
attachment?.file_name || attachment?.name || attachment?.path,
|
|
|
|
|
'附件'
|
|
|
|
|
)
|
|
|
|
|
const fileId = attachment?.file_id || attachment?.path || name
|
|
|
|
|
const fileType = String(attachment?.file_type || '')
|
|
|
|
|
const sizeLabel = formatFileSize(attachment?.file_size)
|
|
|
|
|
const typeLabel = getFileExtensionLabel(name) || getMimeSubtypeLabel(fileType) || '文件'
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
raw: attachment,
|
|
|
|
|
fileId,
|
|
|
|
|
name,
|
|
|
|
|
previewUrl: attachment?.original_artifact_url || attachment?.artifact_url || '',
|
|
|
|
|
isImage: fileType.startsWith('image/') || getPreviewTypeByPath(name) === 'image',
|
|
|
|
|
meta: [typeLabel, sizeLabel === '-' ? '' : sizeLabel].filter(Boolean).join(' · '),
|
|
|
|
|
icon: getFileIcon(name),
|
|
|
|
|
iconColor: getFileIconColor(name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const normalizeAttachmentPreviews = (attachments) => {
|
|
|
|
|
if (!Array.isArray(attachments)) return []
|
|
|
|
|
return attachments.map(normalizeAttachmentPreview).filter((attachment) => attachment.fileId)
|
|
|
|
|
}
|