From 3d40cca255d462cc190a602f7fb16c1f4ade821d Mon Sep 17 00:00:00 2001 From: Vika Date: Wed, 13 Nov 2024 04:51:52 +0300 Subject: media endpoint: small code cleanup Change-Id: I10d8ffd53bd3c02d6de3ba4520ab859c18697c61 --- src/media/mod.rs | 11 ++++------- src/media/storage/mod.rs | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) (limited to 'src/media') diff --git a/src/media/mod.rs b/src/media/mod.rs index 85b3b87..32b8405 100644 --- a/src/media/mod.rs +++ b/src/media/mod.rs @@ -1,7 +1,7 @@ use axum::{ extract::{multipart::Multipart, FromRef, Host, Path, State}, response::{IntoResponse, Response} }; -use axum_extra::headers::{HeaderMapExt, HeaderValue, IfNoneMatch}; +use axum_extra::headers::{ContentLength, HeaderMapExt, HeaderValue, IfNoneMatch}; use axum_extra::TypedHeader; use kittybox_util::micropub::{Error as MicropubError, ErrorKind as ErrorType}; use kittybox_indieauth::Scope; @@ -32,7 +32,7 @@ pub(crate) async fn upload( error_description: "Interacting with the media storage requires the \"media\" scope.".to_owned() }.into_response(); } - let host = user.me.host().unwrap().to_string() + &user.me.port().map(|i| format!(":{}", i)).unwrap_or_default(); + let host = user.me.authority(); let field = match upload.next_field().await { Ok(Some(field)) => field, Ok(None) => { @@ -49,7 +49,7 @@ pub(crate) async fn upload( }, }; let metadata: Metadata = (&field).into(); - match blobstore.write_streaming(&host, metadata, field).await { + match blobstore.write_streaming(host, metadata, field).await { Ok(filename) => IntoResponse::into_response(( axum::http::StatusCode::CREATED, [ @@ -103,10 +103,7 @@ pub(crate) async fn serve( ).unwrap() ); if let Some(length) = metadata.length { - headers.insert( - "Content-Length", - HeaderValue::from_str(&length.to_string()).unwrap() - ); + headers.typed_insert(ContentLength(length.get().try_into().unwrap())); } if let Some(etag) = etag { headers.typed_insert(etag); diff --git a/src/media/storage/mod.rs b/src/media/storage/mod.rs index c2a66ec..b6f6ad7 100644 --- a/src/media/storage/mod.rs +++ b/src/media/storage/mod.rs @@ -164,7 +164,7 @@ pub trait MediaStore: 'static + Send + Sync + Clone { .map_ok(|x| x.unwrap()) ); - return Ok(stream); + Ok(stream) } } /// Read metadata for a file. -- cgit 1.4.1