mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-20 17:11:19 +02:00
refactor: downloads/uploads every media from a tweet async way
This commit is contained in:
88
src/util.rs
88
src/util.rs
@@ -15,45 +15,73 @@ pub async fn generate_media_ids(
|
|||||||
cache_path: &str,
|
cache_path: &str,
|
||||||
mastodon: &Mastodon,
|
mastodon: &Mastodon,
|
||||||
) -> (String, Vec<String>) {
|
) -> (String, Vec<String>) {
|
||||||
|
let mut media_url = "".to_string();
|
||||||
let mut media_ids: Vec<String> = vec![];
|
let mut media_ids: Vec<String> = vec![];
|
||||||
let mut media_url: String = "".to_string();
|
|
||||||
|
|
||||||
if let Some(m) = &tweet.extended_entities {
|
if let Some(m) = &tweet.extended_entities {
|
||||||
for media in &m.media {
|
// create tasks list
|
||||||
// attribute the media url
|
let mut tasks = vec![];
|
||||||
|
|
||||||
|
// size of media_ids vector, should be equal to the media vector
|
||||||
|
media_ids.resize(m.media.len(), String::new());
|
||||||
|
|
||||||
|
for (i, media) in m.media.iter().enumerate() {
|
||||||
|
// attribute media url
|
||||||
media_url = media.url.clone();
|
media_url = media.url.clone();
|
||||||
let local_tweet_media_path = match get_tweet_media(media, cache_path).await {
|
|
||||||
Ok(m) => m,
|
|
||||||
Err(e) => {
|
|
||||||
error!("Cannot get tweet media for {}: {}", &media.url, e);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mastodon_media_ids = match mastodon
|
// clone everything we need
|
||||||
.media(Cow::from(local_tweet_media_path.to_owned()))
|
let cache_path = String::from(cache_path);
|
||||||
{
|
let media = media.clone();
|
||||||
Ok(m) => {
|
let mastodon = mastodon.clone();
|
||||||
remove_file(&local_tweet_media_path).await.unwrap_or_else(|e|
|
|
||||||
warn!("Attachment for {} has been uploaded, but I’m unable to remove the existing file: {}", &local_tweet_media_path, e));
|
|
||||||
m.id
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!(
|
|
||||||
"Attachment {} cannot be uploaded to Mastodon Instance: {}",
|
|
||||||
&local_tweet_media_path, e
|
|
||||||
);
|
|
||||||
// file is no longer useful, deleting
|
|
||||||
remove_file(&local_tweet_media_path).await.unwrap_or_else(|e|
|
|
||||||
warn!("Attachment for {} has been uploaded, but I’m unable to remove the existing file: {}", &local_tweet_media_path, e));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
media_ids.push(mastodon_media_ids);
|
let task = tokio::task::spawn(async move {
|
||||||
|
// get the tweet embedded media
|
||||||
|
let local_tweet_media_path = match get_tweet_media(&media, &cache_path).await {
|
||||||
|
Ok(l) => l,
|
||||||
|
Err(e) => {
|
||||||
|
return Err(ScootalooError::new(&format!(
|
||||||
|
"Cannot get tweet media for {}: {}",
|
||||||
|
&media.url, e
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// upload media to Mastodon
|
||||||
|
let mastodon_media =
|
||||||
|
match mastodon.media(Cow::from(local_tweet_media_path.to_owned())) {
|
||||||
|
Ok(l) => l,
|
||||||
|
Err(e) => {
|
||||||
|
return Err(ScootalooError::new(&format!(
|
||||||
|
"Attachment {} cannot be uploaded to Mastodon Instance: {}",
|
||||||
|
&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))
|
||||||
|
});
|
||||||
|
|
||||||
|
tasks.push(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
for task in tasks {
|
||||||
|
match task.await {
|
||||||
|
// insert the media at the right place
|
||||||
|
Ok(Ok((i, v))) => media_ids[i] = v,
|
||||||
|
Ok(Err(e)) => warn!("{}", e),
|
||||||
|
Err(e) => error!("Something went wrong when joining the main thread: {}", e),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// in case some media_ids slot remained empty due to errors, remove them
|
||||||
|
media_ids.retain(|x| !x.is_empty());
|
||||||
|
|
||||||
(media_url, media_ids)
|
(media_url, media_ids)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user