Skip to content

Commit

Permalink
external to local replace
Browse files Browse the repository at this point in the history
  • Loading branch information
tikitko committed Nov 27, 2023
1 parent 7d51dc6 commit 839d092
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
28 changes: 20 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,27 @@ pub const fn parse_u32(s: &str) -> u32 {
out
}

pub const SERVER_ADDRESS: &'static str = env!("SERVER_ADDRESS"); // 127.0.0.1:4000
// 127.0.0.1:4000
pub const SERVER_ADDRESS: &'static str = env!("SERVER_ADDRESS");

// https://site.com/images/
pub const EXTERN_LOCATION_IMAGES_STORAGE_PATH: &'static str =
env!("EXTERN_LOCATION_IMAGES_STORAGE_PATH"); // https://site.com/images/
pub const LOCAL_IMAGES_STORAGE_PATH: &'static str = env!("LOCAL_IMAGES_STORAGE_PATH"); // ./images/
pub const THUMBNAIL_SMALL_WIDTH: u32 = parse_u32(env!("THUMBNAIL_SMALL_WIDTH")); // 250
pub const THUMBNAIL_MEDIUM_WIDTH: u32 = parse_u32(env!("THUMBNAIL_MEDIUM_WIDTH")); // 750
pub const THUMBNAIL_HEIGHT_MULTIPLIER: u32 = parse_u32(env!("THUMBNAIL_HEIGHT_MULTIPLIER")); // 3
pub const IGNORE_INVALID_CERTS_URLS_PREFIXES_LIST: &'static str =
env!("IGNORE_INVALID_CERTS_URLS_PREFIXES_LIST"); // https://site.com,https://site.ru
env!("EXTERN_LOCATION_IMAGES_STORAGE_PATH");

// ./images/
pub const LOCAL_IMAGES_STORAGE_PATH: &'static str = env!("LOCAL_IMAGES_STORAGE_PATH");

// 250
pub const THUMBNAIL_SMALL_WIDTH: u32 = parse_u32(env!("THUMBNAIL_SMALL_WIDTH"));

// 750
pub const THUMBNAIL_MEDIUM_WIDTH: u32 = parse_u32(env!("THUMBNAIL_MEDIUM_WIDTH"));

// 3
pub const THUMBNAIL_HEIGHT_MULTIPLIER: u32 = parse_u32(env!("THUMBNAIL_HEIGHT_MULTIPLIER"));

// https://site.com/:/var/www/site.com/,https://site.ru/:/var/www/site.ru/
pub const EXTERNAL_TO_LOCAL_PATHS_MAP: &'static str = env!("EXTERNAL_TO_LOCAL_PATHS_MAP");

// ------------------------------

Expand Down
34 changes: 22 additions & 12 deletions src/process_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,32 @@ pub fn process_images(rx: mpsc::Receiver<(ImageType, String, flume::Sender<Proce
}

fn download_and_process_image(image_type: &ImageType, base64_url: &String) -> ProcessResult {
let external_to_local_paths_map: HashMap<&str, &str> = EXTERNAL_TO_LOCAL_PATHS_MAP
.split(',')
.filter_map(|pair| {
let parts: Vec<&str> = pair.split(':').collect();
if parts.len() == 2 {
Some((parts[0], parts[1]))
} else {
None
}
})
.collect();

let url_vec = general_purpose::URL_SAFE.decode(base64_url)?;
let url = String::from_utf8(url_vec)?;
let res = {
let urls_prefixes_list: Vec<&str> =
IGNORE_INVALID_CERTS_URLS_PREFIXES_LIST.split(',').collect();
let mut client_builder = reqwest::blocking::Client::builder();
if urls_prefixes_list
let image = if let Some((external_path_component, local_path_component)) =
external_to_local_paths_map
.iter()
.any(|&url_prefix| !url_prefix.is_empty() && url.starts_with(url_prefix))
{
client_builder = client_builder.danger_accept_invalid_certs(true);
}
client_builder.build()?.get(url).send()?
.find(|&(k, &_)| url.contains(k))
{
let local_path = url.replace(external_path_component, local_path_component);
image::io::Reader::open(local_path)?.decode()?
} else {
let res = reqwest::blocking::get(url)?;
let bytes = res.bytes()?;
image::load_from_memory(&bytes)?
};
let bytes = res.bytes()?;
let image = image::load_from_memory(&bytes)?;
let image = image_type.process_image(image);
let path = image_type.local_path(base64_url);
let temp_path = format!("{path}.tmp");
Expand Down

0 comments on commit 839d092

Please sign in to comment.