media: decomplexify get_all_media_keys for deleting all MXC URIs

wow this was terrible, early strawberry code

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-05-22 21:19:36 -04:00 committed by June 🍓🦴
parent cb73ae3732
commit d49507bc21
3 changed files with 107 additions and 119 deletions

View file

@ -1,3 +1,4 @@
use conduit::debug_info;
use ruma::api::client::error::ErrorKind; use ruma::api::client::error::ErrorKind;
use tracing::debug; use tracing::debug;
@ -55,7 +56,7 @@ impl crate::media::Data for KeyValueDatabase {
if key == mxc.as_bytes().to_vec() { if key == mxc.as_bytes().to_vec() {
let user = string_from_bytes(&value).unwrap_or_default(); let user = string_from_bytes(&value).unwrap_or_default();
debug!("Deleting key \"{key:?}\" which was uploaded by user {user}"); debug_info!("Deleting key \"{key:?}\" which was uploaded by user {user}");
self.mediaid_user.remove(&key)?; self.mediaid_user.remove(&key)?;
} }
} }
@ -70,11 +71,11 @@ impl crate::media::Data for KeyValueDatabase {
let mut prefix = mxc.as_bytes().to_vec(); let mut prefix = mxc.as_bytes().to_vec();
prefix.push(0xFF); prefix.push(0xFF);
let mut keys: Vec<Vec<u8>> = vec![]; let keys: Vec<Vec<u8>> = self
.mediaid_file
for (key, _) in self.mediaid_file.scan_prefix(prefix) { .scan_prefix(prefix)
keys.push(key); .map(|(key, _)| key)
} .collect();
if keys.is_empty() { if keys.is_empty() {
return Err(Error::bad_database( return Err(Error::bad_database(
@ -100,7 +101,7 @@ impl crate::media::Data for KeyValueDatabase {
.mediaid_file .mediaid_file
.scan_prefix(prefix) .scan_prefix(prefix)
.next() .next()
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Media not found"))?; .ok_or_else(|| Error::BadRequest(ErrorKind::NotFound, "Media not found"))?;
let mut parts = key.rsplit(|&b| b == 0xFF); let mut parts = key.rsplit(|&b| b == 0xFF);
@ -129,15 +130,7 @@ impl crate::media::Data for KeyValueDatabase {
/// Gets all the media keys in our database (this includes all the metadata /// Gets all the media keys in our database (this includes all the metadata
/// associated with it such as width, height, content-type, etc) /// associated with it such as width, height, content-type, etc)
fn get_all_media_keys(&self) -> Result<Vec<Vec<u8>>> { fn get_all_media_keys(&self) -> Vec<Vec<u8>> { self.mediaid_file.iter().map(|(key, _)| key).collect() }
let mut keys: Vec<Vec<u8>> = vec![];
for (key, _) in self.mediaid_file.iter() {
keys.push(key);
}
Ok(keys)
}
fn remove_url_preview(&self, url: &str) -> Result<()> { self.url_previews.remove(url.as_bytes()) } fn remove_url_preview(&self, url: &str) -> Result<()> { self.url_previews.remove(url.as_bytes()) }

View file

@ -15,7 +15,7 @@ pub trait Data: Send + Sync {
fn search_mxc_metadata_prefix(&self, mxc: String) -> Result<Vec<Vec<u8>>>; fn search_mxc_metadata_prefix(&self, mxc: String) -> Result<Vec<Vec<u8>>>;
fn get_all_media_keys(&self) -> Result<Vec<Vec<u8>>>; fn get_all_media_keys(&self) -> Vec<Vec<u8>>;
// TODO: use this // TODO: use this
#[allow(dead_code)] #[allow(dead_code)]

View file

@ -183,7 +183,8 @@ impl Service {
/// Deletes all remote only media files in the given at or after /// Deletes all remote only media files in the given at or after
/// time/duration. Returns a u32 with the amount of media files deleted. /// time/duration. Returns a u32 with the amount of media files deleted.
pub async fn delete_all_remote_media_at_after_time(&self, time: String) -> Result<u32> { pub async fn delete_all_remote_media_at_after_time(&self, time: String) -> Result<u32> {
if let Ok(all_keys) = self.db.get_all_media_keys() { let all_keys = self.db.get_all_media_keys();
let user_duration: SystemTime = match cyborgtime::parse_duration(&time) { let user_duration: SystemTime = match cyborgtime::parse_duration(&time) {
Ok(duration) => { Ok(duration) => {
debug!("Parsed duration: {:?}", duration); debug!("Parsed duration: {:?}", duration);
@ -268,8 +269,7 @@ impl Service {
} }
debug!( debug!(
"Finished going through all our media in database for eligible keys to delete, checking if these are \ "Finished going through all our media in database for eligible keys to delete, checking if these are empty"
empty"
); );
if remote_mxcs.is_empty() { if remote_mxcs.is_empty() {
@ -287,11 +287,6 @@ impl Service {
} }
Ok(deletion_count) Ok(deletion_count)
} else {
Err(Error::bad_database(
"Failed to get all our media keys (filesystem or database issue?).",
))
}
} }
/// Returns width, height of the thumbnail and whether it should be cropped. /// Returns width, height of the thumbnail and whether it should be cropped.
@ -529,7 +524,7 @@ mod tests {
fn search_mxc_metadata_prefix(&self, _mxc: String) -> Result<Vec<Vec<u8>>> { todo!() } fn search_mxc_metadata_prefix(&self, _mxc: String) -> Result<Vec<Vec<u8>>> { todo!() }
fn get_all_media_keys(&self) -> Result<Vec<Vec<u8>>> { todo!() } fn get_all_media_keys(&self) -> Vec<Vec<u8>> { todo!() }
fn search_file_metadata( fn search_file_metadata(
&self, _mxc: String, _width: u32, _height: u32, &self, _mxc: String, _width: u32, _height: u32,