proxy images through pictrs

cleanup-request-rs
Felix Ableitner 2023-10-26 12:39:00 +02:00
parent 98b5746472
commit 45f5448ece
4 changed files with 33 additions and 40 deletions

View File

@ -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(&params.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()))
}

View File

@ -6,6 +6,7 @@ use actix_web::{
StatusCode,
},
web,
web::Query,
Error,
HttpRequest,
HttpResponse,
@ -13,15 +14,17 @@ use actix_web::{
use futures::stream::{Stream, StreamExt};
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::source::{
images::{LocalImage, LocalImageForm},
images::{LocalImage, LocalImageForm, RemoteImage},
local_site::LocalSite,
};
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_middleware::{ClientWithMiddleware, RequestBuilder};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use url::Url;
use urlencoding::decode;
pub fn config(
cfg: &mut web::ServiceConfig,
@ -37,7 +40,12 @@ pub fn config(
)
// 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/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)]
@ -228,6 +236,28 @@ async fn delete(
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(&params.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, &params.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
where
S: Stream + Unpin + 'static,

View File

@ -3,7 +3,6 @@ use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError;
pub mod feeds;
pub mod image_proxy;
pub mod images;
pub mod nodeinfo;
pub mod webfinger;

View File

@ -124,17 +124,11 @@ use lemmy_apub::api::{
search::search,
user_settings_backup::{export_settings, import_settings},
};
use lemmy_routes::image_proxy::image_proxy;
use lemmy_utils::rate_limit::RateLimitCell;
pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
cfg.service(
web::scope("/api/v3")
.service(
web::scope("")
.wrap(rate_limit.message())
.route("image_proxy", web::post().to(image_proxy)),
)
// Site
.service(
web::scope("/site")