| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#![allow(clippy::type_complexity)] |
| 20 |
|
| 21 |
use anyhow::{Result, anyhow}; |
| 22 |
use async_channel::Sender; |
| 23 |
use std::collections::HashMap; |
| 24 |
use std::sync::{Arc, LazyLock, Mutex, RwLock}; |
| 25 |
|
| 26 |
use crate::app::Action; |
| 27 |
use crate::i18n::i18n_f; |
| 28 |
use podcasts_data::dbqueries; |
| 29 |
use podcasts_data::downloader::{DownloadProgress, get_episode}; |
| 30 |
use podcasts_data::errors::DownloadError; |
| 31 |
use podcasts_data::{EpisodeId, EpisodeModel}; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
pub(crate) type ActiveProgress = Arc<Mutex<Progress>>; |
| 36 |
pub(crate) type DownloadProgressLock = Arc<RwLock<HashMap<EpisodeId, ActiveProgress>>>; |
| 37 |
pub(crate) static ACTIVE_DOWNLOADS: LazyLock<DownloadProgressLock> = |
| 38 |
LazyLock::new(|| Arc::new(RwLock::new(HashMap::new()))); |
| 39 |
|
| 40 |
#[derive(Debug, Default)] |
| 41 |
pub(crate) struct Progress { |
| 42 |
total_bytes: u64, |
| 43 |
downloaded_bytes: u64, |
| 44 |
cancel: bool, |
| 45 |
} |
| 46 |
|
| 47 |
impl Progress { |
| 48 |
pub(crate) fn get_fraction(&self) -> f64 { |
| 49 |
let ratio = self.downloaded_bytes as f64 / self.total_bytes as f64; |
| 50 |
debug!("{:?}", self); |
| 51 |
debug!("Ratio completed: {}", ratio); |
| 52 |
|
| 53 |
if ratio >= 1.0 { |
| 54 |
return 1.0; |
| 55 |
}; |
| 56 |
ratio |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
impl DownloadProgress for Progress { |
| 61 |
fn get_downloaded(&self) -> u64 { |
| 62 |
self.downloaded_bytes |
| 63 |
} |
| 64 |
|
| 65 |
fn set_downloaded(&mut self, downloaded: u64) { |
| 66 |
self.downloaded_bytes = downloaded |
| 67 |
} |
| 68 |
|
| 69 |
fn set_size(&mut self, bytes: u64) { |
| 70 |
self.total_bytes = bytes; |
| 71 |
} |
| 72 |
|
| 73 |
fn get_size(&self) -> u64 { |
| 74 |
self.total_bytes |
| 75 |
} |
| 76 |
|
| 77 |
fn should_cancel(&self) -> bool { |
| 78 |
self.cancel |
| 79 |
} |
| 80 |
|
| 81 |
fn cancel(&mut self) { |
| 82 |
self.cancel = true; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
pub(crate) fn add(sender: Sender<Action>, id: EpisodeId, directory: String) -> Result<()> { |
| 87 |
|
| 88 |
let prog = Arc::new(Mutex::new(Progress::default())); |
| 89 |
|
| 90 |
match ACTIVE_DOWNLOADS.write() { |
| 91 |
Ok(mut guard) => guard.insert(id, prog.clone()), |
| 92 |
Err(err) => return Err(anyhow!("ActiveDownloads: {}.", err)), |
| 93 |
}; |
| 94 |
|
| 95 |
crate::RUNTIME.spawn(async move { |
| 96 |
if let Ok(mut episode) = dbqueries::get_episode_widget_from_id(id) { |
| 97 |
let id = episode.id(); |
| 98 |
|
| 99 |
match get_episode(&mut episode, directory.as_str(), Some(prog)).await { |
| 100 |
Ok(_) => (), |
| 101 |
Err(DownloadError::DownloadCancelled) => (), |
| 102 |
Err(e) => { |
| 103 |
send!( |
| 104 |
sender, |
| 105 |
Action::ErrorNotification(i18n_f("Download failed: {}", &[&e.to_string()])) |
| 106 |
); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if let Ok(mut m) = ACTIVE_DOWNLOADS.write() { |
| 111 |
let progress = m.remove(&id); |
| 112 |
debug!("Removed: {:?}", progress); |
| 113 |
} |
| 114 |
} |
| 115 |
}); |
| 116 |
|
| 117 |
Ok(()) |
| 118 |
} |
| 119 |
|
| 120 |
#[cfg(test)] |
| 121 |
mod tests { |
| 122 |
use super::*; |
| 123 |
|
| 124 |
use podcasts_data::dbqueries; |
| 125 |
use podcasts_data::pipeline::pipeline; |
| 126 |
use podcasts_data::utils::get_download_dir; |
| 127 |
use podcasts_data::{Episode, EpisodeModel, Save, Source}; |
| 128 |
|
| 129 |
use podcasts_data::downloader::get_episode; |
| 130 |
|
| 131 |
use std::fs; |
| 132 |
use std::path::Path; |
| 133 |
use std::{thread, time}; |
| 134 |
|
| 135 |
#[test] |
| 136 |
|
| 137 |
|
| 138 |
#[ignore] |
| 139 |
|
| 140 |
|
| 141 |
fn test_start_dl() -> Result<()> { |
| 142 |
let url = "https://web.archive.org/web/20180120110727if_/https://rss.acast.com/thetipoff"; |
| 143 |
|
| 144 |
let mut source = Source::from_url(url)?; |
| 145 |
|
| 146 |
let sid = source.id(); |
| 147 |
source.set_http_etag(None); |
| 148 |
source.set_last_modified(None); |
| 149 |
source.save()?; |
| 150 |
let rt = tokio::runtime::Runtime::new()?; |
| 151 |
rt.block_on(pipeline(vec![source]))?; |
| 152 |
|
| 153 |
|
| 154 |
let pd = dbqueries::get_podcast_from_source_id(sid)?; |
| 155 |
let title = "Coming soon... The Tip Off"; |
| 156 |
let guid = "tag:soundcloud,2010:tracks/327539708"; |
| 157 |
|
| 158 |
let episode: Episode = dbqueries::get_episode(Some(guid), title, pd.id())?; |
| 159 |
|
| 160 |
let download_dir = get_download_dir(pd.title())?; |
| 161 |
let dir2 = download_dir.clone(); |
| 162 |
let (sender, _) = async_channel::unbounded(); |
| 163 |
add(sender, episode.id(), download_dir)?; |
| 164 |
assert_eq!(ACTIVE_DOWNLOADS.read().unwrap().len(), 1); |
| 165 |
|
| 166 |
|
| 167 |
thread::sleep(time::Duration::from_secs(20)); |
| 168 |
|
| 169 |
let final_path = format!("{}/{}.mp3", &dir2, episode.id().0); |
| 170 |
assert_eq!(ACTIVE_DOWNLOADS.read().unwrap().len(), 0); |
| 171 |
assert!(Path::new(&final_path).exists()); |
| 172 |
fs::remove_file(final_path)?; |
| 173 |
Ok(()) |
| 174 |
} |
| 175 |
|
| 176 |
#[test] |
| 177 |
|
| 178 |
#[ignore] |
| 179 |
fn test_dl_steal_the_stars() -> Result<()> { |
| 180 |
let url = |
| 181 |
"https://web.archive.org/web/20180120104957if_/https://rss.art19.com/steal-the-stars"; |
| 182 |
|
| 183 |
let mut source = Source::from_url(url)?; |
| 184 |
|
| 185 |
let sid = source.id(); |
| 186 |
source.set_http_etag(None); |
| 187 |
source.set_last_modified(None); |
| 188 |
source.save()?; |
| 189 |
let rt = tokio::runtime::Runtime::new()?; |
| 190 |
rt.block_on(pipeline(vec![source]))?; |
| 191 |
|
| 192 |
|
| 193 |
let pd = dbqueries::get_podcast_from_source_id(sid)?; |
| 194 |
let title = "Introducing Steal the Stars"; |
| 195 |
let guid = "gid://art19-episode-locator/V0/S6kmOE2cviFS0HD-IUYOPRO0fvjTPYmCsMDe5bjABnA"; |
| 196 |
|
| 197 |
|
| 198 |
let mut episode = dbqueries::get_episode(Some(guid), title, pd.id())?.into(); |
| 199 |
let download_dir = get_download_dir(pd.title())?; |
| 200 |
|
| 201 |
rt.block_on(get_episode(&mut episode, &download_dir, None))?; |
| 202 |
|
| 203 |
let final_path = format!("{}/{}.mp3", &download_dir, episode.id().0); |
| 204 |
assert!(Path::new(&final_path).exists()); |
| 205 |
fs::remove_file(final_path)?; |
| 206 |
Ok(()) |
| 207 |
} |
| 208 |
} |