Files
jinshen-website/app/utils/file.ts
R2m1liA 75d4d40d39
All checks were successful
deploy to server / build-and-deploy (push) Successful in 4m18s
fix: 修改文件拓展名获取逻辑
- 若无扩展名则返回空字符串
2025-10-25 15:30:23 +08:00

22 lines
718 B
TypeScript

export function formatFileSize(sizeInBytes: number): string {
if (sizeInBytes < 1024) {
return `${sizeInBytes.toFixed(2)} B`;
} else if (sizeInBytes < 1024 * 1024) {
const sizeInKB = sizeInBytes / 1024;
return `${sizeInKB.toFixed(2)} KB`;
} else {
const sizeInMB = sizeInBytes / 1024 / 1024;
return `${sizeInMB.toFixed(2)} MB`;
}
}
export function getFileExtension(filename: string): string {
const lastDotIndex = filename.lastIndexOf('.');
if (lastDotIndex === -1) return ''; // 找不到拓展名
return filename.slice(lastDotIndex + 1);
}
export function formatFileExtension(ext: string): string {
return ext.startsWith('.') ? ext.slice(1).toUpperCase() : ext.toUpperCase();
}