diff options
author | fxqnlr <[email protected]> | 2023-04-23 18:24:32 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2023-04-23 18:24:32 +0200 |
commit | 416f4dc383ff5a1194da3a5532a8e159a4a1dac0 (patch) | |
tree | c3d00456cafa01bfbe620aed85b25caa60b4e29d | |
parent | 4300ad2eb05dddfa4274e04b204f2ad28c87da05 (diff) | |
download | modlist-416f4dc383ff5a1194da3a5532a8e159a4a1dac0.tar modlist-416f4dc383ff5a1194da3a5532a8e159a4a1dac0.tar.gz modlist-416f4dc383ff5a1194da3a5532a8e159a4a1dac0.zip |
added caching, better data location
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | cache | bin | 0 -> 435482 bytes | |||
-rw-r--r-- | src/cache.rs | 36 | ||||
-rw-r--r-- | src/config.rs | 4 | ||||
-rw-r--r-- | src/files.rs | 67 | ||||
-rw-r--r-- | src/lib.rs | 1 |
6 files changed, 87 insertions, 23 deletions
@@ -1,6 +1,6 @@ | |||
1 | [package] | 1 | [package] |
2 | name = "modlist" | 2 | name = "modlist" |
3 | version = "0.12.0" | 3 | version = "0.13.0" |
4 | edition = "2021" | 4 | edition = "2021" |
5 | 5 | ||
6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
Binary files differ | |||
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 @@ | |||
1 | use std::{collections::HashMap, fs::{read_dir, copy}}; | ||
2 | |||
3 | use crate::devdir; | ||
4 | |||
5 | /// . | ||
6 | /// | ||
7 | /// # Panics | ||
8 | /// | ||
9 | /// Panics if . | ||
10 | pub 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 . | ||
31 | pub 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)] |
11 | pub struct Cfg { | 11 | pub 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; | |||
2 | use reqwest::Client; | 2 | use reqwest::Client; |
3 | use std::{ | 3 | use 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 | ||
17 | pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<String> { | 17 | pub 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) |
@@ -4,6 +4,7 @@ pub mod config; | |||
4 | pub mod db; | 4 | pub mod db; |
5 | pub mod error; | 5 | pub mod error; |
6 | pub mod files; | 6 | pub mod files; |
7 | pub mod cache; | ||
7 | 8 | ||
8 | use std::{fmt::Display, path::Path}; | 9 | use std::{fmt::Display, path::Path}; |
9 | 10 | ||