media: use FileMeta instead of tuples

This commit is contained in:
Lambda 2024-08-11 17:19:12 +00:00
parent 1ccb1e572b
commit a4b7df1b3a
4 changed files with 50 additions and 76 deletions

View file

@ -167,7 +167,7 @@ pub(crate) async fn create_content_route(
filename: Some(filename), filename: Some(filename),
}) })
.as_ref(), .as_ref(),
body.content_type.as_deref(), body.content_type.clone(),
&body.file, &body.file,
) )
.await?; .await?;
@ -333,7 +333,7 @@ pub(crate) async fn get_remote_content(
.create( .create(
mxc.to_string(), mxc.to_string(),
response.content.content_disposition.as_ref(), response.content.content_disposition.as_ref(),
response.content.content_type.as_deref(), response.content.content_type.clone(),
&response.content.file, &response.content.file,
) )
.await?; .await?;
@ -851,7 +851,7 @@ async fn get_content_thumbnail_route_ruma(
.upload_thumbnail( .upload_thumbnail(
mxc.to_string(), mxc.to_string(),
None, None,
resp.content.content_type.as_deref(), resp.content.content_type.clone(),
width, width,
height, height,
&resp.content.file, &resp.content.file,

View file

@ -2,7 +2,10 @@ use ruma::api::client::error::ErrorKind;
use crate::{ use crate::{
database::KeyValueDatabase, database::KeyValueDatabase,
service::{self, media::MediaFileKey}, service::{
self,
media::{FileMeta, MediaFileKey},
},
utils, Error, Result, utils, Error, Result,
}; };
@ -12,8 +15,7 @@ impl service::media::Data for KeyValueDatabase {
mxc: String, mxc: String,
width: u32, width: u32,
height: u32, height: u32,
content_disposition: Option<&str>, meta: &FileMeta,
content_type: Option<&str>,
) -> Result<MediaFileKey> { ) -> Result<MediaFileKey> {
let mut key = mxc.as_bytes().to_vec(); let mut key = mxc.as_bytes().to_vec();
key.push(0xFF); key.push(0xFF);
@ -21,14 +23,17 @@ impl service::media::Data for KeyValueDatabase {
key.extend_from_slice(&height.to_be_bytes()); key.extend_from_slice(&height.to_be_bytes());
key.push(0xFF); key.push(0xFF);
key.extend_from_slice( key.extend_from_slice(
content_disposition meta.content_disposition
.as_ref() .as_ref()
.map(|f| f.as_bytes()) .map(String::as_bytes)
.unwrap_or_default(), .unwrap_or_default(),
); );
key.push(0xFF); key.push(0xFF);
key.extend_from_slice( key.extend_from_slice(
content_type.as_ref().map(|c| c.as_bytes()).unwrap_or_default(), meta.content_type
.as_ref()
.map(String::as_bytes)
.unwrap_or_default(),
); );
let key = MediaFileKey::new(key); let key = MediaFileKey::new(key);
@ -43,7 +48,7 @@ impl service::media::Data for KeyValueDatabase {
mxc: String, mxc: String,
width: u32, width: u32,
height: u32, height: u32,
) -> Result<(Option<String>, Option<String>, MediaFileKey)> { ) -> Result<(FileMeta, MediaFileKey)> {
let mut prefix = mxc.as_bytes().to_vec(); let mut prefix = mxc.as_bytes().to_vec();
prefix.push(0xFF); prefix.push(0xFF);
prefix.extend_from_slice(&width.to_be_bytes()); prefix.extend_from_slice(&width.to_be_bytes());
@ -86,6 +91,12 @@ impl service::media::Data for KeyValueDatabase {
}, },
)?) )?)
}; };
Ok((content_disposition, content_type, key)) Ok((
FileMeta {
content_disposition,
content_type,
},
key,
))
} }
} }

View file

