index.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // 导出工具函数
  2. export { cn } from './cn';
  3. export {
  4. MinIOXHRMultipartUploader,
  5. MinIOXHRUploader,
  6. uploadMinIOWithPolicy,
  7. getUploadPolicy,
  8. getMultipartUploadPolicy,
  9. type MinioProgressEvent,
  10. type MinioProgressCallbacks,
  11. type UploadResult
  12. } from './minio';
  13. // 格式化文件大小
  14. export const formatFileSize = (bytes: number): string => {
  15. if (bytes === 0) return '0 Bytes';
  16. const k = 1024;
  17. const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  18. const i = Math.floor(Math.log(bytes) / Math.log(k));
  19. return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
  20. };
  21. // 检查文件类型是否可预览
  22. export const isPreviewableFileType = (fileType: string | null): boolean => {
  23. if (!fileType) return false;
  24. return fileType.startsWith('image/') || fileType.startsWith('video/');
  25. };
  26. // 获取文件图标类型
  27. export const getFileIconType = (fileType: string): 'image' | 'video' | 'audio' | 'pdf' | 'document' | 'spreadsheet' | 'text' | 'other' => {
  28. if (fileType.startsWith('image/')) return 'image';
  29. if (fileType.startsWith('video/')) return 'video';
  30. if (fileType.startsWith('audio/')) return 'audio';
  31. if (fileType.includes('pdf')) return 'pdf';
  32. if (fileType.includes('word') || fileType.includes('document')) return 'document';
  33. if (fileType.includes('excel') || fileType.includes('sheet')) return 'spreadsheet';
  34. if (fileType.includes('text')) return 'text';
  35. return 'other';
  36. };
  37. // 验证文件类型
  38. export const validateFileType = (file: File, accept?: string): boolean => {
  39. if (!accept || accept === '*/*') return true;
  40. const acceptTypes = accept.split(',').map(type => type.trim());
  41. return acceptTypes.some(type => {
  42. if (type.startsWith('.')) {
  43. // 文件扩展名匹配
  44. const extension = type.toLowerCase();
  45. const fileName = file.name.toLowerCase();
  46. return fileName.endsWith(extension);
  47. } else {
  48. // MIME类型匹配
  49. return file.type.match(new RegExp(type.replace('*', '.*')));
  50. }
  51. });
  52. };
  53. // 验证文件大小
  54. export const validateFileSize = (file: File, maxSizeMB: number): boolean => {
  55. const maxSizeBytes = maxSizeMB * 1024 * 1024;
  56. return file.size <= maxSizeBytes;
  57. };