Files
continuwuity/src/service/media/data.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

279 lines
6.6 KiB
Rust
Raw Normal View History

2024-10-07 17:54:27 +00:00
use std::{sync::Arc, time::Duration};
2024-05-27 03:17:20 +00:00
2024-12-14 21:58:01 -05:00
use conduwuit::{
Err, Result, debug, debug_info, err,
utils::{ReadyExt, str_from_bytes, stream::TryIgnore, string_from_bytes},
2024-08-16 01:13:20 +00:00
};
2024-10-07 17:54:27 +00:00
use database::{Database, Interfix, Map};
2024-08-08 17:18:30 +00:00
use futures::StreamExt;
use ruma::{Mxc, OwnedMxcUri, UserId, http_headers::ContentDisposition};
2022-09-07 13:25:51 +02:00
2024-08-27 01:15:31 +00:00
use super::{preview::UrlPreviewData, thumbnail::Dim};
2024-05-26 21:29:19 +00:00
2024-06-28 22:51:39 +00:00
pub(crate) struct Data {
mediaid_file: Arc<Map>,
mediaid_user: Arc<Map>,
url_previews: Arc<Map>,
2024-05-26 21:29:19 +00:00
}
2024-07-15 07:21:10 +00:00
#[derive(Debug)]
pub(super) struct Metadata {
pub(super) content_disposition: Option<ContentDisposition>,
2024-07-15 07:21:10 +00:00
pub(super) content_type: Option<String>,
pub(super) key: Vec<u8>,
}
2024-05-27 03:17:20 +00:00
impl Data {
2024-06-28 22:51:39 +00:00
pub(super) fn new(db: &Arc<Database>) -> Self {
2024-05-27 03:17:20 +00:00
Self {
2024-06-28 22:51:39 +00:00
mediaid_file: db["mediaid_file"].clone(),
mediaid_user: db["mediaid_user"].clone(),
url_previews: db["url_previews"].clone(),
2024-05-27 03:17:20 +00:00
}
}
pub(super) fn create_file_metadata(
&self,
mxc: &Mxc<'_>,
user: Option<&UserId>,
dim: &Dim,
content_disposition: Option<&ContentDisposition>,
2024-08-27 01:15:31 +00:00
content_type: Option<&str>,
2024-05-26 21:29:19 +00:00
) -> Result<Vec<u8>> {
2024-10-07 17:54:27 +00:00
let dim: &[u32] = &[dim.width, dim.height];
let key = (mxc, dim, content_disposition, content_type);
2024-11-28 05:54:34 +00:00
let key = database::serialize_key(key)?;
2024-10-07 17:54:27 +00:00
self.mediaid_file.insert(&key, []);
2024-08-16 01:13:20 +00:00
if let Some(user) = user {
2024-10-07 17:54:27 +00:00
let key = (mxc, user);
self.mediaid_user.put_raw(key, user);
2024-05-26 21:29:19 +00:00
}
2024-11-28 05:54:34 +00:00
Ok(key.to_vec())
2024-05-26 21:29:19 +00:00
}
2024-08-08 17:18:30 +00:00
pub(super) async fn delete_file_mxc(&self, mxc: &Mxc<'_>) {
2024-08-16 01:13:20 +00:00
debug!("MXC URI: {mxc}");
2024-05-26 21:29:19 +00:00
2024-10-07 17:54:27 +00:00
let prefix = (mxc, Interfix);
2024-08-08 17:18:30 +00:00
self.mediaid_file
2024-10-07 17:54:27 +00:00
.keys_prefix_raw(&prefix)
2024-08-08 17:18:30 +00:00
.ignore_err()
2024-10-07 17:54:27 +00:00
.ready_for_each(|key| self.mediaid_file.remove(key))
2024-08-08 17:18:30 +00:00
.await;
2024-05-26 21:29:19 +00:00
2024-08-08 17:18:30 +00:00
self.mediaid_user
2024-10-07 17:54:27 +00:00
.stream_prefix_raw(&prefix)
2024-08-08 17:18:30 +00:00
.ignore_err()
.ready_for_each(|(key, val)| {
debug_assert!(
key.starts_with(mxc.to_string().as_bytes()),
"key should start with the mxc"
);
2024-08-08 17:18:30 +00:00
2024-10-07 17:54:27 +00:00
let user = str_from_bytes(val).unwrap_or_default();
debug_info!("Deleting key {key:?} which was uploaded by user {user}");
self.mediaid_user.remove(key);
2024-08-08 17:18:30 +00:00
})
.await;
2024-05-26 21:29:19 +00:00
}
/// Searches for all files with the given MXC
2024-08-08 17:18:30 +00:00
pub(super) async fn search_mxc_metadata_prefix(&self, mxc: &Mxc<'_>) -> Result<Vec<Vec<u8>>> {
2024-08-16 01:13:20 +00:00
debug!("MXC URI: {mxc}");
2024-05-26 21:29:19 +00:00
2024-10-07 17:54:27 +00:00
let prefix = (mxc, Interfix);
2024-05-26 21:29:19 +00:00
let keys: Vec<Vec<u8>> = self
.mediaid_file
2024-10-07 17:54:27 +00:00
.keys_prefix_raw(&prefix)
2024-08-08 17:18:30 +00:00
.ignore_err()
2024-10-04 02:23:28 +00:00
.map(<[u8]>::to_vec)
2024-08-08 17:18:30 +00:00
.collect()
.await;
2024-05-26 21:29:19 +00:00
if keys.is_empty() {
2024-08-16 01:13:20 +00:00
return Err!(Database("Failed to find any keys in database for `{mxc}`",));
2024-05-26 21:29:19 +00:00
}
2024-08-16 01:13:20 +00:00
debug!("Got the following keys: {keys:?}");
2024-05-26 21:29:19 +00:00
Ok(keys)
}
pub(super) async fn search_file_metadata(
&self,
mxc: &Mxc<'_>,
dim: &Dim,
) -> Result<Metadata> {
2024-10-07 17:54:27 +00:00
let dim: &[u32] = &[dim.width, dim.height];
let prefix = (mxc, dim, Interfix);
2024-05-26 21:29:19 +00:00
2024-08-08 17:18:30 +00:00
let key = self
2024-05-26 21:29:19 +00:00
.mediaid_file
2024-10-07 17:54:27 +00:00
.keys_prefix_raw(&prefix)
2024-08-08 17:18:30 +00:00
.ignore_err()
.map(ToOwned::to_owned)
2024-05-26 21:29:19 +00:00
.next()
2024-08-08 17:18:30 +00:00
.await
2024-10-07 17:54:27 +00:00
.ok_or_else(|| err!(Request(NotFound("Media not found"))))?;
2024-05-26 21:29:19 +00:00
let mut parts = key.rsplit(|&b| b == 0xFF);
let content_type = parts
.next()
.map(string_from_bytes)
.transpose()
2026-01-04 03:04:37 +00:00
.map_err(|e| err!(Database(error!(%mxc, "Content-type is invalid: {e}"))))?;
2024-05-26 21:29:19 +00:00
let content_disposition = parts
2024-05-26 21:29:19 +00:00
.next()
.map(Some)
2026-01-04 03:04:37 +00:00
.ok_or_else(|| err!(Database(error!(%mxc, "Media ID in db is invalid."))))?
.filter(|bytes| !bytes.is_empty())
.map(string_from_bytes)
.transpose()
2026-01-04 03:04:37 +00:00
.map_err(|e| err!(Database(error!(%mxc, "Content-type is invalid: {e}"))))?
.as_deref()
.map(str::parse)
.transpose()?;
2024-07-15 07:21:10 +00:00
Ok(Metadata { content_disposition, content_type, key })
2024-05-26 21:29:19 +00:00
}
/// Gets all the MXCs associated with a user
2024-08-08 17:18:30 +00:00
pub(super) async fn get_all_user_mxcs(&self, user_id: &UserId) -> Vec<OwnedMxcUri> {
self.mediaid_user
2024-08-08 17:18:30 +00:00
.stream()
.ignore_err()
.ready_filter_map(|(key, user): (&str, &UserId)| {
(user == user_id).then(|| key.into())
})
.collect()
2024-08-08 17:18:30 +00:00
.await
}
2024-05-26 21:29:19 +00:00
/// Gets all the media keys in our database (this includes all the metadata
/// associated with it such as width, height, content-type, etc)
2024-08-08 17:18:30 +00:00
pub(crate) async fn get_all_media_keys(&self) -> Vec<Vec<u8>> {
self.mediaid_file
.raw_keys()
.ignore_err()
.map(<[u8]>::to_vec)
.collect()
.await
}
2024-05-26 21:29:19 +00:00
#[inline]
2024-08-08 17:18:30 +00:00
pub(super) fn remove_url_preview(&self, url: &str) -> Result<()> {
self.url_previews.remove(url.as_bytes());
Ok(())
}
2024-05-26 21:29:19 +00:00
pub(super) fn set_url_preview(
&self,
url: &str,
data: &UrlPreviewData,
timestamp: Duration,
) -> Result<()> {
2024-05-26 21:29:19 +00:00
let mut value = Vec::<u8>::new();
value.extend_from_slice(&timestamp.as_secs().to_be_bytes());
value.push(0xFF);
value.extend_from_slice(
data.title
.as_ref()
.map(String::as_bytes)
.unwrap_or_default(),
);
value.push(0xFF);
value.extend_from_slice(
data.description
.as_ref()
.map(String::as_bytes)
.unwrap_or_default(),
);
value.push(0xFF);
value.extend_from_slice(
data.image
.as_ref()
.map(String::as_bytes)
.unwrap_or_default(),
);
value.push(0xFF);
value.extend_from_slice(&data.image_size.unwrap_or(0).to_be_bytes());
value.push(0xFF);
value.extend_from_slice(&data.image_width.unwrap_or(0).to_be_bytes());
value.push(0xFF);
value.extend_from_slice(&data.image_height.unwrap_or(0).to_be_bytes());
2024-08-08 17:18:30 +00:00
self.url_previews.insert(url.as_bytes(), &value);
Ok(())
2024-05-26 21:29:19 +00:00
}
2024-08-08 17:18:30 +00:00
pub(super) async fn get_url_preview(&self, url: &str) -> Result<UrlPreviewData> {
let values = self.url_previews.get(url).await?;
2024-05-26 21:29:19 +00:00
let mut values = values.split(|&b| b == 0xFF);
let _ts = values.next();
/* if we ever decide to use timestamp, this is here.
match values.next().map(|b| u64::from_be_bytes(b.try_into().expect("valid BE array"))) {
Some(0) => None,
x => x,
};*/
let title = match values
.next()
.and_then(|b| String::from_utf8(b.to_vec()).ok())
{
| Some(s) if s.is_empty() => None,
| x => x,
2024-05-26 21:29:19 +00:00
};
let description = match values
.next()
.and_then(|b| String::from_utf8(b.to_vec()).ok())
{
| Some(s) if s.is_empty() => None,
| x => x,
2024-05-26 21:29:19 +00:00
};
let image = match values
.next()
.and_then(|b| String::from_utf8(b.to_vec()).ok())
{
| Some(s) if s.is_empty() => None,
| x => x,
2024-05-26 21:29:19 +00:00
};
let image_size = match values
.next()
.map(|b| usize::from_be_bytes(b.try_into().unwrap_or_default()))
{
| Some(0) => None,
| x => x,
2024-05-26 21:29:19 +00:00
};
let image_width = match values
.next()
.map(|b| u32::from_be_bytes(b.try_into().unwrap_or_default()))
{
| Some(0) => None,
| x => x,
2024-05-26 21:29:19 +00:00
};
let image_height = match values
.next()
.map(|b| u32::from_be_bytes(b.try_into().unwrap_or_default()))
{
| Some(0) => None,
| x => x,
2024-05-26 21:29:19 +00:00
};
2024-03-05 19:48:54 -05:00
2024-08-08 17:18:30 +00:00
Ok(UrlPreviewData {
2024-05-26 21:29:19 +00:00
title,
description,
image,
image_size,
image_width,
image_height,
})
}
2022-09-07 13:25:51 +02:00
}