db functions to delete media via MXC

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-02-21 20:34:11 -05:00 committed by June
parent cc762c49e2
commit 27c29e6063
3 changed files with 58 additions and 1 deletions

View file

@ -8,8 +8,9 @@ use std::{
pub(crate) use data::Data;
use serde::Serialize;
use tracing::{debug, error, warn};
use crate::{services, Result};
use crate::{services, Error, Result};
use image::imageops::FilterType;
use tokio::{
@ -18,6 +19,7 @@ use tokio::{
sync::Mutex,
};
#[derive(Debug)]
pub struct FileMeta {
pub content_disposition: Option<String>,
pub content_type: Option<String>,
@ -89,6 +91,50 @@ impl Service {
Ok(())
}
/// Deletes a file in the database and from the media directory via an MXC
pub async fn delete(&self, mxc: String) -> Result<()> {
if let Ok(filemeta) = self.get(mxc.clone()).await {
match filemeta {
Some(filemeta) => {
debug!("Got file metadata: {:?}", filemeta);
let file_key = filemeta.file;
debug!("File key from file metadata: {:?}", file_key);
let file_path = if cfg!(feature = "sha256_media") {
services().globals.get_media_file_new(&file_key)
} else {
#[allow(deprecated)]
services().globals.get_media_file(&file_key)
};
debug!("Got local file path: {:?}", file_path);
debug!(
"Deleting local file {:?} from filesystem, original MXC: {mxc}",
file_path
);
tokio::fs::remove_file(file_path).await?;
debug!("Deleting MXC {mxc} from database");
self.db.delete_file_mxc(mxc)?;
Ok(())
}
None => {
warn!(
"MXC {mxc} does not exist in our database or file in MXC does not exist."
);
Err(Error::bad_database(
"MXC does not exist in our database or file in MXC does not exist.",
))
}
}
} else {
// we shouldn't get to this point as this is failing to actually attempt to get the file metadata (Result)
error!("Failed getting file metadata for MXC \"{mxc}\" in database (does not exist or database issue?)");
Err(Error::bad_database("Failed getting file metadata via MXC in database (does not exist or database issue?)"))
}
}
/// Uploads or replaces a file thumbnail.
pub async fn upload_thumbnail(
&self,