diff --git a/__test__/index.spec.mjs b/__test__/index.spec.mjs index fa82944..bf43f2d 100644 --- a/__test__/index.spec.mjs +++ b/__test__/index.spec.mjs @@ -43,3 +43,23 @@ for (const params of [ ]) { test(testEncode, ...params); } + +test('encode too small', (t) => { + let buffer = fs.readFileSync('images/invalid_small.png'); + let encoded = encodeImage(buffer); + let mchash = Buffer.from(encoded.minecraftHash).toString('hex'); + let error = encoded.error; + console.log(error); + t.is(mchash, ''); + t.true(error.includes('too small')); +}) + +test('encode too big', (t) => { + let buffer = fs.readFileSync('images/invalid_big.png'); + let encoded = encodeImage(buffer); + let mchash = Buffer.from(encoded.minecraftHash).toString('hex'); + let error = encoded.error; + console.log(error); + t.is(mchash, ''); + t.true(error.includes('too small')); // not sure why it says too small +}) \ No newline at end of file diff --git a/images/invalid_big.png b/images/invalid_big.png new file mode 100644 index 0000000..347062a Binary files /dev/null and b/images/invalid_big.png differ diff --git a/images/invalid_small.png b/images/invalid_small.png new file mode 100644 index 0000000..2166734 Binary files /dev/null and b/images/invalid_small.png differ diff --git a/index.d.ts b/index.d.ts index b1e035c..2d65656 100644 --- a/index.d.ts +++ b/index.d.ts @@ -9,4 +9,5 @@ export declare class ImageWithHashes { png: Buffer minecraftHash: Buffer hash: Buffer + error?: string } diff --git a/src/lib.rs b/src/lib.rs index 3f331da..fb02584 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,7 @@ const SKIN_DATA_LENGTH: usize = (SKIN_WIDTH * SKIN_HEIGHT * SKIN_CHANNELS); #[napi] pub fn sum(a: i32, b: i32) -> i32 { - a + b + a + b } #[napi] @@ -23,6 +23,7 @@ pub struct ImageWithHashes { pub png: Buffer, pub minecraft_hash: Buffer, pub hash: Buffer, + pub error: Option, } #[napi] @@ -37,6 +38,15 @@ pub fn encode_custom_image(buffer: &[u8], width: usize, height: usize) -> ImageW decoder.info_png_mut().interlace_method = 0; // should be 0 but just to be sure let decoded = decoder.decode(buffer); + if decoded.is_err() { + let error = decoded.unwrap_err(); + return ImageWithHashes { + png: Buffer::from(vec![]), + minecraft_hash: Buffer::from(vec![]), + hash: Buffer::from(vec![]), + error: Some(format!("{:?}", error)), + }; + } let decoded1 = decoded.unwrap(); let decoded_data = decoded1.bytes(); @@ -58,6 +68,15 @@ pub fn encode_custom_image(buffer: &[u8], width: usize, height: usize) -> ImageW let result = encoder.encode(&raw_data, width, height); // println!("Result: {:?}", result); + if result.is_err() { + let error = result.unwrap_err(); + return ImageWithHashes { + png: Buffer::from(vec![]), + minecraft_hash: Buffer::from(vec![]), + hash: Buffer::from(vec![]), + error: Some(format!("{:?}", error)), + }; + } let png = result.unwrap(); let mut hasher = Sha256::new(); @@ -72,6 +91,7 @@ pub fn encode_custom_image(buffer: &[u8], width: usize, height: usize) -> ImageW ImageWithHashes { png: Buffer::from(png.as_slice()), minecraft_hash: Buffer::from(minecraft_hash.as_slice()), - hash: Buffer::from(hash.as_slice()) + hash: Buffer::from(hash.as_slice()), + error: None, } } diff --git a/tools/randomImage.js b/tools/randomImage.js index a2c7b71..1c17fd8 100644 --- a/tools/randomImage.js +++ b/tools/randomImage.js @@ -33,6 +33,6 @@ function makeRandomImage(width = 64, height = 64) { } (() => { - const { imageBuffer, imageData, data, blob } = makeRandomImage(64, 64); + const { imageBuffer, imageData, data, blob } = makeRandomImage(128, 128); fs.writeFileSync(`images/randomImage${Math.round(Math.random()*100)}.png`, imageBuffer); })(); \ No newline at end of file