108 lines
3.5 KiB
TypeScript
Executable File
108 lines
3.5 KiB
TypeScript
Executable File
|
|
/**
|
|
* 格式化字节大小
|
|
*/
|
|
export function formatBytes(bytes: number): string {
|
|
if (bytes === 0) return '0 B';
|
|
|
|
const k = 1024;
|
|
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(1))} ${sizes[i]}`;
|
|
}
|
|
|
|
/**
|
|
* 格式化日期
|
|
*/
|
|
export function formatDate(date: Date): string {
|
|
const now = new Date();
|
|
const diffTime = Math.abs(now.getTime() - date.getTime());
|
|
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
|
|
|
|
if (diffDays === 1) {
|
|
return '昨天';
|
|
} else if (diffDays <= 7) {
|
|
return `${diffDays} 天前`;
|
|
} else if (diffDays <= 30) {
|
|
const weeks = Math.floor(diffDays / 7);
|
|
return `${weeks} 周前`;
|
|
} else if (diffDays <= 365) {
|
|
const months = Math.floor(diffDays / 30);
|
|
return `${months} 个月前`;
|
|
} else {
|
|
return date.toLocaleDateString('zh-CN', {
|
|
year: 'numeric',
|
|
month: 'short',
|
|
day: 'numeric',
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 格式化文件类型显示名称
|
|
*/
|
|
export function getFileTypeName(mimeType?: string): string {
|
|
if (!mimeType) return '未知类型';
|
|
|
|
const typeMap: Record<string, string> = {
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'Word 文档',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'Excel 表格',
|
|
'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'PowerPoint 演示文稿',
|
|
'application/pdf': 'PDF 文档',
|
|
'text/plain': '文本文件',
|
|
'image/jpeg': 'JPEG 图片',
|
|
'image/png': 'PNG 图片',
|
|
'image/gif': 'GIF 图片',
|
|
'image/svg+xml': 'SVG 图片',
|
|
'video/mp4': 'MP4 视频',
|
|
'video/avi': 'AVI 视频',
|
|
'audio/mp3': 'MP3 音频',
|
|
'audio/wav': 'WAV 音频',
|
|
'application/zip': 'ZIP 压缩包',
|
|
'application/x-rar-compressed': 'RAR 压缩包',
|
|
};
|
|
|
|
if (typeMap[mimeType]) {
|
|
return typeMap[mimeType];
|
|
}
|
|
|
|
// 回退到通用类型判断
|
|
if (mimeType.startsWith('image/')) return '图片';
|
|
if (mimeType.startsWith('video/')) return '视频';
|
|
if (mimeType.startsWith('audio/')) return '音频';
|
|
if (mimeType.startsWith('text/')) return '文本';
|
|
|
|
return '文件';
|
|
}
|
|
|
|
/**
|
|
* 根据文件扩展名获取MIME类型
|
|
*/
|
|
export function getMimeTypeFromExtension(filename: string): string {
|
|
const ext = filename.split('.').pop()?.toLowerCase();
|
|
|
|
const extMap: Record<string, string> = {
|
|
'pdf': 'application/pdf',
|
|
'doc': 'application/msword',
|
|
'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
'xls': 'application/vnd.ms-excel',
|
|
'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
'ppt': 'application/vnd.ms-powerpoint',
|
|
'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
'txt': 'text/plain',
|
|
'jpg': 'image/jpeg',
|
|
'jpeg': 'image/jpeg',
|
|
'png': 'image/png',
|
|
'gif': 'image/gif',
|
|
'svg': 'image/svg+xml',
|
|
'mp4': 'video/mp4',
|
|
'avi': 'video/avi',
|
|
'mp3': 'audio/mp3',
|
|
'wav': 'audio/wav',
|
|
'zip': 'application/zip',
|
|
'rar': 'application/x-rar-compressed',
|
|
};
|
|
|
|
return ext ? extMap[ext] || 'application/octet-stream' : 'application/octet-stream';
|
|
}
|