From 758e608e6c331df2a5900de7f932f9b498573ed1 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Wed, 7 Dec 2022 17:45:16 +0100 Subject: groundwork for downloads; cleanup --- src/files.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'src/files.rs') diff --git a/src/files.rs b/src/files.rs index 2c5994d..959da75 100644 --- a/src/files.rs +++ b/src/files.rs @@ -2,9 +2,24 @@ use std::{fs::{File, read_dir, remove_file}, io::Write, collections::HashMap}; use futures_util::StreamExt; use reqwest::Client; -use crate::List; +use crate::{List, modrinth::Version}; -pub async fn download_file(url: String, path: String, name: String) -> Result<(), Box> { +pub async fn download_versions(current_list: List, versions: Vec) -> Result> { + + let dl_path = String::from(¤t_list.download_folder); + + for ver in versions { + let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); + let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); + let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?; + let filename = format!("{}.mr{}.{}", splitname.join("."), ver.id, extension); + download_file(primary_file.url, current_list.clone().download_folder, filename).await?; + } + + Ok(dl_path) +} + +async fn download_file(url: String, path: String, name: String) -> Result<(), Box> { println!("Downloading {}", url); let dl_path_file = format!("{}/{}", path, name); let res = Client::new() @@ -50,3 +65,17 @@ pub fn get_file_path(list: List, versionid: String) -> Result Result, Box> { + let mut versions: Vec = vec![]; + for file in read_dir(list.download_folder)? { + let path = file?.path(); + if path.is_file() && path.extension().ok_or("BAH")? == "jar" { + let pathstr = path.to_str().ok_or("BAH")?; + let namesplit: Vec<&str> = pathstr.split('.').collect(); + versions.push(String::from(namesplit[namesplit.len() - 2].split_at(2).1)); + } + } + + Ok(versions) +} -- cgit v1.2.3