From 44ce9a73a2e020d3848576303536b0d19e8a7172 Mon Sep 17 00:00:00 2001 From: silverpill Date: Thu, 5 Jan 2023 17:36:38 +0000 Subject: [PATCH] Save media types of uploaded avatar and banner images --- CHANGELOG.md | 2 ++ docs/openapi.yaml | 8 ++++++++ src/activitypub/actors/helpers.rs | 4 ++-- src/mastodon_api/accounts/types.rs | 15 ++++++++++++--- src/models/profiles/types.rs | 1 + 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71326f5..a52e8fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Implemented activity delivery queue. - Started to keep track of unreachable actors. - Added `configuration` object to response of `/api/v1/instance` endpoint. +- Save media types of uploaded avatar and banner images. ### Changed @@ -22,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Deprecated - Deprecated `post_character_limit` property in `/api/v1/instance` response. +- Avatar and banner uploads without media type via `/api/v1/accounts/update_credentials`. ### Removed diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 55762c8..09e3991 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -152,10 +152,18 @@ paths: description: Avatar image encoded as base64. type: string nullable: true + avatar_media_type: + description: The media type of avatar image. + type: string + nullable: true header: description: Header image encoded as base64. type: string nullable: true + header_media_type: + description: The media type of header image. + type: string + nullable: true fields_attributes: description: The profile fields to be set. type: array diff --git a/src/activitypub/actors/helpers.rs b/src/activitypub/actors/helpers.rs index 092fbfd..7df5fbd 100644 --- a/src/activitypub/actors/helpers.rs +++ b/src/activitypub/actors/helpers.rs @@ -28,7 +28,7 @@ async fn fetch_actor_images( let maybe_avatar = if let Some(icon) = &actor.icon { match fetch_file(instance, &icon.url, media_dir).await { Ok((file_name, _)) => { - let image = ProfileImage { file_name }; + let image = ProfileImage { file_name, media_type: None }; Some(image) }, Err(error) => { @@ -42,7 +42,7 @@ async fn fetch_actor_images( let maybe_banner = if let Some(image) = &actor.image { match fetch_file(instance, &image.url, media_dir).await { Ok((file_name, _)) => { - let image = ProfileImage { file_name }; + let image = ProfileImage { file_name, media_type: None }; Some(image) }, Err(error) => { diff --git a/src/mastodon_api/accounts/types.rs b/src/mastodon_api/accounts/types.rs index c776ad9..b02bd86 100644 --- a/src/mastodon_api/accounts/types.rs +++ b/src/mastodon_api/accounts/types.rs @@ -214,12 +214,15 @@ pub struct AccountUpdateData { display_name: Option, note: Option, avatar: Option, + avatar_media_type: Option, header: Option, + header_media_type: Option, fields_attributes: Option>, } fn process_b64_image_field_value( form_value: Option, + form_media_type: Option, db_value: Option, output_dir: &Path, ) -> Result, UploadError> { @@ -230,13 +233,16 @@ fn process_b64_image_field_value( None } else { // Decode and save file - let (file_name, _) = save_b64_file( + let (file_name, media_type) = save_b64_file( &b64_data, - None, + form_media_type, output_dir, Some("image/"), )?; - let image = ProfileImage { file_name }; + let image = ProfileImage { + file_name, + media_type: Some(media_type), + }; Some(image) } }, @@ -261,11 +267,13 @@ impl AccountUpdateData { }; let avatar = process_b64_image_field_value( self.avatar, + self.avatar_media_type, profile.avatar.clone(), media_dir, )?; let banner = process_b64_image_field_value( self.header, + self.header_media_type, profile.banner.clone(), media_dir, )?; @@ -488,6 +496,7 @@ mod tests { let profile = DbActorProfile { avatar: Some(ProfileImage { file_name: "test".to_string(), + media_type: None, }), ..Default::default() }; diff --git a/src/models/profiles/types.rs b/src/models/profiles/types.rs index 779dc0d..9a7e67b 100644 --- a/src/models/profiles/types.rs +++ b/src/models/profiles/types.rs @@ -33,6 +33,7 @@ use super::validators::{ #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ProfileImage { pub file_name: String, + pub media_type: Option, } json_from_sql!(ProfileImage);