From 167559bb2724957cde80cc8efc07b4462a4eebe2 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 24 Jun 2024 08:27:06 +0000 Subject: [PATCH] rename FileMeta::file to Option Signed-off-by: Jason Volk --- src/api/client/media.rs | 9 +++++--- src/service/media/mod.rs | 45 ++++++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/api/client/media.rs b/src/api/client/media.rs index 9d3fbabe..39640b23 100644 --- a/src/api/client/media.rs +++ b/src/api/client/media.rs @@ -200,12 +200,13 @@ pub(crate) async fn get_content_route( let mxc = format!("mxc://{}/{}", body.server_name, body.media_id); if let Some(FileMeta { + content, content_type, - file, content_disposition, }) = services().media.get(&mxc).await? { let content_disposition = Some(make_content_disposition(&content_type, content_disposition, None)); + let file = content.expect("content"); Ok(get_content::v3::Response { file, @@ -282,8 +283,8 @@ pub(crate) async fn get_content_as_filename_route( let mxc = format!("mxc://{}/{}", body.server_name, body.media_id); if let Some(FileMeta { + content, content_type, - file, content_disposition, }) = services().media.get(&mxc).await? { @@ -293,6 +294,7 @@ pub(crate) async fn get_content_as_filename_route( Some(body.filename.clone()), )); + let file = content.expect("content"); Ok(get_content_as_filename::v3::Response { file, content_type, @@ -371,8 +373,8 @@ pub(crate) async fn get_content_thumbnail_route( let mxc = format!("mxc://{}/{}", body.server_name, body.media_id); if let Some(FileMeta { + content, content_type, - file, content_disposition, }) = services() .media @@ -388,6 +390,7 @@ pub(crate) async fn get_content_thumbnail_route( .await? { let content_disposition = Some(make_content_disposition(&content_type, content_disposition, None)); + let file = content.expect("content"); Ok(get_content_thumbnail::v3::Response { file, diff --git a/src/service/media/mod.rs b/src/service/media/mod.rs index 9b1cbe06..2f687925 100644 --- a/src/service/media/mod.rs +++ b/src/service/media/mod.rs @@ -20,10 +20,9 @@ use crate::services; #[derive(Debug)] pub struct FileMeta { - #[allow(dead_code)] - pub content_disposition: Option, + pub content: Option>, pub content_type: Option, - pub file: Vec, + pub content_disposition: Option, } #[derive(Serialize, Default)] @@ -132,16 +131,16 @@ impl Service { /// Downloads a file. pub async fn get(&self, mxc: &str) -> Result> { if let Ok((content_disposition, content_type, key)) = self.db.search_file_metadata(mxc, 0, 0) { - let mut file = Vec::new(); + let mut content = Vec::new(); let path = self.get_media_file(&key); BufReader::new(fs::File::open(path).await?) - .read_to_end(&mut file) + .read_to_end(&mut content) .await?; Ok(Some(FileMeta { - content_disposition, + content: Some(content), content_type, - file, + content_disposition, })) } else { Ok(None) @@ -283,29 +282,35 @@ impl Service { if let Ok((content_disposition, content_type, key)) = self.db.search_file_metadata(mxc, width, height) { // Using saved thumbnail - let mut file = Vec::new(); + let mut content = Vec::new(); let path = self.get_media_file(&key); - fs::File::open(path).await?.read_to_end(&mut file).await?; + fs::File::open(path) + .await? + .read_to_end(&mut content) + .await?; Ok(Some(FileMeta { - content_disposition, + content: Some(content), content_type, - file: file.clone(), + content_disposition, })) } else if let Ok((content_disposition, content_type, key)) = self.db.search_file_metadata(mxc, 0, 0) { // Generate a thumbnail - let mut file = Vec::new(); + let mut content = Vec::new(); let path = self.get_media_file(&key); - fs::File::open(path).await?.read_to_end(&mut file).await?; + fs::File::open(path) + .await? + .read_to_end(&mut content) + .await?; - if let Ok(image) = image::load_from_memory(&file) { + if let Ok(image) = image::load_from_memory(&content) { let original_width = image.width(); let original_height = image.height(); if width > original_width || height > original_height { return Ok(Some(FileMeta { - content_disposition, + content: Some(content), content_type, - file: file.clone(), + content_disposition, })); } @@ -350,16 +355,16 @@ impl Service { f.write_all(&thumbnail_bytes).await?; Ok(Some(FileMeta { - content_disposition, + content: Some(thumbnail_bytes), content_type, - file: thumbnail_bytes.clone(), + content_disposition, })) } else { // Couldn't parse file to generate thumbnail, send original Ok(Some(FileMeta { - content_disposition, + content: Some(content), content_type, - file: file.clone(), + content_disposition, })) } } else {