Skip to content

Commit

Permalink
Fix false positive IIIF detection
Browse files Browse the repository at this point in the history
Fixes #86
  • Loading branch information
lovasoa committed Nov 19, 2020
1 parent 76aecef commit 5a7e821
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
28 changes: 20 additions & 8 deletions src/iiif/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use custom_error::custom_error;
use log::info;
use log::{info, debug};
use serde::export::Formatter;

use tile_info::ImageInfo;
Expand All @@ -10,7 +10,6 @@ use crate::dezoomer::*;
use crate::iiif::tile_info::TileSizeFormat;
use crate::json_utils::all_json;
use crate::max_size_in_rect;
use regex::bytes::Regex;

pub mod tile_info;

Expand Down Expand Up @@ -49,12 +48,11 @@ fn zoom_levels(url: &str, raw_info: &[u8]) -> Result<ZoomLevels, IIIFError> {
// Due to the very fault-tolerant way we parse iiif manifests, a single javascript
// object with a 'width' and a 'height' field is enough to be detected as an IIIF level
// See https://github.com/lovasoa/dezoomify-rs/issues/80
let openseadragon_detected =
Regex::new(r"[oO]pen[sS]eadragon|tileSources").unwrap().is_match(raw_info);
let levels: Vec<ZoomLevel> = all_json::<ImageInfo>(raw_info)
.filter(|info| {
let keep = openseadragon_detected || !info.has_distinctive_iiif_properties();
if !keep { info!("dropping level {:?}", info); }
let keep = info.has_distinctive_iiif_properties();
if keep { debug!("keeping image info {:?} because it has distinctive IIIF properties", info) }
else { info!("dropping level {:?}", info) }
keep
})
.flat_map(|info| zoom_levels_from_info(url, info).into_iter().flatten())
Expand All @@ -63,8 +61,8 @@ fn zoom_levels(url: &str, raw_info: &[u8]) -> Result<ZoomLevels, IIIFError> {
Err(e.into())
} else {
info!("No normal info.json parsing failed ({}), \
but {} inline json5 zoom level(s) were found. \
OpenSeadragon detected: {}", e, levels.len(), openseadragon_detected);
but {} inline json5 zoom level(s) were found.",
e, levels.len());
Ok(levels)
}
}
Expand Down Expand Up @@ -226,6 +224,20 @@ fn test_missing_id() {
)
}

#[test]
fn test_false_positive() {
let data = br#"
var mainImage={
type: "zoomifytileservice",
width: 62596,
height: 38467,
tilesUrl: "./ORIONFINAL/"
};
"#;
let res = zoom_levels("https://orion2020v5b.spaceforeverybody.com/", data);
assert!(res.is_err(), "openseadragon zoomify image should not be misdetected");
}

#[test]
fn test_qualities() {
let data = br#"{
Expand Down
8 changes: 5 additions & 3 deletions src/iiif/tile_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ impl ImageInfo {
/// that this is not in fact a valid IIIF image
pub fn has_distinctive_iiif_properties(&self) -> bool {
self.id.is_some() || self.protocol.is_some() || self.context.is_some() ||
self.tiles.is_some() || self.iiif_type.is_some() || self.formats.is_some() ||
self.iiif_type.as_ref().filter(|&s| s == "iiif:ImageProfile").is_some()
self.tiles.is_some() || self.formats.is_some() ||
self.iiif_type.as_ref().filter(
|&s| s == "iiif:ImageProfile" || s == "ImageService3"
).is_some()
}

/// Some info.json files contain a an invalid value for "@id",
Expand Down Expand Up @@ -288,4 +290,4 @@ fn test_best_quality() {
let info = ImageInfo { qualities, ..ImageInfo::default() };
assert_eq!(info.best_quality(), expected_best_quality);
}
}
}

0 comments on commit 5a7e821

Please sign in to comment.