diff --git a/src/activitypub/fetcher/fetchers.rs b/src/activitypub/fetcher/fetchers.rs index 3f9cbff..082b88b 100644 --- a/src/activitypub/fetcher/fetchers.rs +++ b/src/activitypub/fetcher/fetchers.rs @@ -73,13 +73,23 @@ async fn send_request( Ok(data) } +pub async fn fetch_file( + url: &str, + output_dir: &Path, +) -> Result<(String, Option), FetchError> { + let response = reqwest::get(url).await?; + let file_data = response.bytes().await?; + let (file_name, media_type) = save_file(file_data.to_vec(), output_dir)?; + Ok((file_name, media_type)) +} + pub async fn fetch_avatar_and_banner( actor: &Actor, media_dir: &Path, ) -> Result<(Option, Option), FetchError> { let avatar = match &actor.icon { Some(icon) => { - let (file_name, _) = fetch_attachment( + let (file_name, _) = fetch_file( &icon.url, media_dir, ).await?; @@ -89,7 +99,7 @@ pub async fn fetch_avatar_and_banner( }; let banner = match &actor.image { Some(image) => { - let (file_name, _) = fetch_attachment( + let (file_name, _) = fetch_file( &image.url, media_dir, ).await?; @@ -166,16 +176,6 @@ pub async fn fetch_profile_by_actor_id( Ok(profile_data) } -pub async fn fetch_attachment( - url: &str, - output_dir: &Path, -) -> Result<(String, Option), FetchError> { - let response = reqwest::get(url).await?; - let file_data = response.bytes().await?; - let (file_name, media_type) = save_file(file_data.to_vec(), output_dir)?; - Ok((file_name, media_type)) -} - pub async fn fetch_object( instance: &Instance, object_url: &str, diff --git a/src/activitypub/receiver.rs b/src/activitypub/receiver.rs index fb570a4..b199193 100644 --- a/src/activitypub/receiver.rs +++ b/src/activitypub/receiver.rs @@ -50,7 +50,7 @@ use super::actor::Actor; use super::deliverer::deliver_activity; use super::fetcher::fetchers::{ fetch_avatar_and_banner, - fetch_attachment, + fetch_file, fetch_object, }; use super::fetcher::helpers::{ @@ -286,7 +286,7 @@ pub async fn import_post( }; let attachment_url = attachment.url .ok_or(ValidationError("attachment URL is missing"))?; - let (file_name, media_type) = fetch_attachment(&attachment_url, &output_dir).await + let (file_name, media_type) = fetch_file(&attachment_url, &output_dir).await .map_err(|_| ValidationError("failed to fetch attachment"))?; log::info!("downloaded attachment {}", attachment_url); downloaded.push((