Skip to content

Commit

Permalink
handle encode/decode errors
Browse files Browse the repository at this point in the history
  • Loading branch information
InventivetalentDev committed Oct 11, 2024
1 parent 81d67da commit 0230620
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
20 changes: 20 additions & 0 deletions __test__/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Binary file added images/invalid_big.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/invalid_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export declare class ImageWithHashes {
png: Buffer
minecraftHash: Buffer
hash: Buffer
error?: string
}
24 changes: 22 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ 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]
pub struct ImageWithHashes {
pub png: Buffer,
pub minecraft_hash: Buffer,
pub hash: Buffer,
pub error: Option<String>,
}

#[napi]
Expand All @@ -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();

Expand All @@ -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();
Expand All @@ -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,
}
}
2 changes: 1 addition & 1 deletion tools/randomImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})();

0 comments on commit 0230620

Please sign in to comment.