Skip to content

Commit

Permalink
Support loading images from urls without extensions (#163)
Browse files Browse the repository at this point in the history
* Support loading images from urls without extensions

* clippy fix

* update cargo-bundle-licenses
  • Loading branch information
jonmmease authored Apr 14, 2024
1 parent e941288 commit 2ece4bc
Show file tree
Hide file tree
Showing 10 changed files with 218,178 additions and 19,002 deletions.
4 changes: 2 additions & 2 deletions DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Updating the CI build to use QEMU seems to have fixed the issue. Leaving this co
> The Deno dependencies currently correspond to Deno 1.30.3. When updating to later versions, we've run into errors for packages cross compiled to Linux aarch64. See https://github.com/vega/vl-convert/issues/52. Be sure to test this scenario when updating Deno in the future.
## Debug Logging
To enable logging, set the RUST_LOG environment variable to "vl_convert"
To enable logging, set the RUST_LOG environment variable to info, warn, or error
```
RUST_LOG=vl_convert
RUST_LOG=info
```
59,072 changes: 54,326 additions & 4,746 deletions thirdparty_rust.yaml

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions vl-convert-pdf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,7 @@ fn node_has_zero_opacity(node: &Node) -> bool {
}

fn get_text_width(text: &Text, font_db: &Database) -> Option<f64> {
let Some(node) = text.convert(font_db, Default::default()) else {
return None;
};
get_text_width_from_path(node)
get_text_width_from_path(text.convert(font_db, Default::default())?)
}

fn get_text_width_from_path(node: Node) -> Option<f64> {
Expand Down
59,072 changes: 54,326 additions & 4,746 deletions vl-convert-python/thirdparty_rust.yaml

Large diffs are not rendered by default.

41 changes: 29 additions & 12 deletions vl-convert-rs/src/image_loading.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use http::StatusCode;
use log::error;
use log::{error, info};
use reqwest::Client;
use std::io::Write;
use tokio::task;
Expand Down Expand Up @@ -29,21 +29,18 @@ pub type ImageHrefStringResolverFn = Box<dyn Fn(&str, &Options) -> Option<ImageK
pub fn custom_string_resolver() -> ImageHrefStringResolverFn {
let default_string_resolver = ImageHrefResolver::default_string_resolver();
Box::new(move |href: &str, opts: &Options| {
info!("Resolving image: {href}");
if href.starts_with("http://") || href.starts_with("https://") {
// parse as file to extract the extension
let href_path = std::path::Path::new(href);
let extension = href_path
.extension()
.and_then(|ext| ext.to_str().map(|ext| format!(".{}", ext)))
.unwrap_or("".to_string());

// Download image to temporary file with reqwest
let bytes: Option<_> = task::block_in_place(move || {
let (bytes, content_type): (Option<_>, Option<_>) = task::block_in_place(move || {
IMAGE_TOKIO_RUNTIME.block_on(async {
if let Ok(response) = REQWEST_CLIENT.get(href).send().await {
let content_type = response.headers().get("Content-Type")
.and_then(|h| h.to_str().ok().map(|c| c.to_string()));

// Check status code.
match response.status() {
StatusCode::OK => response.bytes().await.ok(),
StatusCode::OK => (response.bytes().await.ok(), content_type),
status => {
let msg = response
.bytes()
Expand All @@ -60,15 +57,35 @@ pub fn custom_string_resolver() -> ImageHrefStringResolverFn {
href, status
);
}
None
(None, None)
}
}
} else {
None
(None, None)
}
})
});

// Compute file extension, which usvg uses to infer the image type
let href_path = std::path::Path::new(href);
let extension = href_path
.extension()
.and_then(|ext| ext.to_str().map(|ext| format!(".{}", ext)))
.unwrap_or_else(|| {
// Fall back to extension based on content type
if let Some(content_type) = &content_type {
match content_type.as_str() {
"image/jpeg" => ".jpg".to_string(),
"image/png" => ".png".to_string(),
"image/gif" => ".gif".to_string(),
"image/svg+xml" => ".svg".to_string(),
_ => String::new(),
}
} else {
String::new()
}
});

if let Some(bytes) = bytes {
// Create the temporary file (maybe with an extension)
let mut builder = tempfile::Builder::new();
Expand Down
1 change: 1 addition & 0 deletions vl-convert-rs/tests/test_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ mod test_png_no_theme {
case("line_with_log_scale", 2.0),
case("remote_images", 1.0),
case("maptile_background", 1.0),
case("maptile_background_2", 1.0),
case("float_font_size", 1.0),
case("no_text_in_font_metrics", 1.0),
case("custom_projection", 1.0),
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2ece4bc

Please sign in to comment.