mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-04 14:41:14 +00:00
Move PNG/JPEG/BMP/GIF/WEBP parser implementations out of index.ts into dedicated files under packages/napcat-image-size/src/parser. Export ImageParser and matchMagic from index.ts, import the new BmpParser, GifParser, JpegParser, PngParser and WebpParser classes, and update the parsers array to instantiate those classes. Keeps existing parsing logic and stream-based size detection while cleaning up index.ts for modularity.
33 lines
969 B
TypeScript
33 lines
969 B
TypeScript
import { ImageParser, ImageType, matchMagic, ImageSize } from '@/napcat-image-size/src';
|
||
import { ReadStream } from 'fs';
|
||
|
||
// BMP解析器
|
||
export class BmpParser implements ImageParser {
|
||
readonly type = ImageType.BMP;
|
||
// BMP 魔术头:42 4D (BM)
|
||
private readonly BMP_SIGNATURE = [0x42, 0x4D];
|
||
|
||
canParse (buffer: Buffer): boolean {
|
||
return matchMagic(buffer, this.BMP_SIGNATURE);
|
||
}
|
||
|
||
async parseSize (stream: ReadStream): Promise<ImageSize | undefined> {
|
||
return new Promise((resolve, reject) => {
|
||
stream.once('error', reject);
|
||
stream.once('readable', () => {
|
||
const buf = stream.read(26) as Buffer;
|
||
if (!buf || buf.length < 26) {
|
||
return resolve(undefined);
|
||
}
|
||
if (this.canParse(buf)) {
|
||
const width = buf.readUInt32LE(18);
|
||
const height = buf.readUInt32LE(22);
|
||
resolve({ width, height });
|
||
} else {
|
||
resolve(undefined);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
}
|