refactor: better logic flow for uploading/deleting media

This commit is contained in:
VC
2022-11-08 10:54:42 +01:00
parent 89f1372f9f
commit 4415c4ac12

View File

@@ -56,23 +56,21 @@ pub async fn generate_media_ids(
}; };
// upload media to Mastodon // upload media to Mastodon
let mastodon_media = let mastodon_media = mastodon.media(Cow::from(local_tweet_media_path.to_owned()));
match mastodon.media(Cow::from(local_tweet_media_path.to_owned())) { // at this point, we can safely erase the original file
Ok(l) => l, // it doesnt matter if we cant remove, cache_media fn is idempotent
remove_file(&local_tweet_media_path).await.ok();
let mastodon_media = match mastodon_media {
Ok(m) => m,
Err(e) => { Err(e) => {
remove_file(&local_tweet_media_path).await.unwrap();
return Err(ScootalooError::new(&format!( return Err(ScootalooError::new(&format!(
"Attachment {} cannot be uploaded to Mastodon Instance: {}", "Attachment {} cannot be uploaded to Mastodon Instance: {}",
&local_tweet_media_path, e &local_tweet_media_path, e
))); )))
} }
}; };
// at this point, we can safely erase the original file
if let Err(e) = remove_file(&local_tweet_media_path).await {
ScootalooError::new(&format!("Attachment {} has been uploaded but Im unable to remove the existing file: {}", &local_tweet_media_path, e));
}
Ok((i, mastodon_media.id)) Ok((i, mastodon_media.id))
}); });