mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-21 09:31:19 +02:00
refactor: better logic flow for uploading/deleting media
This commit is contained in:
18
src/util.rs
18
src/util.rs
@@ -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 doesn’t matter if we can’t 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 I’m unable to remove the existing file: {}", &local_tweet_media_path, e));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok((i, mastodon_media.id))
|
Ok((i, mastodon_media.id))
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user