diff options
author | fxqnlr <[email protected]> | 2022-12-09 20:30:10 +0100 |
---|---|---|
committer | fxqnlr <[email protected]> | 2022-12-09 20:30:10 +0100 |
commit | 1c8964441432047eaa2e866666c4055b35b55253 (patch) | |
tree | a14da6da0410a73defa724bac832c7b7f147dcab /src/files.rs | |
parent | a9b5f7be4e98ecaa016382d3024270192ee586b0 (diff) | |
download | modlist-1c8964441432047eaa2e866666c4055b35b55253.tar modlist-1c8964441432047eaa2e866666c4055b35b55253.tar.gz modlist-1c8964441432047eaa2e866666c4055b35b55253.zip |
added download with delete; new file format
Diffstat (limited to 'src/files.rs')
-rw-r--r-- | src/files.rs | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/src/files.rs b/src/files.rs index 959da75..b1e537c 100644 --- a/src/files.rs +++ b/src/files.rs | |||
@@ -1,8 +1,8 @@ | |||
1 | use std::{fs::{File, read_dir, remove_file}, io::Write, collections::HashMap}; | 1 | use std::{fs::{File, read_dir, remove_file, rename}, io::Write, collections::HashMap}; |
2 | use futures_util::StreamExt; | 2 | use futures_util::StreamExt; |
3 | use reqwest::Client; | 3 | use reqwest::Client; |
4 | 4 | ||
5 | use crate::{List, modrinth::Version}; | 5 | use crate::{List, modrinth::Version, db::userlist_add_disabled_versions, config::Cfg}; |
6 | 6 | ||
7 | pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { | 7 | pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Result<String, Box<dyn std::error::Error>> { |
8 | 8 | ||
@@ -12,7 +12,7 @@ pub async fn download_versions(current_list: List, versions: Vec<Version>) -> Re | |||
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 = splitname.pop().ok_or("NO_FILE_EXTENSION")?; |
15 | let filename = format!("{}.mr{}.{}", splitname.join("."), ver.id, extension); | 15 | 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?; | 16 | download_file(primary_file.url, current_list.clone().download_folder, filename).await?; |
17 | } | 17 | } |
18 | 18 | ||
@@ -39,6 +39,18 @@ async fn download_file(url: String, path: String, name: String) -> Result<(), Bo | |||
39 | Ok(()) | 39 | Ok(()) |
40 | } | 40 | } |
41 | 41 | ||
42 | pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> Result<(), Box<dyn std::error::Error>> { | ||
43 | println!("Disabling version {} for mod {}", versionid, mod_id); | ||
44 | let file = get_file_path(current_list.clone(), String::from(&versionid))?; | ||
45 | let disabled = format!("{}.disabled", file); | ||
46 | |||
47 | rename(file, disabled)?; | ||
48 | |||
49 | userlist_add_disabled_versions(config, current_list.id, versionid, mod_id)?; | ||
50 | |||
51 | Ok(()) | ||
52 | } | ||
53 | |||
42 | pub fn delete_version(list: List, version: String) -> Result<(), Box<dyn std::error::Error>> { | 54 | pub fn delete_version(list: List, version: String) -> Result<(), Box<dyn std::error::Error>> { |
43 | let file = get_file_path(list, version)?; | 55 | let file = get_file_path(list, version)?; |
44 | 56 | ||
@@ -58,24 +70,21 @@ pub fn get_file_path(list: List, versionid: String) -> Result<String, Box<dyn st | |||
58 | names.insert(String::from(ver_id), String::from(pathstr)); | 70 | names.insert(String::from(ver_id), String::from(pathstr)); |
59 | } | 71 | } |
60 | }; | 72 | }; |
61 | 73 | ||
62 | let api_versionid = format!("mr{}", versionid); | 74 | let filename = names.get(&versionid).ok_or("VERSION_NOT_FOUND_IN_FILES")?; |
63 | |||
64 | let filename = names.get(&api_versionid).ok_or("VERSION_NOT_FOUND_IN_FILES")?; | ||
65 | 75 | ||
66 | Ok(filename.to_owned()) | 76 | Ok(filename.to_owned()) |
67 | } | 77 | } |
68 | 78 | ||
69 | pub fn get_downloaded_versions(list: List) -> Result<Vec<String>, Box<dyn std::error::Error>> { | 79 | pub fn get_downloaded_versions(list: List) -> Result<HashMap<String, String>, Box<dyn std::error::Error>> { |
70 | let mut versions: Vec<String> = vec![]; | 80 | let mut versions: HashMap<String, String> = HashMap::new(); |
71 | for file in read_dir(list.download_folder)? { | 81 | for file in read_dir(list.download_folder)? { |
72 | let path = file?.path(); | 82 | let path = file?.path(); |
73 | if path.is_file() && path.extension().ok_or("BAH")? == "jar" { | 83 | if path.is_file() && path.extension().ok_or("BAH")? == "jar" { |
74 | let pathstr = path.to_str().ok_or("BAH")?; | 84 | let pathstr = path.to_str().ok_or("BAH")?; |
75 | let namesplit: Vec<&str> = pathstr.split('.').collect(); | 85 | let namesplit: Vec<&str> = pathstr.split('.').collect(); |
76 | versions.push(String::from(namesplit[namesplit.len() - 2].split_at(2).1)); | 86 | versions.insert(String::from(namesplit[namesplit.len() - 3]), String::from(namesplit[namesplit.len() - 2])); |
77 | } | 87 | } |
78 | } | 88 | } |
79 | |||
80 | Ok(versions) | 89 | Ok(versions) |
81 | } | 90 | } |