summaryrefslogtreecommitdiff
path: root/src/files.rs
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2023-01-09 23:12:52 +0100
committerfxqnlr <[email protected]>2023-01-09 23:12:52 +0100
commit89193143f90e1b8cbb91445d14942fa39509acbb (patch)
tree4aeb60ae2aceffc2468589c615ead9dc7079a34d /src/files.rs
parent94d7656cce4ca751be545eeb2ff52bdea1f37fa0 (diff)
downloadmodlist-89193143f90e1b8cbb91445d14942fa39509acbb.tar
modlist-89193143f90e1b8cbb91445d14942fa39509acbb.tar.gz
modlist-89193143f90e1b8cbb91445d14942fa39509acbb.zip
implemented more Error (dumb)
Diffstat (limited to 'src/files.rs')
-rw-r--r--src/files.rs29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/files.rs b/src/files.rs
index a3f838a..998ed32 100644
--- a/src/files.rs
+++ b/src/files.rs
@@ -2,16 +2,19 @@ use std::{fs::{File, read_dir, remove_file, rename}, io::Write, collections::Has
2use futures_util::StreamExt; 2use futures_util::StreamExt;
3use reqwest::Client; 3use reqwest::Client;
4 4
5use crate::{List, modrinth::Version, db::userlist_add_disabled_versions, config::Cfg}; 5use crate::{List, modrinth::Version, db::userlist_add_disabled_versions, config::Cfg, error::{MLE, MLError, ErrorType}};
6 6
7pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { 7pub async fn download_versions(current_list: List, versions: Vec<Version>) -> MLE<String> {
8 8
9 let dl_path = String::from(&current_list.download_folder); 9 let dl_path = String::from(&current_list.download_folder);
10 10
11 for ver in versions { 11 for ver in versions {
12 let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); 12 let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap();
13 let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); 13 let mut splitname: Vec<&str> = primary_file.filename.split('.').collect();
14 let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?; 14 let extension = match splitname.pop().ok_or("") {
15 Ok(e) => e,
16 Err(..) => return Err(MLError::new(ErrorType::Other, "NO_FILE_EXTENSION")),
17 };
15 let filename = format!("{}.mr.{}.{}.{}", splitname.join("."), ver.project_id, ver.id, extension); 18 let filename = format!("{}.mr.{}.{}.{}", splitname.join("."), ver.project_id, ver.id, extension);
16 download_file(primary_file.url, current_list.clone().download_folder, filename).await?; 19 download_file(primary_file.url, current_list.clone().download_folder, filename).await?;
17 } 20 }
@@ -19,14 +22,14 @@ pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Re
19 Ok(dl_path) 22 Ok(dl_path)
20} 23}
21 24
22async fn download_file(url: String, path: String, name: String) -> Result<(), Box<dyn std::error::Error>> { 25async fn download_file(url: String, path: String, name: String) -> MLE<()> {
23 println!("Downloading {}", url); 26 println!("Downloading {}", url);
24 let dl_path_file = format!("{}/{}", path, name); 27 let dl_path_file = format!("{}/{}", path, name);
25 let res = Client::new() 28 let res = Client::new()
26 .get(String::from(&url)) 29 .get(String::from(&url))
27 .send() 30 .send()
28 .await?; 31 .await?;
29 32
30 // download chunks 33 // download chunks
31 let mut file = File::create(&dl_path_file)?; 34 let mut file = File::create(&dl_path_file)?;
32 let mut stream = res.bytes_stream(); 35 let mut stream = res.bytes_stream();
@@ -39,7 +42,7 @@ async fn download_file(url: String, path: String, name: String) -> Result<(), Bo
39 Ok(()) 42 Ok(())
40} 43}
41 44
42pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> Result<(), Box<dyn std::error::Error>> { 45pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> MLE<()> {
43 println!("Disabling version {} for mod {}", versionid, mod_id); 46 println!("Disabling version {} for mod {}", versionid, mod_id);
44 let file = get_file_path(current_list.clone(), String::from(&versionid))?; 47 let file = get_file_path(current_list.clone(), String::from(&versionid))?;
45 let disabled = format!("{}.disabled", file); 48 let disabled = format!("{}.disabled", file);
@@ -51,7 +54,7 @@ pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_i
51 Ok(()) 54 Ok(())
52} 55}
53 56
54pub fn delete_version(list: List, version: String) -> Result<(), Box<dyn std::error::Error>> { 57pub fn delete_version(list: List, version: String) -> MLE<()> {
55 let file = get_file_path(list, version)?; 58 let file = get_file_path(list, version)?;
56 59
57 remove_file(file)?; 60 remove_file(file)?;
@@ -59,19 +62,25 @@ pub fn delete_version(list: List, version: String) -> Result<(), Box<dyn std::er
59 Ok(()) 62 Ok(())
60} 63}
61 64
62pub fn get_file_path(list: List, versionid: String) -> Result<String, Box<dyn std::error::Error>> { 65pub fn get_file_path(list: List, versionid: String) -> MLE<String> {
63 let mut names: HashMap<String, String> = HashMap::new(); 66 let mut names: HashMap<String, String> = HashMap::new();
64 for file in read_dir(list.download_folder)? { 67 for file in read_dir(list.download_folder)? {
65 let path = file?.path(); 68 let path = file?.path();
66 if path.is_file() { 69 if path.is_file() {
67 let pathstr = path.to_str().ok_or("BAH")?; 70 let pathstr = match path.to_str().ok_or("") {
71 Ok(s) => s,
72 Err(..) => return Err(MLError::new(ErrorType::Other, "INVALID_PATH"))
73 };
68 let namesplit: Vec<&str> = pathstr.split('.').collect(); 74 let namesplit: Vec<&str> = pathstr.split('.').collect();
69 let ver_id = namesplit[namesplit.len() - 2]; 75 let ver_id = namesplit[namesplit.len() - 2];
70 names.insert(String::from(ver_id), String::from(pathstr)); 76 names.insert(String::from(ver_id), String::from(pathstr));
71 } 77 }
72 }; 78 };
73 79
74 let filename = names.get(&versionid).ok_or("VERSION_NOT_FOUND_IN_FILES")?; 80 let filename = match names.get(&versionid).ok_or("") {
81 Ok(n) => n,
82 Err(..) => return Err(MLError::new(ErrorType::ArgumentError, "VERSION_NOT_FOUND_IN_FILES"))
83 };
75 84
76 Ok(filename.to_owned()) 85 Ok(filename.to_owned())
77} 86}