Move media deletion helper to media module
This commit is contained in:
parent
b56e11e81d
commit
f037a4d58c
6 changed files with 40 additions and 36 deletions
|
@ -14,7 +14,7 @@ use mitra::ethereum::{
|
||||||
sync::save_current_block_number,
|
sync::save_current_block_number,
|
||||||
utils::key_to_ethereum_address,
|
utils::key_to_ethereum_address,
|
||||||
};
|
};
|
||||||
use mitra::media::remove_files;
|
use mitra::media::{remove_files, remove_media};
|
||||||
use mitra::models::{
|
use mitra::models::{
|
||||||
attachments::queries::delete_unused_attachments,
|
attachments::queries::delete_unused_attachments,
|
||||||
cleanup::find_orphaned_files,
|
cleanup::find_orphaned_files,
|
||||||
|
@ -252,7 +252,7 @@ impl DeleteProfile {
|
||||||
maybe_delete_person = Some(activity);
|
maybe_delete_person = Some(activity);
|
||||||
};
|
};
|
||||||
let deletion_queue = delete_profile(db_client, &profile.id).await?;
|
let deletion_queue = delete_profile(db_client, &profile.id).await?;
|
||||||
deletion_queue.process(config).await;
|
remove_media(config, deletion_queue).await;
|
||||||
// Send Delete(Person) activities
|
// Send Delete(Person) activities
|
||||||
if let Some(activity) = maybe_delete_person {
|
if let Some(activity) = maybe_delete_person {
|
||||||
activity.enqueue(db_client).await?;
|
activity.enqueue(db_client).await?;
|
||||||
|
@ -287,7 +287,7 @@ impl DeletePost {
|
||||||
maybe_delete_note = Some(activity);
|
maybe_delete_note = Some(activity);
|
||||||
};
|
};
|
||||||
let deletion_queue = delete_post(db_client, &post.id).await?;
|
let deletion_queue = delete_post(db_client, &post.id).await?;
|
||||||
deletion_queue.process(config).await;
|
remove_media(config, deletion_queue).await;
|
||||||
// Send Delete(Note) activity
|
// Send Delete(Note) activity
|
||||||
if let Some(activity) = maybe_delete_note {
|
if let Some(activity) = maybe_delete_note {
|
||||||
activity.enqueue(db_client).await?;
|
activity.enqueue(db_client).await?;
|
||||||
|
@ -316,7 +316,7 @@ impl DeleteEmoji {
|
||||||
self.hostname.as_deref(),
|
self.hostname.as_deref(),
|
||||||
).await?;
|
).await?;
|
||||||
let deletion_queue = delete_emoji(db_client, &emoji.id).await?;
|
let deletion_queue = delete_emoji(db_client, &emoji.id).await?;
|
||||||
deletion_queue.process(config).await;
|
remove_media(config, deletion_queue).await;
|
||||||
println!("emoji deleted");
|
println!("emoji deleted");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -338,7 +338,7 @@ impl DeleteExtraneousPosts {
|
||||||
let posts = find_extraneous_posts(db_client, &updated_before).await?;
|
let posts = find_extraneous_posts(db_client, &updated_before).await?;
|
||||||
for post_id in posts {
|
for post_id in posts {
|
||||||
let deletion_queue = delete_post(db_client, &post_id).await?;
|
let deletion_queue = delete_post(db_client, &post_id).await?;
|
||||||
deletion_queue.process(config).await;
|
remove_media(config, deletion_queue).await;
|
||||||
println!("post {} deleted", post_id);
|
println!("post {} deleted", post_id);
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -362,7 +362,7 @@ impl DeleteUnusedAttachments {
|
||||||
db_client,
|
db_client,
|
||||||
&created_before,
|
&created_before,
|
||||||
).await?;
|
).await?;
|
||||||
deletion_queue.process(config).await;
|
remove_media(config, deletion_queue).await;
|
||||||
println!("unused attachments deleted");
|
println!("unused attachments deleted");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -412,7 +412,7 @@ impl DeleteEmptyProfiles {
|
||||||
for profile_id in profiles {
|
for profile_id in profiles {
|
||||||
let profile = get_profile_by_id(db_client, &profile_id).await?;
|
let profile = get_profile_by_id(db_client, &profile_id).await?;
|
||||||
let deletion_queue = delete_profile(db_client, &profile.id).await?;
|
let deletion_queue = delete_profile(db_client, &profile.id).await?;
|
||||||
deletion_queue.process(config).await;
|
remove_media(config, deletion_queue).await;
|
||||||
println!("profile {} deleted", profile.acct);
|
println!("profile {} deleted", profile.acct);
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -9,6 +9,7 @@ use crate::activitypub::{
|
||||||
};
|
};
|
||||||
use crate::database::{DatabaseClient, DatabaseError};
|
use crate::database::{DatabaseClient, DatabaseError};
|
||||||
use crate::errors::ValidationError;
|
use crate::errors::ValidationError;
|
||||||
|
use crate::media::remove_media;
|
||||||
use crate::models::{
|
use crate::models::{
|
||||||
posts::queries::{
|
posts::queries::{
|
||||||
delete_post,
|
delete_post,
|
||||||
|
@ -49,7 +50,7 @@ pub async fn handle_delete(
|
||||||
let deletion_queue = delete_profile(db_client, &profile.id).await?;
|
let deletion_queue = delete_profile(db_client, &profile.id).await?;
|
||||||
let config = config.clone();
|
let config = config.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
deletion_queue.process(&config).await;
|
remove_media(&config, deletion_queue).await;
|
||||||
});
|
});
|
||||||
log::info!("deleted profile {}", profile.acct);
|
log::info!("deleted profile {}", profile.acct);
|
||||||
return Ok(Some(PERSON));
|
return Ok(Some(PERSON));
|
||||||
|
@ -73,7 +74,7 @@ pub async fn handle_delete(
|
||||||
let deletion_queue = delete_post(db_client, &post.id).await?;
|
let deletion_queue = delete_post(db_client, &post.id).await?;
|
||||||
let config = config.clone();
|
let config = config.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
deletion_queue.process(&config).await;
|
remove_media(&config, deletion_queue).await;
|
||||||
});
|
});
|
||||||
Ok(Some(NOTE))
|
Ok(Some(NOTE))
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ use crate::ethereum::{
|
||||||
update_expired_subscriptions,
|
update_expired_subscriptions,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
use crate::media::remove_media;
|
||||||
use crate::monero::subscriptions::check_monero_subscriptions;
|
use crate::monero::subscriptions::check_monero_subscriptions;
|
||||||
use crate::models::{
|
use crate::models::{
|
||||||
posts::queries::{delete_post, find_extraneous_posts},
|
posts::queries::{delete_post, find_extraneous_posts},
|
||||||
|
@ -138,7 +139,7 @@ pub async fn delete_extraneous_posts(
|
||||||
let posts = find_extraneous_posts(db_client, &updated_before).await?;
|
let posts = find_extraneous_posts(db_client, &updated_before).await?;
|
||||||
for post_id in posts {
|
for post_id in posts {
|
||||||
let deletion_queue = delete_post(db_client, &post_id).await?;
|
let deletion_queue = delete_post(db_client, &post_id).await?;
|
||||||
deletion_queue.process(config).await;
|
remove_media(config, deletion_queue).await;
|
||||||
log::info!("deleted post {}", post_id);
|
log::info!("deleted post {}", post_id);
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -157,7 +158,7 @@ pub async fn delete_empty_profiles(
|
||||||
for profile_id in profiles {
|
for profile_id in profiles {
|
||||||
let profile = get_profile_by_id(db_client, &profile_id).await?;
|
let profile = get_profile_by_id(db_client, &profile_id).await?;
|
||||||
let deletion_queue = delete_profile(db_client, &profile.id).await?;
|
let deletion_queue = delete_profile(db_client, &profile.id).await?;
|
||||||
deletion_queue.process(config).await;
|
remove_media(config, deletion_queue).await;
|
||||||
log::info!("deleted profile {}", profile.acct);
|
log::info!("deleted profile {}", profile.acct);
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -34,6 +34,7 @@ use crate::mastodon_api::{
|
||||||
errors::MastodonError,
|
errors::MastodonError,
|
||||||
oauth::auth::get_current_user,
|
oauth::auth::get_current_user,
|
||||||
};
|
};
|
||||||
|
use crate::media::remove_media;
|
||||||
use crate::models::{
|
use crate::models::{
|
||||||
posts::helpers::{can_create_post, can_view_post},
|
posts::helpers::{can_create_post, can_view_post},
|
||||||
posts::queries::{
|
posts::queries::{
|
||||||
|
@ -286,7 +287,7 @@ async fn delete_status(
|
||||||
).await?;
|
).await?;
|
||||||
let deletion_queue = delete_post(db_client, &status_id).await?;
|
let deletion_queue = delete_post(db_client, &status_id).await?;
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
deletion_queue.process(&config).await;
|
remove_media(&config, deletion_queue).await;
|
||||||
});
|
});
|
||||||
delete_note.enqueue(db_client).await?;
|
delete_note.enqueue(db_client).await?;
|
||||||
|
|
||||||
|
|
25
src/media.rs
25
src/media.rs
|
@ -4,8 +4,12 @@ use std::path::Path;
|
||||||
|
|
||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
|
use mitra_config::Config;
|
||||||
use mitra_utils::files::{get_media_type_extension, write_file};
|
use mitra_utils::files::{get_media_type_extension, write_file};
|
||||||
|
|
||||||
|
use crate::ipfs::store as ipfs_store;
|
||||||
|
use crate::models::cleanup::DeletionQueue;
|
||||||
|
|
||||||
pub const SUPPORTED_MEDIA_TYPES: [&str; 11] = [
|
pub const SUPPORTED_MEDIA_TYPES: [&str; 11] = [
|
||||||
"audio/mpeg",
|
"audio/mpeg",
|
||||||
"audio/ogg",
|
"audio/ogg",
|
||||||
|
@ -62,6 +66,27 @@ pub fn remove_files(files: Vec<String>, from_dir: &Path) -> () {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn remove_media(
|
||||||
|
config: &Config,
|
||||||
|
queue: DeletionQueue,
|
||||||
|
) -> () {
|
||||||
|
remove_files(queue.files, &config.media_dir());
|
||||||
|
if !queue.ipfs_objects.is_empty() {
|
||||||
|
match &config.ipfs_api_url {
|
||||||
|
Some(ipfs_api_url) => {
|
||||||
|
ipfs_store::remove(ipfs_api_url, queue.ipfs_objects).await
|
||||||
|
.unwrap_or_else(|err| log::error!("{}", err));
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
log::error!(
|
||||||
|
"can not remove objects because IPFS API URL is not set: {:?}",
|
||||||
|
queue.ipfs_objects,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use mitra_utils::files::sniff_media_type;
|
use mitra_utils::files::sniff_media_type;
|
||||||
|
|
|
@ -1,34 +1,10 @@
|
||||||
use mitra_config::Config;
|
|
||||||
|
|
||||||
use crate::database::{DatabaseClient, DatabaseError};
|
use crate::database::{DatabaseClient, DatabaseError};
|
||||||
use crate::ipfs::store as ipfs_store;
|
|
||||||
use crate::media::remove_files;
|
|
||||||
|
|
||||||
pub struct DeletionQueue {
|
pub struct DeletionQueue {
|
||||||
pub files: Vec<String>,
|
pub files: Vec<String>,
|
||||||
pub ipfs_objects: Vec<String>,
|
pub ipfs_objects: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DeletionQueue {
|
|
||||||
pub async fn process(self, config: &Config) -> () {
|
|
||||||
remove_files(self.files, &config.media_dir());
|
|
||||||
if !self.ipfs_objects.is_empty() {
|
|
||||||
match &config.ipfs_api_url {
|
|
||||||
Some(ipfs_api_url) => {
|
|
||||||
ipfs_store::remove(ipfs_api_url, self.ipfs_objects).await
|
|
||||||
.unwrap_or_else(|err| log::error!("{}", err));
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
log::error!(
|
|
||||||
"can not remove objects because IPFS API URL is not set: {:?}",
|
|
||||||
self.ipfs_objects,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn find_orphaned_files(
|
pub async fn find_orphaned_files(
|
||||||
db_client: &impl DatabaseClient,
|
db_client: &impl DatabaseClient,
|
||||||
files: Vec<String>,
|
files: Vec<String>,
|
||||||
|
|
Loading…
Reference in a new issue