Skip to content

Commit

Permalink
Add a workaround for netvl/xml-rs#155
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
lovasoa committed May 20, 2020
1 parent ad6c3b9 commit 07068f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
18 changes: 1 addition & 17 deletions src/dzi/dzi_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,4 @@ fn test_dzi() {
assert_eq!(dzi.get_size().unwrap(), Vec2d { x: 5393, y: 3852 });
assert_eq!(dzi.get_tile_size(), Vec2d { x: 256, y: 256 });
assert_eq!(dzi.max_level(), 13);
}

#[test]
#[ignore]
fn test_dzi_with_leading_space() {
// See https://github.com/lovasoa/dezoomify-rs/issues/45
// Trying to parse a file with a byte order mark
let dzi: DziFile = serde_xml_rs::from_str(
r#" <?xml version="1.0" encoding="utf-8"?>
<Image TileSize="256" Overlap="0" Format="jpg" xmlns="http://schemas.microsoft.com/deepzoom/2008">
<Size Width="6261" Height="6047" />
</Image>"#,
).unwrap();
assert_eq!(dzi.get_size().unwrap(), Vec2d { x: 6261, y: 6047 });
assert_eq!(dzi.get_tile_size(), Vec2d { x: 256, y: 256 });
assert_eq!(dzi.max_level(), 13);
}
}
23 changes: 22 additions & 1 deletion src/dzi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,17 @@ impl From<DziError> for DezoomerError {
}
}

const BOM: &'static [u8] = &[0xEF, 0xBB, 0xBF]; // UTF8 byte order mark

fn load_from_properties(url: &str, contents: &[u8]) -> Result<ZoomLevels, DziError> {
let image_properties: DziFile = serde_xml_rs::from_reader(contents)?;

// Workaround for https://github.com/netvl/xml-rs/issues/155
// which the original author seems unwilling to fix
let image_properties: DziFile = serde_xml_rs::from_reader(
if contents.starts_with(BOM) {
&contents[BOM.len()..]
} else { contents }
)?;

if image_properties.tile_size == 0 {
return Err(DziError::InvalidTileSize);
Expand Down Expand Up @@ -139,3 +148,15 @@ fn test_panorama() {
]
);
}


#[test]
fn test_dzi_with_bom() {
// See https://github.com/lovasoa/dezoomify-rs/issues/45
// Trying to parse a file with a byte order mark
let contents = "\u{feff}<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Image TileSize=\"256\" Overlap=\"0\" Format=\"jpg\" xmlns=\"http://schemas.microsoft.com/deepzoom/2008\">
<Size Width=\"6261\" Height=\"6047\" />
</Image>";
load_from_properties("http://test.com/test.xml", contents.as_ref()).unwrap();
}

0 comments on commit 07068f7

Please sign in to comment.