Use media type hint when processing uploaded media attachment

This commit is contained in:
silverpill 2022-12-21 16:05:26 +00:00
parent 81c0f5e2bd
commit c4ad98126a
4 changed files with 33 additions and 4 deletions

View file

@ -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.

View file

@ -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<String>,
}
/// https://docs.joinmastodon.org/entities/attachment/

View file

@ -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(

View file

@ -33,13 +33,14 @@ impl From<UploadError> for HttpError {
pub fn save_b64_file(
b64data: &str,
maybe_media_type: Option<String>,
output_dir: &Path,
) -> Result<(String, Option<String>), 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(