| 4 | import path from 'path' |
| 5 | |
| 6 | export class ImageInfoFetchTask implements TaskItem { |
| 7 | /** |
| 8 | * 本地图片路径 |
| 9 | */ |
| 10 | path = '' |
| 11 | /** |
| 12 | * 文件大小 |
| 13 | */ |
| 14 | size: number = 0 |
| 15 | /** |
| 16 | * 格式化后的文件大小字符串 |
| 17 | */ |
| 18 | sizeFormatted: string = '' |
| 19 | /** |
| 20 | * 图片宽度 |
| 21 | */ |
| 22 | width: number = 0 |
| 23 | /** |
| 24 | * 图片高度 |
| 25 | */ |
| 26 | height: number = 0 |
| 27 | /** |
| 28 | * 图片格式 jpeg png gif webp等 |
| 29 | */ |
| 30 | ext = '' |
| 31 | /** |
| 32 | * 文件状态 |
| 33 | */ |
| 34 | hasError: boolean = false |
| 35 | /** |
| 36 | * 错误信息 |
| 37 | */ |
| 38 | errorMessage: string = '' |
| 39 | |
| 40 | constructor(path: string) { |
| 41 | this.path = path |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * 格式化文件大小 |
| 46 | */ |
| 47 | private formatFileSize(bytes: number): string { |
| 48 | if (bytes === 0) return '0 B' |
| 49 | |
| 50 | const units = ['B', 'KB', 'MB', 'GB', 'TB'] |
| 51 | const k = 1024 |
| 52 | const i = Math.floor(Math.log(bytes) / Math.log(k)) |
| 53 | |
| 54 | return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${units[i]}` |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * 检查文件是否存在且可读 |
| 59 | */ |
| 60 | private async checkFileAccessibility(): Promise<boolean> { |
| 61 | try { |
| 62 | await fs.access(this.path, fs.constants.R_OK) |
| 63 | return true |
nothing calls this directly
no outgoing calls
no test coverage detected