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
+9 -16
View File
@@ -1,3 +1,4 @@
use conduit::debug_info;
use ruma::api::client::error::ErrorKind;
use tracing::debug;
@@ -55,7 +56,7 @@ impl crate::media::Data for KeyValueDatabase {
if key == mxc.as_bytes().to_vec() {
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)?;
}
}
@@ -70,11 +71,11 @@ impl crate::media::Data for KeyValueDatabase {
let mut prefix = mxc.as_bytes().to_vec();
prefix.push(0xFF);
let mut keys: Vec<Vec<u8>> = vec![];
for (key, _) in self.mediaid_file.scan_prefix(prefix) {
keys.push(key);
}
let keys: Vec<Vec<u8>> = self
.mediaid_file
.scan_prefix(prefix)
.map(|(key, _)| key)
.collect();
if keys.is_empty() {
return Err(Error::bad_database(
@@ -100,7 +101,7 @@ impl crate::media::Data for KeyValueDatabase {
.mediaid_file
.scan_prefix(prefix)
.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);
@@ -129,15 +130,7 @@ impl crate::media::Data for KeyValueDatabase {
/// Gets all the media keys in our database (this includes all the metadata
/// associated with it such as width, height, content-type, etc)
fn get_all_media_keys(&self) -> Result<Vec<Vec<u8>>> {
let mut keys: Vec<Vec<u8>> = vec![];
for (key, _) in self.mediaid_file.iter() {
keys.push(key);
}
Ok(keys)
}
fn get_all_media_keys(&self) -> Vec<Vec<u8>> { self.mediaid_file.iter().map(|(key, _)| key).collect() }
fn remove_url_preview(&self, url: &str) -> Result<()> { self.url_previews.remove(url.as_bytes()) }