Factor content out of FileMeta

That's not what *meta*data means
This commit is contained in:
Lambda 2024-08-11 16:59:02 +00:00
parent a06c8db996
commit 84850a163d
3 changed files with 68 additions and 47 deletions

View file

@ -430,11 +430,13 @@ async fn get_content_route_ruma(
) -> Result<authenticated_media_client::get_content::v1::Response> { ) -> Result<authenticated_media_client::get_content::v1::Response> {
let mxc = MxcData::new(&body.server_name, &body.media_id)?; let mxc = MxcData::new(&body.server_name, &body.media_id)?;
if let Some(FileMeta { if let Some((
content_type, FileMeta {
content_type,
..
},
file, file,
.. )) = services().media.get(mxc.to_string()).await?
}) = services().media.get(mxc.to_string()).await?
{ {
Ok(authenticated_media_client::get_content::v1::Response { Ok(authenticated_media_client::get_content::v1::Response {
file, file,
@ -554,11 +556,13 @@ pub(crate) async fn get_content_as_filename_route_ruma(
) -> Result<authenticated_media_client::get_content_as_filename::v1::Response> { ) -> Result<authenticated_media_client::get_content_as_filename::v1::Response> {
let mxc = MxcData::new(&body.server_name, &body.media_id)?; let mxc = MxcData::new(&body.server_name, &body.media_id)?;
if let Some(FileMeta { if let Some((
content_type, FileMeta {
content_type,
..
},
file, file,
.. )) = services().media.get(mxc.to_string()).await?
}) = services().media.get(mxc.to_string()).await?
{ {
Ok(authenticated_media_client::get_content_as_filename::v1::Response { Ok(authenticated_media_client::get_content_as_filename::v1::Response {
file, file,
@ -811,11 +815,13 @@ async fn get_content_thumbnail_route_ruma(
} }
}; };
if let Some(FileMeta { if let Some((
content_type, FileMeta {
content_type,
..
},
file, file,
.. )) =
}) =
services().media.get_thumbnail(mxc.to_string(), width, height).await? services().media.get_thumbnail(mxc.to_string(), width, height).await?
{ {
return Ok(make_response(file, content_type)); return Ok(make_response(file, content_type));
@ -866,11 +872,13 @@ async fn get_content_thumbnail_route_ruma(
get_remote_content(&mxc).await?; get_remote_content(&mxc).await?;
if let Some(FileMeta { if let Some((
content_type, FileMeta {
content_type,
..
},
file, file,
.. )) = services()
}) = services()
.media .media
.get_thumbnail(mxc.to_string(), width, height) .get_thumbnail(mxc.to_string(), width, height)
.await? .await?

View file

@ -2046,11 +2046,13 @@ pub(crate) async fn media_download_route(
body: Ar<authenticated_media::get_content::v1::Request>, body: Ar<authenticated_media::get_content::v1::Request>,
) -> Result<Ra<authenticated_media::get_content::v1::Response>> { ) -> Result<Ra<authenticated_media::get_content::v1::Response>> {
let mxc = MxcData::new(services().globals.server_name(), &body.media_id)?; let mxc = MxcData::new(services().globals.server_name(), &body.media_id)?;
let Some(crate::service::media::FileMeta { let Some((
content_disposition, crate::service::media::FileMeta {
content_type, content_disposition,
content_type,
},
file, file,
}) = services().media.get(mxc.to_string()).await? )) = services().media.get(mxc.to_string()).await?
else { else {
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::NotYetUploaded, ErrorKind::NotYetUploaded,
@ -2091,11 +2093,13 @@ pub(crate) async fn media_thumbnail_route(
Error::BadRequest(ErrorKind::InvalidParam, "Height is invalid.") Error::BadRequest(ErrorKind::InvalidParam, "Height is invalid.")
})?; })?;
let Some(crate::service::media::FileMeta { let Some((
content_type, crate::service::media::FileMeta {
content_type,
..
},
file, file,
.. )) = services().media.get_thumbnail(mxc.to_string(), width, height).await?
}) = services().media.get_thumbnail(mxc.to_string(), width, height).await?
else { else {
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::NotYetUploaded, ErrorKind::NotYetUploaded,

View file

@ -21,9 +21,7 @@ pub(crate) struct FileMeta {
// only the filename instead of the entire `Content-Disposition` header. // only the filename instead of the entire `Content-Disposition` header.
#[allow(dead_code)] #[allow(dead_code)]
pub(crate) content_disposition: Option<String>, pub(crate) content_disposition: Option<String>,
pub(crate) content_type: Option<String>, pub(crate) content_type: Option<String>,
pub(crate) file: Vec<u8>,
} }
pub(crate) struct Service { pub(crate) struct Service {
@ -84,7 +82,10 @@ impl Service {
/// Downloads a file. /// Downloads a file.
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub(crate) async fn get(&self, mxc: String) -> Result<Option<FileMeta>> { pub(crate) async fn get(
&self,
mxc: String,
) -> Result<Option<(FileMeta, Vec<u8>)>> {
if let Ok((content_disposition, content_type, key)) = if let Ok((content_disposition, content_type, key)) =
self.db.search_file_metadata(mxc, 0, 0) self.db.search_file_metadata(mxc, 0, 0)
{ {
@ -96,11 +97,13 @@ impl Service {
file.read_to_end(&mut file_data).await?; file.read_to_end(&mut file_data).await?;
Ok(Some(FileMeta { Ok(Some((
content_disposition, FileMeta {
content_type, content_disposition,
file: file_data, content_type,
})) },
file_data,
)))
} else { } else {
Ok(None) Ok(None)
} }
@ -224,7 +227,7 @@ impl Service {
mxc: String, mxc: String,
width: u32, width: u32,
height: u32, height: u32,
) -> Result<Option<FileMeta>> { ) -> Result<Option<(FileMeta, Vec<u8>)>> {
// 0, 0 because that's the original file // 0, 0 because that's the original file
let (width, height, crop) = let (width, height, crop) =
Self::thumbnail_properties(width, height).unwrap_or((0, 0, false)); Self::thumbnail_properties(width, height).unwrap_or((0, 0, false));
@ -237,11 +240,13 @@ impl Service {
let mut file = Vec::new(); let mut file = Vec::new();
File::open(path).await?.read_to_end(&mut file).await?; File::open(path).await?.read_to_end(&mut file).await?;
return Ok(Some(FileMeta { return Ok(Some((
content_disposition, FileMeta {
content_type, content_disposition,
file: file.clone(), content_type,
})); },
file.clone(),
)));
} }
let Ok((content_disposition, content_type, key)) = let Ok((content_disposition, content_type, key)) =
@ -271,11 +276,13 @@ impl Service {
let Some(thumbnail_bytes) = thumbnail_result? else { let Some(thumbnail_bytes) = thumbnail_result? else {
debug!("Returning source image as-is"); debug!("Returning source image as-is");
return Ok(Some(FileMeta { return Ok(Some((
content_disposition, FileMeta {
content_type, content_disposition,
content_type,
},
file, file,
})); )));
}; };
debug!("Saving created thumbnail"); debug!("Saving created thumbnail");
@ -294,10 +301,12 @@ impl Service {
let mut f = File::create(path).await?; let mut f = File::create(path).await?;
f.write_all(&thumbnail_bytes).await?; f.write_all(&thumbnail_bytes).await?;
Ok(Some(FileMeta { Ok(Some((
content_disposition, FileMeta {
content_type, content_disposition,
file: thumbnail_bytes.clone(), content_type,
})) },
thumbnail_bytes.clone(),
)))
} }
} }