fix (backend-rs): use proxy and proxyBypassHosts config
This commit is contained in:
parent
320f933e9d
commit
13b648f6bf
3 changed files with 29 additions and 15 deletions
|
@ -1,9 +1,8 @@
|
|||
use crate::misc::redis_cache::{get_cache, set_cache, CacheError};
|
||||
use crate::util::http_client;
|
||||
use image::{io::Reader, ImageError, ImageFormat};
|
||||
use nom_exif::{parse_jpeg_exif, EntryValue, ExifTag};
|
||||
use once_cell::sync::OnceCell;
|
||||
use std::io::Cursor;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
|
@ -35,18 +34,6 @@ const BROWSER_SAFE_IMAGE_TYPES: [ImageFormat; 8] = [
|
|||
ImageFormat::Avif,
|
||||
];
|
||||
|
||||
static CLIENT: OnceCell<reqwest::Client> = OnceCell::new();
|
||||
|
||||
fn client() -> Result<reqwest::Client, reqwest::Error> {
|
||||
CLIENT
|
||||
.get_or_try_init(|| {
|
||||
reqwest::Client::builder()
|
||||
.timeout(Duration::from_secs(5))
|
||||
.build()
|
||||
})
|
||||
.cloned()
|
||||
}
|
||||
|
||||
static MTX_GUARD: Mutex<()> = Mutex::const_new(());
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
|
@ -78,7 +65,7 @@ pub async fn get_image_size_from_url(url: &str) -> Result<ImageSize, Error> {
|
|||
|
||||
tracing::info!("retrieving image size from {}", url);
|
||||
|
||||
let image_bytes = client()?.get(url).send().await?.bytes().await?;
|
||||
let image_bytes = http_client()?.get(url).send().await?.bytes().await?;
|
||||
let reader = Reader::new(Cursor::new(&image_bytes)).with_guessed_format()?;
|
||||
|
||||
let format = reader.format();
|
||||
|
|
24
packages/backend-rs/src/util/http_client.rs
Normal file
24
packages/backend-rs/src/util/http_client.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use crate::config::CONFIG;
|
||||
use once_cell::sync::OnceCell;
|
||||
use reqwest::{Client, Error, NoProxy, Proxy};
|
||||
use std::time::Duration;
|
||||
|
||||
static CLIENT: OnceCell<Client> = OnceCell::new();
|
||||
|
||||
pub fn http_client() -> Result<Client, Error> {
|
||||
CLIENT
|
||||
.get_or_try_init(|| {
|
||||
let mut builder = Client::builder().timeout(Duration::from_secs(5));
|
||||
|
||||
if let Some(proxy_url) = &CONFIG.proxy {
|
||||
let mut proxy = Proxy::all(proxy_url)?;
|
||||
if let Some(proxy_bypass_hosts) = &CONFIG.proxy_bypass_hosts {
|
||||
proxy = proxy.no_proxy(NoProxy::from_string(&proxy_bypass_hosts.join(",")));
|
||||
}
|
||||
builder = builder.proxy(proxy);
|
||||
}
|
||||
|
||||
builder.build()
|
||||
})
|
||||
.cloned()
|
||||
}
|
|
@ -1,2 +1,5 @@
|
|||
pub use http_client::http_client;
|
||||
|
||||
pub mod http_client;
|
||||
pub mod id;
|
||||
pub mod random;
|
||||
|
|
Loading…
Reference in a new issue