Split image endpoints into API v3 and v4

This commit is contained in:
Felix Ableitner 2024-12-12 15:59:07 +01:00
parent 8a5daeec97
commit 56ab67f804
3 changed files with 286 additions and 289 deletions

View file

@ -18,27 +18,13 @@ use lemmy_db_schema::source::{
local_site::LocalSite, local_site::LocalSite,
}; };
use lemmy_db_views::structs::LocalUserView; use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{error::LemmyResult, rate_limit::RateLimitCell, REQWEST_TIMEOUT}; use lemmy_utils::{error::LemmyResult, REQWEST_TIMEOUT};
use reqwest::Body; use reqwest::Body;
use reqwest_middleware::{ClientWithMiddleware, RequestBuilder}; use reqwest_middleware::RequestBuilder;
use serde::Deserialize; use serde::Deserialize;
use std::time::Duration; use std::time::Duration;
use url::Url; use url::Url;
pub fn config(cfg: &mut ServiceConfig, client: ClientWithMiddleware, rate_limit: &RateLimitCell) {
cfg
.app_data(Data::new(client))
.service(
resource("/pictrs/image")
.wrap(rate_limit.image())
.route(post().to(upload)),
)
// This has optional query params: /image/{filename}?format=jpg&thumbnail=256
.service(resource("/pictrs/image/{filename}").route(get().to(full_res)))
.service(resource("/pictrs/image/delete/{token}/{filename}").route(get().to(delete)))
.service(resource("/pictrs/healthz").route(get().to(healthz)));
}
trait ProcessUrl { trait ProcessUrl {
/// If thumbnail or format is given, this uses the pictrs process endpoint. /// If thumbnail or format is given, this uses the pictrs process endpoint.
/// Otherwise, it uses the normal pictrs url (IE image/original). /// Otherwise, it uses the normal pictrs url (IE image/original).
@ -46,7 +32,7 @@ trait ProcessUrl {
} }
#[derive(Deserialize, Clone)] #[derive(Deserialize, Clone)]
struct PictrsGetParams { pub struct PictrsGetParams {
format: Option<String>, format: Option<String>,
thumbnail: Option<i32>, thumbnail: Option<i32>,
} }
@ -99,15 +85,12 @@ impl ProcessUrl for ImageProxyParams {
} }
} }
} }
fn adapt_request( fn adapt_request(request: &HttpRequest, context: &LemmyContext, url: String) -> RequestBuilder {
request: &HttpRequest,
client: &ClientWithMiddleware,
url: String,
) -> RequestBuilder {
// remove accept-encoding header so that pictrs doesn't compress the response // remove accept-encoding header so that pictrs doesn't compress the response
const INVALID_HEADERS: &[HeaderName] = &[ACCEPT_ENCODING, HOST]; const INVALID_HEADERS: &[HeaderName] = &[ACCEPT_ENCODING, HOST];
let client_request = client let client_request = context
.client()
.request(convert_method(request.method()), url) .request(convert_method(request.method()), url)
.timeout(REQWEST_TIMEOUT); .timeout(REQWEST_TIMEOUT);
@ -124,19 +107,17 @@ fn adapt_request(
}) })
} }
async fn upload( pub async fn upload_image(
req: HttpRequest, req: HttpRequest,
body: Payload, body: Payload,
// require login // require login
local_user_view: LocalUserView, local_user_view: LocalUserView,
client: Data<ClientWithMiddleware>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
) -> LemmyResult<HttpResponse> { ) -> LemmyResult<HttpResponse> {
// TODO: check rate limit here
let pictrs_config = context.settings().pictrs_config()?; let pictrs_config = context.settings().pictrs_config()?;
let image_url = format!("{}image", pictrs_config.url); let image_url = format!("{}image", pictrs_config.url);
let mut client_req = adapt_request(&req, &client, image_url); let mut client_req = adapt_request(&req, &context, image_url);
if let Some(addr) = req.head().peer_addr { if let Some(addr) = req.head().peer_addr {
client_req = client_req.header("X-Forwarded-For", addr.to_string()) client_req = client_req.header("X-Forwarded-For", addr.to_string())
@ -169,11 +150,10 @@ async fn upload(
Ok(HttpResponse::build(convert_status(status)).json(images)) Ok(HttpResponse::build(convert_status(status)).json(images))
} }
async fn full_res( pub async fn get_full_res_image(
filename: Path<String>, filename: Path<String>,
Query(params): Query<PictrsGetParams>, Query(params): Query<PictrsGetParams>,
req: HttpRequest, req: HttpRequest,
client: Data<ClientWithMiddleware>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>, local_user_view: Option<LocalUserView>,
) -> LemmyResult<HttpResponse> { ) -> LemmyResult<HttpResponse> {
@ -189,15 +169,11 @@ async fn full_res(
let processed_url = params.process_url(name, &pictrs_config.url); let processed_url = params.process_url(name, &pictrs_config.url);
image(processed_url, req, &client).await image(processed_url, req, &context).await
} }
async fn image( async fn image(url: String, req: HttpRequest, context: &LemmyContext) -> LemmyResult<HttpResponse> {
url: String, let mut client_req = adapt_request(&req, context, url);
req: HttpRequest,
client: &ClientWithMiddleware,
) -> LemmyResult<HttpResponse> {
let mut client_req = adapt_request(&req, client, url);
if let Some(addr) = req.head().peer_addr { if let Some(addr) = req.head().peer_addr {
client_req = client_req.header("X-Forwarded-For", addr.to_string()); client_req = client_req.header("X-Forwarded-For", addr.to_string());
@ -222,10 +198,9 @@ async fn image(
Ok(client_res.body(BodyStream::new(res.bytes_stream()))) Ok(client_res.body(BodyStream::new(res.bytes_stream())))
} }
async fn delete( pub async fn delete_image(
components: Path<(String, String)>, components: Path<(String, String)>,
req: HttpRequest, req: HttpRequest,
client: Data<ClientWithMiddleware>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
// require login // require login
_local_user_view: LocalUserView, _local_user_view: LocalUserView,
@ -235,7 +210,7 @@ async fn delete(
let pictrs_config = context.settings().pictrs_config()?; let pictrs_config = context.settings().pictrs_config()?;
let url = format!("{}image/delete/{}/{}", pictrs_config.url, &token, &file); let url = format!("{}image/delete/{}/{}", pictrs_config.url, &token, &file);
let mut client_req = adapt_request(&req, &client, url); let mut client_req = adapt_request(&req, &context, url);
if let Some(addr) = req.head().peer_addr { if let Some(addr) = req.head().peer_addr {
client_req = client_req.header("X-Forwarded-For", addr.to_string()); client_req = client_req.header("X-Forwarded-For", addr.to_string());
@ -248,15 +223,11 @@ async fn delete(
Ok(HttpResponse::build(convert_status(res.status())).body(BodyStream::new(res.bytes_stream()))) Ok(HttpResponse::build(convert_status(res.status())).body(BodyStream::new(res.bytes_stream())))
} }
async fn healthz( pub async fn pictrs_healthz(req: HttpRequest, context: Data<LemmyContext>) -> LemmyResult<HttpResponse> {
req: HttpRequest,
client: Data<ClientWithMiddleware>,
context: Data<LemmyContext>,
) -> LemmyResult<HttpResponse> {
let pictrs_config = context.settings().pictrs_config()?; let pictrs_config = context.settings().pictrs_config()?;
let url = format!("{}healthz", pictrs_config.url); let url = format!("{}healthz", pictrs_config.url);
let mut client_req = adapt_request(&req, &client, url); let mut client_req = adapt_request(&req, &context, url);
if let Some(addr) = req.head().peer_addr { if let Some(addr) = req.head().peer_addr {
client_req = client_req.header("X-Forwarded-For", addr.to_string()); client_req = client_req.header("X-Forwarded-For", addr.to_string());
@ -270,7 +241,6 @@ async fn healthz(
pub async fn image_proxy( pub async fn image_proxy(
Query(params): Query<ImageProxyParams>, Query(params): Query<ImageProxyParams>,
req: HttpRequest, req: HttpRequest,
client: Data<ClientWithMiddleware>,
context: Data<LemmyContext>, context: Data<LemmyContext>,
) -> LemmyResult<Either<HttpResponse<()>, HttpResponse<BoxBody>>> { ) -> LemmyResult<Either<HttpResponse<()>, HttpResponse<BoxBody>>> {
let url = Url::parse(&params.url)?; let url = Url::parse(&params.url)?;
@ -291,7 +261,7 @@ pub async fn image_proxy(
Ok(Either::Left(Redirect::to(url.to_string()).respond_to(&req))) Ok(Either::Left(Redirect::to(url.to_string()).respond_to(&req)))
} else { } else {
// Proxy the image data through Lemmy // Proxy the image data through Lemmy
Ok(Either::Right(image(processed_url, req, &client).await?)) Ok(Either::Right(image(processed_url, req, &context).await?))
} }
} }

View file

@ -134,14 +134,23 @@ 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::images::image_proxy; use lemmy_routes::images::{delete_image, get_full_res_image, image_proxy, pictrs_healthz, upload_image};
use lemmy_utils::rate_limit::RateLimitCell; use lemmy_utils::rate_limit::RateLimitCell;
// Deprecated, use api v4 instead. // Deprecated, use api v4 instead.
// When removing api v3, we also need to rewrite all links in database with // When removing api v3, we also need to rewrite all links in database with
// `/api/v3/image_proxy` to use `/api/v4/image_proxy` instead. // `/api/v3/image_proxy` to use `/api/v4/image_proxy` instead.
pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) { pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) {
cfg.service( cfg
.service(
resource("/pictrs/image")
.wrap(rate_limit.image())
.route(post().to(upload_image)),
)
.service(resource("/pictrs/image/{filename}").route(get().to(get_full_res_image)))
.service(resource("/pictrs/image/delete/{token}/{filename}").route(get().to(delete_image)))
.service(resource("/pictrs/healthz").route(get().to(pictrs_healthz)))
.service(
scope("/api/v3") scope("/api/v3")
.route("/image_proxy", get().to(image_proxy)) .route("/image_proxy", get().to(image_proxy))
// Site // Site
@ -304,7 +313,8 @@ pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) {
.wrap(rate_limit.import_user_settings()) .wrap(rate_limit.import_user_settings())
.route(post().to(import_settings)), .route(post().to(import_settings)),
) )
// TODO, all the current account related actions under /user need to get moved here eventually // TODO, all the current account related actions under /user need to get moved here
// eventually
.service( .service(
scope("/account") scope("/account")
.wrap(rate_limit.message()) .wrap(rate_limit.message())

View file

@ -159,7 +159,13 @@ 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::images::image_proxy; use lemmy_routes::images::{
delete_image,
get_full_res_image,
image_proxy,
pictrs_healthz,
upload_image,
};
use lemmy_utils::rate_limit::RateLimitCell; use lemmy_utils::rate_limit::RateLimitCell;
pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) { pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) {
@ -387,6 +393,17 @@ pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) {
.wrap(rate_limit.register()) .wrap(rate_limit.register())
.route("/authenticate", post().to(authenticate_with_oauth)), .route("/authenticate", post().to(authenticate_with_oauth)),
) )
.route("/sitemap.xml", get().to(get_sitemap)), .route("/sitemap.xml", get().to(get_sitemap))
.service(
scope("/image")
.service(
resource("")
.wrap(rate_limit.image())
.route(post().to(upload_image)),
)
.route("/{filename}", get().to(get_full_res_image))
.route("{token}/{filename}", delete().to(delete_image))
.route("/healthz", get().to(pictrs_healthz)),
),
); );
} }