summaryrefslogtreecommitdiff
path: root/src/files.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-04-27 10:10:03 +0200
committerfx <[email protected]>2023-04-27 10:10:03 +0200
commit43ca5fec20933fc31dfe7d7dbd1f1b9258612219 (patch)
treeeef58fecfadad90386b38af581abab8cc3d3cfc0 /src/files.rs
parent4300ad2eb05dddfa4274e04b204f2ad28c87da05 (diff)
parent4e6466af1329f7b9e341df2e76ab696d11f80c93 (diff)
downloadmodlist-43ca5fec20933fc31dfe7d7dbd1f1b9258612219.tar
modlist-43ca5fec20933fc31dfe7d7dbd1f1b9258612219.tar.gz
modlist-43ca5fec20933fc31dfe7d7dbd1f1b9258612219.zip
Merge pull request 'cache' (#3) from cache into master
Reviewed-on: http://raspberrypi.fritz.box:7920/fx/modlist/pulls/3
Diffstat (limited to 'src/files.rs')
-rw-r--r--src/files.rs72
1 files changed, 52 insertions, 20 deletions
diff --git a/src/files.rs b/src/files.rs
index 6160cb4..59fc7de 100644
--- a/src/files.rs
+++ b/src/files.rs
@@ -2,11 +2,12 @@ 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::{copy, read_dir, remove_file, rename, File},
6 io::Write, 6 io::Write,
7}; 7};
8 8
9use crate::{ 9use crate::{
10 cache::{copy_cached_version, get_cached_versions},
10 config::Cfg, 11 config::Cfg,
11 db::{mods_get_info, userlist_add_disabled_versions}, 12 db::{mods_get_info, userlist_add_disabled_versions},
12 error::{ErrorType, MLError, MLE}, 13 error::{ErrorType, MLError, MLE},
@@ -15,31 +16,62 @@ use crate::{
15}; 16};
16 17
17pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<String> { 18pub async fn download_versions(list: List, config: Cfg, versions: Vec<Version>) -> MLE<String> {
19 let mut cached = get_cached_versions(&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!(
29 let extension = match splitname.pop().ok_or("") { 34 "\t└({})Get version {} from cache",
30 Ok(e) => e, 35 project_info.title, ver.id
31 Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")), 36 );
32 }; 37 //Force flush of stdout, else print! doesn't print instantly
33 let filename = format!( 38 std::io::stdout().flush()?;
34 "{}.mr.{}.{}.{}", 39 copy_cached_version(&c.unwrap(), &dl_path);
35 splitname.join("."), 40 println!(" ✓");
36 ver.project_id, 41 } else {
37 ver.id, 42 print!("\t└({})Download version {}", project_info.title, ver.id);
38 extension 43 //Force flush of stdout, else print! doesn't print instantly
39 ); 44 std::io::stdout().flush().unwrap();
40 download_file(primary_file.url, list.clone().download_folder, filename).await?; 45 let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap();
41 //tokio::time::sleep(std::time::Duration::new(3, 0)).await; 46 let mut splitname: Vec<&str> = primary_file.filename.split('.').collect();
42 println!(" ✓"); 47 let extension = match splitname.pop().ok_or("") {
48 Ok(e) => e,
49 Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")),
50 };
51 let filename = format!(
52 "{}.mr.{}.{}.{}",
53 splitname.join("."),
54 ver.project_id,
55 ver.id,
56 extension
57 );
58 download_file(
59 primary_file.url,
60 list.clone().download_folder,
61 filename.clone(),
62 )
63 .await?;
64 println!(" ✓");
65 //Copy file to cache
66 print!("\t └Copy to cache");
67 //Force flush of stdout, else print! doesn't print instantly
68 std::io::stdout().flush().unwrap();
69 let dl_path_file = format!("{}/{}", list.download_folder, filename);
70 let cache_path = format!("{}/{}", &config.clone().cache, filename);
71 // println!("{}:{}", dl_path_file, cache_path);
72 copy(dl_path_file, cache_path)?;
73 println!(" ✓");
74 }
43 } 75 }
44 76
45 Ok(dl_path) 77 Ok(dl_path)