Skip to content

Commit

Permalink
fix: reject if image is not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Feb 26, 2023
1 parent aa42fb1 commit 4fb265c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ crate-type = ["cdylib"]
anyhow = "1"
base64 = "0.21"
cssparser = "0.29"
infer = "0.12"
napi = { version = "2", default-features = false, features = [
"napi3",
"serde-json",
Expand Down
1 change: 1 addition & 0 deletions __test__/fixtures/broken.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion __test__/image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { promises as fs } from 'fs'
import { join } from 'path'
import test from 'ava'

import { createCanvas, Image } from '../index'
import { createCanvas, Image, loadImage } from '../index'

import { snapshotImage } from './image-snapshot'

Expand Down Expand Up @@ -88,3 +88,7 @@ test('svg-transparent-background', async (t) => {

await snapshotImage(t, { canvas })
})

test('load invalid image should throw error', async (t) => {
await t.throwsAsync(() => loadImage(join(__dirname, 'fixtures', 'broken.png')))
})
2 changes: 1 addition & 1 deletion load-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = async function loadImage(source, options = {}) {
}
}

// throw error as dont support that source
// throw error as don't support that source
throw new TypeError('unsupported image source')
}

Expand Down
24 changes: 19 additions & 5 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,29 @@ impl Image {
let image_binary = STANDARD
.decode(base64_str)
.map_err(|e| Error::new(Status::InvalidArg, format!("Decode data url failed {e}")))?;
Some(Bitmap::from_buffer(
image_binary.as_ptr() as *mut u8,
image_binary.len(),
))
if let Some(kind) = infer::get(&image_binary) {
if kind.matcher_type() == infer::MatcherType::Image {
Some(Bitmap::from_buffer(
image_binary.as_ptr() as *mut u8,
image_binary.len(),
))
} else {
return Err(Error::new(Status::InvalidArg, "Unsupported image type"));
}
} else {
return Err(Error::new(Status::InvalidArg, "Unsupported image type"));
}
} else {
None
}
} else if let Some(kind) = infer::get(&data) {
if kind.matcher_type() == infer::MatcherType::Image {
Some(Bitmap::from_buffer(data.as_ptr() as *mut u8, length))
} else {
return Err(Error::new(Status::InvalidArg, "Unsupported image type"));
}
} else {
Some(Bitmap::from_buffer(data.as_ptr() as *mut u8, length))
return Err(Error::new(Status::InvalidArg, "Unsupported image type"));
};
if let Some(ref b) = bitmap {
if (self.width - -1.0).abs() < f64::EPSILON {
Expand Down

1 comment on commit 4fb265c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 4fb265c Previous: aa42fb1 Ratio
Draw house#skia-canvas 26 ops/sec (±0.09%) 26 ops/sec (±0.22%) 1
Draw house#node-canvas 23 ops/sec (±0.2%) 23.3 ops/sec (±0.25%) 1.01
Draw house#@napi-rs/skia 24 ops/sec (±0.36%) 23.1 ops/sec (±0.99%) 0.96
Draw gradient#skia-canvas 25 ops/sec (±0.08%) 25 ops/sec (±0.06%) 1
Draw gradient#node-canvas 22 ops/sec (±0.26%) 22.1 ops/sec (±0.2%) 1.00
Draw gradient#@napi-rs/skia 23 ops/sec (±0.21%) 22.4 ops/sec (±0.35%) 0.97

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.