1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-08-04 11:43:58 +00:00
continuwuity/src/database/key_value/media.rs

83 lines
2.5 KiB
Rust
Raw Normal View History

2022-10-05 12:45:54 +02:00
use ruma::api::client::error::ErrorKind;
2022-10-05 20:34:31 +02:00
use crate::{database::KeyValueDatabase, service, utils, Error, Result};
2022-10-05 18:36:12 +02:00
impl service::media::Data for KeyValueDatabase {
2022-10-05 20:34:31 +02:00
fn create_file_metadata(
&self,
mxc: String,
width: u32,
height: u32,
content_disposition: Option<&str>,
content_type: Option<&str>,
) -> Result<Vec<u8>> {
let mut key = mxc.as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(&width.to_be_bytes());
key.extend_from_slice(&height.to_be_bytes());
key.push(0xff);
key.extend_from_slice(
content_disposition
.as_ref()
.map(|f| f.as_bytes())
.unwrap_or_default(),
);
key.push(0xff);
key.extend_from_slice(
content_type
.as_ref()
.map(|c| c.as_bytes())
.unwrap_or_default(),
);
2021-06-04 08:06:12 +04:30
self.mediaid_file.insert(&key, &[])?;
2022-09-07 13:25:51 +02:00
Ok(key)
}
2022-10-05 20:34:31 +02:00
fn search_file_metadata(
&self,
mxc: String,
width: u32,
height: u32,
) -> Result<(Option<String>, Option<String>, Vec<u8>)> {
2020-05-18 17:53:34 +02:00
let mut prefix = mxc.as_bytes().to_vec();
prefix.push(0xff);
2020-05-19 18:31:34 +02:00
prefix.extend_from_slice(&0_u32.to_be_bytes()); // Width = 0 if it's not a thumbnail
prefix.extend_from_slice(&0_u32.to_be_bytes()); // Height = 0 if it's not a thumbnail
prefix.push(0xff);
2020-05-18 17:53:34 +02:00
2022-10-05 20:34:31 +02:00
let (key, _) = self
.mediaid_file
.scan_prefix(prefix)
.next()
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Media not found"))?;
2021-06-06 16:58:32 +04:30
2022-09-07 13:25:51 +02:00
let mut parts = key.rsplit(|&b| b == 0xff);
2020-05-19 18:31:34 +02:00
2022-09-07 13:25:51 +02:00
let content_type = parts
.next()
.map(|bytes| {
utils::string_from_bytes(bytes).map_err(|_| {
Error::bad_database("Content type in mediaid_file is invalid unicode.")
})
2022-09-07 13:25:51 +02:00
})
.transpose()?;
2021-06-06 16:58:32 +04:30
2022-09-07 13:25:51 +02:00
let content_disposition_bytes = parts
.next()
.ok_or_else(|| Error::bad_database("Media ID in db is invalid."))?;
2020-05-19 18:31:34 +02:00
2022-09-07 13:25:51 +02:00
let content_disposition = if content_disposition_bytes.is_empty() {
None
2020-05-18 17:53:34 +02:00
} else {
2022-09-07 13:25:51 +02:00
Some(
utils::string_from_bytes(content_disposition_bytes).map_err(|_| {
2022-10-05 20:34:31 +02:00
Error::bad_database("Content Disposition in mediaid_file is invalid unicode.")
2022-09-07 13:25:51 +02:00
})?,
)
};
Ok((content_disposition, content_type, key))
2020-05-18 17:53:34 +02:00
}
}