This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/max_bytes
- Loading branch information
Showing
9 changed files
with
101 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,43 @@ | ||
use std::time::Duration; | ||
|
||
use actix_web::web::Bytes; | ||
use actix_web::{web::Query, HttpResponse, Responder}; | ||
use serde::Deserialize; | ||
|
||
use crate::util::request::{fetch, get_bytes}; | ||
use crate::util::result::Error; | ||
|
||
lazy_static! { | ||
static ref CACHE: moka::future::Cache<String, Result<Bytes, Error>> = | ||
moka::future::Cache::builder() | ||
.weigher(|_key, value: &Result<Bytes, Error>| { | ||
value.as_ref().map(|bytes| bytes.len() as u32).unwrap_or(1) | ||
}) | ||
.max_capacity(1024 * 1024 * 1024) | ||
.time_to_live(Duration::from_secs(60)) | ||
.build(); | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct Parameters { | ||
url: String, | ||
} | ||
|
||
pub async fn get(info: Query<Parameters>) -> Result<impl Responder, Error> { | ||
let url = info.into_inner().url; | ||
async fn proxy(url: String) -> Result<Bytes, Error> { | ||
let (mut resp, mime) = fetch(&url).await?; | ||
|
||
if matches!(mime.type_(), mime::IMAGE | mime::VIDEO) { | ||
let bytes = get_bytes(&mut resp).await?; | ||
Ok(HttpResponse::Ok().body(bytes)) | ||
Ok(bytes) | ||
} else { | ||
Err(Error::NotAllowedToProxy) | ||
} | ||
} | ||
|
||
pub async fn get(Query(info): Query<Parameters>) -> Result<impl Responder, Error> { | ||
let url = info.url; | ||
let result = CACHE | ||
.get_with(url.clone(), async { proxy(url).await }) | ||
.await; | ||
result.map(|b| HttpResponse::Ok().body(b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters