From 316b7510bf782c9c5a8eb9ea2e282b3bff81ca9c Mon Sep 17 00:00:00 2001 From: "617914118@qq.com" <617914118@qq.com> Date: Mon, 29 Jan 2024 17:25:42 +0800 Subject: [PATCH] Add zoom-format --- src/arguments.rs | 4 ++++ src/lib.rs | 1 + src/network.rs | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/arguments.rs b/src/arguments.rs index 5ff8327..65d2a5c 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -116,6 +116,9 @@ pub struct Arguments { /// retrying partially failed downloads, or stitching the tiles with an external program. #[arg(short = 'c', long = "tile-cache")] pub tile_storage_folder: Option, + /// A zoom_level image format suffix eg.default.png or default.jpg + #[arg(short = 'f', long = "zoom-format", default_value = "auto")] + pub zoom_format: String, } impl Default for Arguments { @@ -140,6 +143,7 @@ impl Default for Arguments { connect_timeout: Duration::from_secs(6), logging: "warn".to_string(), tile_storage_folder: None, + zoom_format: "".to_string(), } } } diff --git a/src/lib.rs b/src/lib.rs index d42500f..f19aff0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,6 +172,7 @@ pub async fn dezoomify_level( retries: args.retries, retry_delay: args.retry_delay, tile_storage_folder: args.tile_storage_folder.clone(), + zoom_format: args.zoom_format.clone(), }; let mut throttler = throttler::Throttler::new(args.min_interval); info!("Creating canvas"); diff --git a/src/network.rs b/src/network.rs index 52119fa..3f48233 100644 --- a/src/network.rs +++ b/src/network.rs @@ -54,13 +54,27 @@ pub struct TileDownloader { pub retries: usize, pub retry_delay: Duration, pub tile_storage_folder: Option, + pub zoom_format: String, } impl TileDownloader { pub async fn download_tile( &self, - tile_reference: TileReference, + mut tile_reference: TileReference, ) -> Result { + if !"auto".eq(&self.zoom_format) { + if let Ok(regx) = regex::Regex::new(r"default.*") { + let url = regx + .replace( + &tile_reference.url, + &format!("default.{}", self.zoom_format), + ) + .to_string(); + + tile_reference.url = url + }; + } + // The initial delay after which a failed request is retried depends on the position of the tile // in order to avoid sending repeated "bursts" of requests to a server that is struggling let n = 100;