summaryrefslogtreecommitdiff
path: root/src/files.rs
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2023-04-23 18:24:32 +0200
committerfxqnlr <[email protected]>2023-04-23 18:24:32 +0200
commit416f4dc383ff5a1194da3a5532a8e159a4a1dac0 (patch)
treec3d00456cafa01bfbe620aed85b25caa60b4e29d /src/files.rs
parent4300ad2eb05dddfa4274e04b204f2ad28c87da05 (diff)
downloadmodlist-416f4dc383ff5a1194da3a5532a8e159a4a1dac0.tar
modlist-416f4dc383ff5a1194da3a5532a8e159a4a1dac0.tar.gz
modlist-416f4dc383ff5a1194da3a5532a8e159a4a1dac0.zip
added caching, better data location
Diffstat (limited to 'src/files.rs')
-rw-r--r--src/files.rs67
1 files changed, 46 insertions, 21 deletions
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)