1
0
Fork 0
forked from mirror/grapevine

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> {
let mxc = MxcData::new(&body.server_name, &body.media_id)?;
if let Some(FileMeta {
content_type,
if let Some((
FileMeta {
content_type,
..
},
file,
..
}) = services().media.get(mxc.to_string()).await?
)) = services().media.get(mxc.to_string()).await?
{
Ok(authenticated_media_client::get_content::v1::Response {
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> {
let mxc = MxcData::new(&body.server_name, &body.media_id)?;
if let Some(FileMeta {
content_type,
if let Some((
FileMeta {
content_type,
..
},
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 {
file,
@ -811,11 +815,13 @@ async fn get_content_thumbnail_route_ruma(
}
};
if let Some(FileMeta {
content_type,
if let Some((
FileMeta {
content_type,
..
},
file,
..
}) =
)) =
services().media.get_thumbnail(mxc.to_string(), width, height).await?
{
return Ok(make_response(file, content_type));
@ -866,11 +872,13 @@ async fn get_content_thumbnail_route_ruma(
get_remote_content(&mxc).await?;
if let Some(FileMeta {
content_type,
if let Some((
FileMeta {
content_type,
..
},
file,
..
}) = services()
)) = services()
.media
.get_thumbnail(mxc.to_string(), width, height)
.await?

View file

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

View file

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