mirror of https://github.com/LemmyNet/lemmy.git
proxy images through pictrs
parent
98b5746472
commit
45f5448ece
|
@ -1,30 +0,0 @@
|
||||||
use actix_web::{web, web::Query, HttpResponse};
|
|
||||||
use lemmy_api_common::context::LemmyContext;
|
|
||||||
use lemmy_db_schema::source::images::RemoteImage;
|
|
||||||
use lemmy_utils::error::LemmyResult;
|
|
||||||
use serde::Deserialize;
|
|
||||||
use url::Url;
|
|
||||||
use urlencoding::decode;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct ImageProxyParams {
|
|
||||||
url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn image_proxy(
|
|
||||||
Query(params): Query<ImageProxyParams>,
|
|
||||||
context: web::Data<LemmyContext>,
|
|
||||||
) -> LemmyResult<HttpResponse> {
|
|
||||||
let url = Url::parse(&decode(¶ms.url)?)?;
|
|
||||||
|
|
||||||
// Check that url corresponds to a federated image so that this can't be abused as a proxy
|
|
||||||
// for arbitrary purposes.
|
|
||||||
RemoteImage::validate(&mut context.pool(), url.clone().into()).await?;
|
|
||||||
|
|
||||||
// TODO: Once pictrs 0.5 is out, use it for proxying like `GET /image/original?proxy={url}`. In
|
|
||||||
// case pictrs is unavailable, fallback to this impl.
|
|
||||||
// https://git.asonix.dog/asonix/pict-rs/#api
|
|
||||||
let image_response = context.client().get(url).send().await?;
|
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().streaming(image_response.bytes_stream()))
|
|
||||||
}
|
|
|
@ -6,6 +6,7 @@ use actix_web::{
|
||||||
StatusCode,
|
StatusCode,
|
||||||
},
|
},
|
||||||
web,
|
web,
|
||||||
|
web::Query,
|
||||||
Error,
|
Error,
|
||||||
HttpRequest,
|
HttpRequest,
|
||||||
HttpResponse,
|
HttpResponse,
|
||||||
|
@ -13,15 +14,17 @@ use actix_web::{
|
||||||
use futures::stream::{Stream, StreamExt};
|
use futures::stream::{Stream, StreamExt};
|
||||||
use lemmy_api_common::context::LemmyContext;
|
use lemmy_api_common::context::LemmyContext;
|
||||||
use lemmy_db_schema::source::{
|
use lemmy_db_schema::source::{
|
||||||
images::{LocalImage, LocalImageForm},
|
images::{LocalImage, LocalImageForm, RemoteImage},
|
||||||
local_site::LocalSite,
|
local_site::LocalSite,
|
||||||
};
|
};
|
||||||
use lemmy_db_views::structs::LocalUserView;
|
use lemmy_db_views::structs::LocalUserView;
|
||||||
use lemmy_utils::{rate_limit::RateLimitCell, REQWEST_TIMEOUT};
|
use lemmy_utils::{error::LemmyResult, rate_limit::RateLimitCell, REQWEST_TIMEOUT};
|
||||||
use reqwest::Body;
|
use reqwest::Body;
|
||||||
use reqwest_middleware::{ClientWithMiddleware, RequestBuilder};
|
use reqwest_middleware::{ClientWithMiddleware, RequestBuilder};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use url::Url;
|
||||||
|
use urlencoding::decode;
|
||||||
|
|
||||||
pub fn config(
|
pub fn config(
|
||||||
cfg: &mut web::ServiceConfig,
|
cfg: &mut web::ServiceConfig,
|
||||||
|
@ -37,7 +40,12 @@ pub fn config(
|
||||||
)
|
)
|
||||||
// This has optional query params: /image/{filename}?format=jpg&thumbnail=256
|
// This has optional query params: /image/{filename}?format=jpg&thumbnail=256
|
||||||
.service(web::resource("/pictrs/image/{filename}").route(web::get().to(full_res)))
|
.service(web::resource("/pictrs/image/{filename}").route(web::get().to(full_res)))
|
||||||
.service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete)));
|
.service(web::resource("/pictrs/image/delete/{token}/{filename}").route(web::get().to(delete)))
|
||||||
|
.service(
|
||||||
|
web::scope("/api/v3")
|
||||||
|
.wrap(rate_limit.message())
|
||||||
|
.route("image_proxy", web::post().to(image_proxy)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
@ -228,6 +236,28 @@ async fn delete(
|
||||||
Ok(HttpResponse::build(res.status()).body(BodyStream::new(res.bytes_stream())))
|
Ok(HttpResponse::build(res.status()).body(BodyStream::new(res.bytes_stream())))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct ImageProxyParams {
|
||||||
|
url: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn image_proxy(
|
||||||
|
Query(params): Query<ImageProxyParams>,
|
||||||
|
context: web::Data<LemmyContext>,
|
||||||
|
) -> LemmyResult<HttpResponse> {
|
||||||
|
let url = Url::parse(&decode(¶ms.url)?)?;
|
||||||
|
|
||||||
|
// Check that url corresponds to a federated image so that this can't be abused as a proxy
|
||||||
|
// for arbitrary purposes.
|
||||||
|
RemoteImage::validate(&mut context.pool(), url.clone().into()).await?;
|
||||||
|
|
||||||
|
let pictrs_config = context.settings().pictrs_config()?;
|
||||||
|
let url = format!("{}image/original?proxy={}", pictrs_config.url, ¶ms.url);
|
||||||
|
let image_response = context.client().get(url).send().await?;
|
||||||
|
|
||||||
|
Ok(HttpResponse::Ok().streaming(image_response.bytes_stream()))
|
||||||
|
}
|
||||||
|
|
||||||
fn make_send<S>(mut stream: S) -> impl Stream<Item = S::Item> + Send + Unpin + 'static
|
fn make_send<S>(mut stream: S) -> impl Stream<Item = S::Item> + Send + Unpin + 'static
|
||||||
where
|
where
|
||||||
S: Stream + Unpin + 'static,
|
S: Stream + Unpin + 'static,
|
||||||
|
|
|
@ -3,7 +3,6 @@ use lemmy_db_views::structs::LocalUserView;
|
||||||
use lemmy_utils::error::LemmyError;
|
use lemmy_utils::error::LemmyError;
|
||||||
|
|
||||||
pub mod feeds;
|
pub mod feeds;
|
||||||
pub mod image_proxy;
|
|
||||||
pub mod images;
|
pub mod images;
|
||||||
pub mod nodeinfo;
|
pub mod nodeinfo;
|
||||||
pub mod webfinger;
|
pub mod webfinger;
|
||||||
|
|
|
@ -124,17 +124,11 @@ use lemmy_apub::api::{
|
||||||
search::search,
|
search::search,
|
||||||
user_settings_backup::{export_settings, import_settings},
|
user_settings_backup::{export_settings, import_settings},
|
||||||
};
|
};
|
||||||
use lemmy_routes::image_proxy::image_proxy;
|
|
||||||
use lemmy_utils::rate_limit::RateLimitCell;
|
use lemmy_utils::rate_limit::RateLimitCell;
|
||||||
|
|
||||||
pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
|
pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
|
||||||
cfg.service(
|
cfg.service(
|
||||||
web::scope("/api/v3")
|
web::scope("/api/v3")
|
||||||
.service(
|
|
||||||
web::scope("")
|
|
||||||
.wrap(rate_limit.message())
|
|
||||||
.route("image_proxy", web::post().to(image_proxy)),
|
|
||||||
)
|
|
||||||
// Site
|
// Site
|
||||||
.service(
|
.service(
|
||||||
web::scope("/site")
|
web::scope("/site")
|
||||||
|
|
Loading…
Reference in New Issue