mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
fix
This commit is contained in:
@@ -23,10 +23,14 @@ interface ImageParser {
|
|||||||
|
|
||||||
// 魔术匹配
|
// 魔术匹配
|
||||||
function matchMagic(buffer: Buffer, magic: number[], offset = 0): boolean {
|
function matchMagic(buffer: Buffer, magic: number[], offset = 0): boolean {
|
||||||
if (buffer.length < offset + magic.length) return false;
|
if (buffer.length < offset + magic.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
for (let i = 0; i < magic.length; i++) {
|
for (let i = 0; i < magic.length; i++) {
|
||||||
if (buffer[offset + i] !== magic[i]) return false;
|
if (buffer[offset + i] !== magic[i]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -46,7 +50,9 @@ class PngParser implements ImageParser {
|
|||||||
stream.once('error', reject);
|
stream.once('error', reject);
|
||||||
stream.once('readable', () => {
|
stream.once('readable', () => {
|
||||||
const buf = stream.read(24) as Buffer;
|
const buf = stream.read(24) as Buffer;
|
||||||
if (!buf || buf.length < 24) return resolve(undefined);
|
if (!buf || buf.length < 24) {
|
||||||
|
return resolve(undefined);
|
||||||
|
}
|
||||||
if (this.canParse(buf)) {
|
if (this.canParse(buf)) {
|
||||||
const width = buf.readUInt32BE(16);
|
const width = buf.readUInt32BE(16);
|
||||||
const height = buf.readUInt32BE(20);
|
const height = buf.readUInt32BE(20);
|
||||||
@@ -229,7 +235,9 @@ class GifParser implements ImageParser {
|
|||||||
stream.once('error', reject);
|
stream.once('error', reject);
|
||||||
stream.once('readable', () => {
|
stream.once('readable', () => {
|
||||||
const buf = stream.read(10) as Buffer;
|
const buf = stream.read(10) as Buffer;
|
||||||
if (!buf || buf.length < 10) return resolve(undefined);
|
if (!buf || buf.length < 10) {
|
||||||
|
return resolve(undefined);
|
||||||
|
}
|
||||||
if (this.canParse(buf)) {
|
if (this.canParse(buf)) {
|
||||||
const width = buf.readUInt16LE(6);
|
const width = buf.readUInt16LE(6);
|
||||||
const height = buf.readUInt16LE(8);
|
const height = buf.readUInt16LE(8);
|
||||||
@@ -385,7 +393,9 @@ export async function imageSizeFromFile(filePath: string): Promise<ImageSize | u
|
|||||||
// 先检测类型
|
// 先检测类型
|
||||||
const type = await detectImageType(filePath);
|
const type = await detectImageType(filePath);
|
||||||
const parser = parsers.find(p => p.type === type);
|
const parser = parsers.find(p => p.type === type);
|
||||||
if (!parser) return undefined;
|
if (!parser) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
// 用流式方式解析尺寸
|
// 用流式方式解析尺寸
|
||||||
const stream = fs.createReadStream(filePath);
|
const stream = fs.createReadStream(filePath);
|
||||||
|
Reference in New Issue
Block a user