@ -48,22 +48,21 @@ impl Service {
&self, &self,
mxc: String, mxc: String,
content_disposition: Option<&ContentDisposition>, content_disposition: Option<&ContentDisposition>,
content_type: Option<&str>, content_type: Option<String>,
file: &[u8], file: &[u8],
) -> Result<()> { ) -> Result<FileMeta> {
// Width, Height = 0 if it's not a thumbnail let meta = FileMeta {
let key = self.db.create_file_metadata( content_disposition: content_disposition
mxc, .map(ContentDisposition::to_string),
0,
0,
content_disposition.map(ContentDisposition::to_string).as_deref(),
content_type, content_type,
)?; };
// Width, Height = 0 if it's not a thumbnail
let key = self.db.create_file_metadata(mxc, 0, 0, &meta)?;
let path = services().globals.get_media_file(&key); let path = services().globals.get_media_file(&key);
let mut f = File::create(path).await?; let mut f = File::create(path).await?;
f.write_all(file).await?; f.write_all(file).await?;
Ok(()) Ok(meta)
} }
/// Uploads or replaces a file thumbnail. /// Uploads or replaces a file thumbnail.
@ -72,25 +71,23 @@ impl Service {
pub(crate) async fn upload_thumbnail( pub(crate) async fn upload_thumbnail(
&self, &self,
mxc: String, mxc: String,
content_disposition: Option<&str>, content_disposition: Option<String>,
content_type: Option<&str>, content_type: Option<String>,
width: u32, width: u32,
height: u32, height: u32,
file: &[u8], file: &[u8],
) -> Result<()> { ) -> Result<FileMeta> {
let key = self.db.create_file_metadata( let meta = FileMeta {
mxc,
width,
height,
content_disposition, content_disposition,
content_type, content_type,
)?; };
let key = self.db.create_file_metadata(mxc, width, height, &meta)?;
let path = services().globals.get_media_file(&key); let path = services().globals.get_media_file(&key);
let mut f = File::create(path).await?; let mut f = File::create(path).await?;
f.write_all(file).await?; f.write_all(file).await?;
Ok(()) Ok(meta)
} }
/// Downloads a file. /// Downloads a file.
@ -99,9 +96,7 @@ impl Service {
&self, &self,
mxc: String, mxc: String,
) -> Result<Option<(FileMeta, Vec<u8>)>> { ) -> Result<Option<(FileMeta, Vec<u8>)>> {
if let Ok((content_disposition, content_type, key)) = if let Ok((meta, key)) = self.db.search_file_metadata(mxc, 0, 0) {
self.db.search_file_metadata(mxc, 0, 0)
{
let path = services().globals.get_media_file(&key); let path = services().globals.get_media_file(&key);
let mut file_data = Vec::new(); let mut file_data = Vec::new();
let Ok(mut file) = File::open(path).await else { let Ok(mut file) = File::open(path).await else {
@ -110,13 +105,7 @@ impl Service {
file.read_to_end(&mut file_data).await?; file.read_to_end(&mut file_data).await?;
Ok(Some(( Ok(Some((meta, file_data)))
FileMeta {
content_disposition,
content_type,
},
file_data,
)))
} else { } else {
Ok(None) Ok(None)
} }
@ -245,7 +234,7 @@ impl Service {
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));
if let Ok((content_disposition, content_type, key)) = if let Ok((meta, key)) =
self.db.search_file_metadata(mxc.clone(), width, height) self.db.search_file_metadata(mxc.clone(), width, height)
{ {
debug!("Using saved thumbnail"); debug!("Using saved thumbnail");
@ -253,17 +242,10 @@ 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(( return Ok(Some((meta, file.clone())));
FileMeta {
content_disposition,
content_type,
},
file.clone(),
)));
} }
let Ok((content_disposition, content_type, key)) = let Ok((meta, key)) = self.db.search_file_metadata(mxc.clone(), 0, 0)
self.db.search_file_metadata(mxc.clone(), 0, 0)
else { else {
debug!("Original image not found, can't generate thumbnail"); debug!("Original image not found, can't generate thumbnail");
return Ok(None); return Ok(None);
@ -289,37 +271,20 @@ 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(( return Ok(Some((meta, file)));
FileMeta {
content_disposition,
content_type,
},
file,
)));
}; };
debug!("Saving created thumbnail"); debug!("Saving created thumbnail");
// Save thumbnail in database so we don't have to generate it // Save thumbnail in database so we don't have to generate it
// again next time // again next time
let thumbnail_key = self.db.create_file_metadata( let thumbnail_key =
mxc, self.db.create_file_metadata(mxc, width, height, &meta)?;
width,
height,
content_disposition.as_deref(),
content_type.as_deref(),
)?;
let path = services().globals.get_media_file(&thumbnail_key); let path = services().globals.get_media_file(&thumbnail_key);
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(( Ok(Some((meta, thumbnail_bytes.clone())))
FileMeta {
content_disposition,
content_type,
},
thumbnail_bytes.clone(),
)))
} }
} }

View file

@ -1,4 +1,4 @@
use super::MediaFileKey; use super::{FileMeta, MediaFileKey};
use crate::Result; use crate::Result;
pub(crate) trait Data: Send + Sync { pub(crate) trait Data: Send + Sync {
@ -7,15 +7,13 @@ pub(crate) trait Data: Send + Sync {
mxc: String, mxc: String,
width: u32, width: u32,
height: u32, height: u32,
content_disposition: Option<&str>, meta: &FileMeta,
content_type: Option<&str>,
) -> Result<MediaFileKey>; ) -> Result<MediaFileKey>;
/// Returns `content_disposition`, `content_type` and the `metadata` key.
fn search_file_metadata( fn search_file_metadata(
&self, &self,
mxc: String, mxc: String,
width: u32, width: u32,
height: u32, height: u32,
) -> Result<(Option<String>, Option<String>, MediaFileKey)>; ) -> Result<(FileMeta, MediaFileKey)>;
} }