(sample: Uint8Array)
| 100 | } |
| 101 | |
| 102 | function isLikelyBinarySample(sample: Uint8Array): boolean { |
| 103 | if (sample.length === 0) return false |
| 104 | if (sample.includes(0)) return true |
| 105 | |
| 106 | try { |
| 107 | utf8Decoder.decode(sample) |
| 108 | } catch { |
| 109 | return true |
| 110 | } |
| 111 | |
| 112 | let suspiciousControlBytes = 0 |
| 113 | for (const byte of sample) { |
| 114 | if (byte < 0x20 && byte !== 0x08 && byte !== 0x09 && byte !== 0x0a && byte !== 0x0c && byte !== 0x0d) { |
| 115 | suspiciousControlBytes += 1 |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return suspiciousControlBytes / sample.length > 0.01 |
| 120 | } |
| 121 | |
| 122 | export function classifyFileMetadata(filePath: string, sample: Uint8Array): FileMetadata { |
| 123 | const mediaType = mediaTypeFromMagic(sample) ?? mediaTypeFromExtension(filePath) |
no outgoing calls
no test coverage detected