From c4ad98126a4374dba1f80f30d9309dff1c17c1f1 Mon Sep 17 00:00:00 2001 From: silverpill Date: Wed, 21 Dec 2022 16:05:26 +0000 Subject: [PATCH] Use media type hint when processing uploaded media attachment --- docs/openapi.yaml | 26 ++++++++++++++++++++++++++ src/mastodon_api/media/types.rs | 6 +++--- src/mastodon_api/media/views.rs | 2 ++ src/mastodon_api/uploads.rs | 3 ++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 656ff01..d08e424 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -639,6 +639,32 @@ paths: application/json: schema: $ref: '#/components/schemas/Instance' + /api/v1/media: + post: + summary: Create an attachment to be used with a new post. + security: + - tokenAuth: [] + requestBody: + content: + application/json: + schema: + type: object + properties: + file: + description: File encoded as base64. + type: string + media_type: + description: Media type. + type: string + nullable: true + example: image/jpeg + responses: + 200: + description: Successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/Attachment' /api/v1/notifications: get: summary: Notifications concerning the user. diff --git a/src/mastodon_api/media/types.rs b/src/mastodon_api/media/types.rs index 6fc5744..eec236e 100644 --- a/src/mastodon_api/media/types.rs +++ b/src/mastodon_api/media/types.rs @@ -2,16 +2,16 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::models::attachments::types::{ - DbMediaAttachment, AttachmentType, + DbMediaAttachment, }; use crate::utils::files::get_file_url; -/// https://docs.joinmastodon.org/methods/statuses/media/ #[derive(Deserialize)] pub struct AttachmentCreateData { - // base64-encoded file + // base64-encoded file (not comtaible with Mastodon) pub file: String, + pub media_type: Option, } /// https://docs.joinmastodon.org/entities/attachment/ diff --git a/src/mastodon_api/media/views.rs b/src/mastodon_api/media/views.rs index 5bccb34..cd9098c 100644 --- a/src/mastodon_api/media/views.rs +++ b/src/mastodon_api/media/views.rs @@ -1,3 +1,4 @@ +/// https://docs.joinmastodon.org/methods/media/#v1 use actix_web::{post, web, HttpResponse, Scope}; use actix_web_httpauth::extractors::bearer::BearerAuth; @@ -20,6 +21,7 @@ async fn create_attachment_view( let current_user = get_current_user(db_client, auth.token()).await?; let (file_name, media_type) = save_b64_file( &attachment_data.file, + attachment_data.media_type.clone(), &config.media_dir(), )?; let db_attachment = create_attachment( diff --git a/src/mastodon_api/uploads.rs b/src/mastodon_api/uploads.rs index de98fb6..ffa2b47 100644 --- a/src/mastodon_api/uploads.rs +++ b/src/mastodon_api/uploads.rs @@ -33,13 +33,14 @@ impl From for HttpError { pub fn save_b64_file( b64data: &str, + maybe_media_type: Option, output_dir: &Path, ) -> Result<(String, Option), UploadError> { let data = base64::decode(b64data)?; if data.len() > UPLOAD_MAX_SIZE { return Err(UploadError::TooLarge); }; - Ok(save_file(data, output_dir, None)?) + Ok(save_file(data, output_dir, maybe_media_type)?) } pub fn save_validated_b64_file(