summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cache.rs36
-rw-r--r--src/config.rs4
-rw-r--r--src/files.rs67
-rw-r--r--src/lib.rs1
4 files changed, 86 insertions, 22 deletions
diff --git a/src/cache.rs b/src/cache.rs
new file mode 100644
index 0000000..30c9f09
--- /dev/null
+++ b/src/cache.rs
@@ -0,0 +1,36 @@
1use std::{collections::HashMap, fs::{read_dir, copy}};
2
3use crate::devdir;
4
5/// .
6///
7/// # Panics
8///
9/// Panics if .
10pub fn get_cached_versions(path: &str) -> HashMap<String, String> {
11 let mut versions: HashMap<String, String> = HashMap::new();
12 for file in read_dir(devdir(path)).unwrap() {
13 let path = file.unwrap().path();
14 if path.is_file() && path.extension().ok_or("BAH").unwrap() == "jar" {
15 let pathstr = path.to_str().ok_or("BAH").unwrap();
16 let namesplit: Vec<&str> = pathstr.split('.').collect();
17 versions.insert(
18 String::from(namesplit[namesplit.len() - 2]),
19 String::from(pathstr),
20 );
21 }
22 }
23 versions
24}
25
26/// .
27///
28/// # Panics
29///
30/// Panics if .
31pub fn copy_cached_version(version_path: &str, download_path: &str) {
32 let versplit: Vec<&str> = version_path.split('/').collect();
33 let download = format!("{}{}", download_path, versplit[versplit.len() - 1]);
34 // println!("{:#?}", download);
35 copy(version_path, download).unwrap();
36}
diff --git a/src/config.rs b/src/config.rs
index 1b54d5f..a9a937e 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -10,6 +10,7 @@ use crate::{devdir, error::MLE};
10#[derive(Debug, Clone, Serialize, Deserialize)] 10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct Cfg { 11pub struct Cfg {
12 pub data: String, 12 pub data: String,
13 pub cache: String,
13 pub apis: Apis, 14 pub apis: Apis,
14} 15}
15 16
@@ -28,7 +29,8 @@ impl Cfg {
28 if err.kind() == std::io::ErrorKind::NotFound { 29 if err.kind() == std::io::ErrorKind::NotFound {
29 println!("No config file found, creating one"); 30 println!("No config file found, creating one");
30 let default_cfg = Cfg { 31 let default_cfg = Cfg {
31 data: String::from("./"), 32 data: String::from("~/.cache/modlist/"),
33 cache: String::from("~/.cache/modlist/cache"),
32 apis: Apis { 34 apis: Apis {
33 modrinth: String::from("https://api.modrinth.com/v2/"), 35 modrinth: String::from("https://api.modrinth.com/v2/"),
34 }, 36 },
diff --git a/src/files.rs b/src/files.rs
index 6160cb4..ecf9b52 100644
--- a/src/files.rs
+++ b/src/files.rs
@@ -2,7 +2,7 @@ use futures_util::StreamExt;
2use reqwest::Client; 2use reqwest::Client;
3use std::{ 3use std::{
4 collections::HashMap, 4 collections::HashMap,
5 fs::{read_dir, remove_file, rename, File}, 5 fs::{read_dir, remove_file, rename, File, copy},
6 io::Write, 6 io::Write,
7}; 7};
8 8
@@ -11,35 +11,60 @@ use crate::{
11 db::{mods_get_info, userlist_add_disabled_versions}, 11 db::{mods_get_info, userlist_add_disabled_versions},
12 error::{ErrorType, MLError, MLE}, 12 error::{ErrorType, MLError, MLE},
13 modrinth::Version, 13 modrinth::Version,
14 List, 14 List, cache::{get_cached_versions, copy_cached_version}, devdir,
15}; 15};
16 16
17pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<String> { 17pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<String> {
18
19 let mut cached = get_cached_versions(&devdir(&config.cache));
20
21 println!("{:#?}", cached);
22
18 let dl_path = String::from(&list.download_folder); 23 let dl_path = String::from(&list.download_folder);
19 24
20 println!(" └Download mods to {}", dl_path); 25 println!(" └Download mods to {}", dl_path);
21 26
22 for ver in versions { 27 for ver in versions {
23 let project_info = mods_get_info(config.clone(), &ver.project_id)?; 28 let project_info = mods_get_info(config.clone(), &ver.project_id)?;
24 print!("\t└({})Download version {}", project_info.title, ver.id); 29
25 //Force flush of stdout, else print! doesn't print instantly 30 //Check cache if already downloaded
26 std::io::stdout().flush().unwrap(); 31 let c = cached.remove(&ver.id);
27 let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); 32 if c.is_some() {
28 let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); 33 print!("\t└({})Get version {} from cache", project_info.title, ver.id);
29 let extension = match splitname.pop().ok_or("") { 34 //Force flush of stdout, else print! doesn't print instantly
30 Ok(e) => e, 35 std::io::stdout().flush().unwrap();
31 Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")), 36 copy_cached_version(&c.unwrap(), &dl_path);
32 }; 37 println!(" ✓");
33 let filename = format!( 38 } else {
34 "{}.mr.{}.{}.{}", 39 print!("\t└({})Download version {}", project_info.title, ver.id);
35 splitname.join("."), 40 //Force flush of stdout, else print! doesn't print instantly
36 ver.project_id, 41 std::io::stdout().flush().unwrap();
37 ver.id, 42 let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap();
38 extension 43 let mut splitname: Vec<&str> = primary_file.filename.split('.').collect();
39 ); 44 let extension = match splitname.pop().ok_or("") {
40 download_file(primary_file.url, list.clone().download_folder, filename).await?; 45 Ok(e) => e,
41 //tokio::time::sleep(std::time::Duration::new(3, 0)).await; 46 Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")),
42 println!(" ✓"); 47 };
48 let filename = format!(
49 "{}.mr.{}.{}.{}",
50 splitname.join("."),
51 ver.project_id,
52 ver.id,
53 extension
54 );
55 download_file(primary_file.url, list.clone().download_folder, filename.clone()).await?;
56 println!(" ✓");
57 //Copy file to cache
58 print!("\t └Copy to cache");
59 //Force flush of stdout, else print! doesn't print instantly
60 std::io::stdout().flush().unwrap();
61 let dl_path_file = format!("{}/{}", list.download_folder, filename);
62 let cache_path = format!("{}/{}", devdir(&config.clone().cache), filename);
63 // println!("{}:{}", dl_path_file, cache_path);
64 copy(dl_path_file, cache_path)?;
65 println!(" ✓");
66 }
67
43 } 68 }
44 69
45 Ok(dl_path) 70 Ok(dl_path)
diff --git a/src/lib.rs b/src/lib.rs
index 43f0fe7..390696c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -4,6 +4,7 @@ pub mod config;
4pub mod db; 4pub mod db;
5pub mod error; 5pub mod error;
6pub mod files; 6pub mod files;
7pub mod cache;
7 8
8use std::{fmt::Display, path::Path}; 9use std::{fmt::Display, path::Path};
9 10