diff --git a/api_tests/run-federation-test.sh b/api_tests/run-federation-test.sh index 969a95b3e..f9eab5039 100755 --- a/api_tests/run-federation-test.sh +++ b/api_tests/run-federation-test.sh @@ -11,7 +11,7 @@ killall -s1 lemmy_server || true popd pnpm i -pnpm api-test || true +pnpm api-test-image || true killall -s1 lemmy_server || true killall -s1 pict-rs || true diff --git a/crates/routes/src/images/upload.rs b/crates/routes/src/images/upload.rs index c009aae26..735a18d15 100644 --- a/crates/routes/src/images/upload.rs +++ b/crates/routes/src/images/upload.rs @@ -3,7 +3,7 @@ use actix_web::{self, web::*, HttpRequest}; use lemmy_api_common::{ context::LemmyContext, image::UploadImageResponse, - request::{PictrsFile, PictrsResponse}, + request::PictrsResponse, LemmyErrorType, SuccessResponse, }; @@ -18,7 +18,12 @@ use lemmy_db_views::structs::LocalUserView; use lemmy_utils::error::LemmyResult; use reqwest::Body; use std::time::Duration; -use url::Url; +use UploadType::*; + +pub enum UploadType { + Avatar, + Other, +} pub async fn upload_image( req: HttpRequest, @@ -30,45 +35,28 @@ pub async fn upload_image( return Err(LemmyErrorType::ImageUploadDisabled.into()); } - let image = do_upload_image(req, body, UploadType::Other, &local_user_view, &context).await?; - - let image_url = image.image_url(&context.settings().get_protocol_and_hostname())?; - Ok(Json(UploadImageResponse { - image_url, - filename: image.file, - delete_token: image.delete_token, - })) + Ok(Json( + do_upload_image(req, body, Other, &local_user_view, &context).await?, + )) } -pub async fn upload_avatar( +pub async fn upload_user_avatar( req: HttpRequest, body: Payload, local_user_view: LocalUserView, context: Data, ) -> LemmyResult> { - let image = do_upload_image(req, body, UploadType::Avatar, &local_user_view, &context).await?; - + let image = do_upload_image(req, body, Avatar, &local_user_view, &context).await?; delete_old_image(&local_user_view.person.avatar, &context).await?; - let avatar = format!( - "{}/api/v4/image/{}", - context.settings().get_protocol_and_hostname(), - image.file - ); - let avatar = Some(Some(Url::parse(&avatar)?.into())); let person_form = PersonUpdateForm { - avatar, + avatar: Some(Some(image.image_url.into())), ..Default::default() }; - Person::update(&mut context.pool(), local_user_view.person.id, &person_form).await?; Ok(Json(SuccessResponse::default())) } -pub enum UploadType { - Avatar, - Other, -} pub async fn do_upload_image( req: HttpRequest, @@ -76,14 +64,14 @@ pub async fn do_upload_image( upload_type: UploadType, local_user_view: &LocalUserView, context: &Data, -) -> LemmyResult { +) -> LemmyResult { let pictrs_config = context.settings().pictrs()?; let image_url = format!("{}image", pictrs_config.url); let mut client_req = adapt_request(&req, image_url); client_req = match upload_type { - UploadType::Avatar => { + Avatar => { let max_size = context.settings().pictrs()?.max_avatar_size.to_string(); client_req.query(&[ ("resize", max_size.as_ref()), @@ -92,7 +80,7 @@ pub async fn do_upload_image( ]) } // TODO: same as above but using `max_banner_size` - // UploadType::Banner => {} + // Banner => {} _ => client_req, }; if let Some(addr) = req.head().peer_addr { @@ -128,5 +116,10 @@ pub async fn do_upload_image( .pop() .ok_or(LemmyErrorType::InvalidImageUpload)?; - Ok(image) + let url = image.image_url(&context.settings().get_protocol_and_hostname())?; + Ok(UploadImageResponse { + image_url: url, + filename: image.file, + delete_token: image.delete_token, + }) } diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs index b9c38f203..153c4567e 100644 --- a/crates/utils/src/settings/structs.rs +++ b/crates/utils/src/settings/structs.rs @@ -114,6 +114,7 @@ pub struct PictrsConfig { /// TODO: Unfortunately pictrs can only resize images to fit in a*a square, no rectangle. /// Otherwise we have to use crop, or use max_width/max_height which throws error /// if image is larger. + /// TODO: whats a sensible default here? #[default(512)] pub max_banner_size: u32, diff --git a/src/api_routes_v4.rs b/src/api_routes_v4.rs index f8dabd062..90f751b55 100644 --- a/src/api_routes_v4.rs +++ b/src/api_routes_v4.rs @@ -162,7 +162,7 @@ use lemmy_routes::images::{ delete_image, download::{get_image, image_proxy}, pictrs_health, - upload::{upload_avatar, upload_image}, + upload::{upload_image, upload_user_avatar}, }; use lemmy_utils::rate_limit::RateLimitCell; @@ -319,7 +319,7 @@ pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) { .route("/unread_count", get().to(unread_count)) .route("/list_logins", get().to(list_logins)) .route("/validate_auth", get().to(validate_auth)) - .route("/avatar", post().to(upload_avatar)) + .route("/avatar", post().to(upload_user_avatar)) .service( scope("/block") .route("/person", post().to(user_block_person))