use futures_util::StreamExt;
use indicatif::{ProgressBar, ProgressStyle, MultiProgress};
use reqwest::Client;
use tokio::task::JoinSet;
use std::{
collections::HashMap,
fs::{copy, read_dir, remove_file, rename, File},
io::Write, cmp::min,
};
use crate::{
cache::{copy_cached_version, get_cached_versions},
config::Cfg,
db::{mods_get_info, userlist_add_disabled_versions},
error::{ErrorType, MLError, MLE},
modrinth::Version,
List,
};
const PROGRESS_CHARS: &str = "#>-";
pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<()> {
let cached = get_cached_versions(&config.cache);
let mp = MultiProgress::new();
let mut js = JoinSet::new();
let style_spinner = ProgressStyle::with_template("{spinner:.green}{wide_msg}").unwrap();
let all = mp.add(ProgressBar::new(versions.len().try_into().unwrap()));
all.set_style(ProgressStyle::with_template("{wide_msg}{pos}/{len} [{bar:.green/lime}]").unwrap().progress_chars(PROGRESS_CHARS));
all.set_message("Downloading");
for ver in versions {
let p = mp.insert_before(&all, ProgressBar::new(1));
p.set_style(style_spinner.clone());
js.spawn(download_version(config.clone(), list.clone(), ver, cached.clone(), p));
// std::thread::sleep(std::time::Duration::from_millis(200));
}
while js.join_next().await.is_some() { all.inc(1) }
all.finish();
// mp.clear().unwrap();
Ok(())
}
async fn download_version(config: Cfg, list: List, version: Version, mut cached: HashMap<String, String>, progress: ProgressBar) -> MLE<()> {
let project_info = mods_get_info(&config, &version.project_id)?;
let dl_path = String::from(&list.download_folder);
progress.set_message(format!("{} - {}", project_info.title, version.id));
//Check cache if already downloaded
let c = cached.remove(&version.id);
if c.is_some() {
progress.set_message(format!("Get {} from cache", version.id));
copy_cached_version(&c.unwrap(), &dl_path);
} else {
let files = version.files;
let file = match files.clone().into_iter().find(|f| f.primary) {
Some(f) => f,
None => files[0].clone()
};
let mut splitname: Vec<&str> = file.filename.split('.').collect();
let extension = match splitname.pop().ok_or("") {
Ok(e) => e,
Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")),
};
let filename = format!(
"{}.mr.{}.{}.{}",
splitname.join("."),
version.project_id,
version.id,
extension
);
download_file(
&file.url,
&list.download_folder,
&filename,
&progress
)
.await?;
progress.set_message(format!("Copy {} to cache", version.id));
let dl_path_file = format!("{}/{}", list.download_folder, filename);
let cache_path = format!("{}/{}", &config.clone().cache, filename);
copy(dl_path_file, cache_path)?;
}
progress.finish_with_message(format!("✓{} - {}", project_info.title, version.id));
Ok(())
}
async fn download_file(url: &str, path: &str, name: &str, progress: &ProgressBar) -> MLE<()> {
let dl_path_file = format!("{}/{}", path, name);
let res = Client::new().get(url).send().await?;
let size = res.content_length().expect("Couldn't get content length");
let style_bar_byte = ProgressStyle::with_template("{spinner:.green}{wide_msg}{bytes}/{total_bytes} [{bar:.green/lime}]")
.unwrap()
.progress_chars(PROGRESS_CHARS);
progress.set_length(size);
progress.set_style(style_bar_byte);
// download chunks
let mut file = File::create(&dl_path_file)?;
let mut stream = res.bytes_stream();
let mut downloaded: u64 = 0;
while let Some(item) = stream.next().await {
// progress.inc(1);
let chunk = item?;
file.write_all(&chunk)?;
// Progress bar
let new = min(downloaded + (chunk.len() as u64), size);
downloaded = new;
progress.set_position(new);
// std::thread::sleep(std::time::Duration::from_millis(100));
}
Ok(())
}
pub fn disable_version(
config: Cfg,
current_list: List,
versionid: String,
mod_id: String,
) -> MLE<()> {
//println!("Disabling version {} for mod {}", versionid, mod_id);
let file = get_file_path(current_list.clone(), String::from(&versionid))?;
let disabled = format!("{}.disabled", file);
rename(file, disabled)?;
userlist_add_disabled_versions(config, current_list.id, versionid, mod_id)?;
Ok(())
}
pub fn delete_version(list: List, version: String) -> MLE<()> {
let file = get_file_path(list, version)?;
remove_file(file)?;
Ok(())
}
pub fn get_file_path(list: List, versionid: String) -> MLE<String> {
let mut names: HashMap<String, String> = HashMap::new();
for file in read_dir(list.download_folder)? {
let path = file?.path();
if path.is_file() {
let pathstr = match path.to_str().ok_or("") {
Ok(s) => s,
Err(..) => return Err(MLError::new(ErrorType::Other, "INVALID_PATH")),
};
let namesplit: Vec<&str> = pathstr.split('.').collect();
let ver_id = namesplit[namesplit.len() - 2];
names.insert(String::from(ver_id), String::from(pathstr));
}
}
let filename = match names.get(&versionid).ok_or("") {
Ok(n) => n,
Err(..) => {
return Err(MLError::new(
ErrorType::ArgumentError,
"VERSION_NOT_FOUND_IN_FILES",
))
}
};
Ok(filename.to_owned())
}
pub fn get_downloaded_versions(list: List) -> MLE<HashMap<String, String>> {
let mut versions: HashMap<String, String> = HashMap::new();
for file in read_dir(&list.download_folder)? {
let path = file?.path();
if path.is_file() && path.extension().ok_or("BAH").unwrap() == "jar" {
let pathstr = path.to_str().ok_or("BAH").unwrap();
let namesplit: Vec<&str> = pathstr.split('.').collect();
versions.insert(
String::from(namesplit[namesplit.len() - 3]),
String::from(namesplit[namesplit.len() - 2]),
);
}
}
Ok(versions)
}
pub fn clean_list_dir(list: &List) -> MLE<()> {
let dl_path = &list.download_folder;
println!(" └Clean directory for: {}", list.id);
for entry in std::fs::read_dir(dl_path)? {
let entry = entry?;
std::fs::remove_file(entry.path())?;
}
Ok(())
}