From 1c8964441432047eaa2e866666c4055b35b55253 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Fri, 9 Dec 2022 20:30:10 +0100 Subject: added download with delete; new file format --- src/files.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'src/files.rs') 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 @@ -use std::{fs::{File, read_dir, remove_file}, io::Write, collections::HashMap}; +use std::{fs::{File, read_dir, remove_file, rename}, io::Write, collections::HashMap}; use futures_util::StreamExt; use reqwest::Client; -use crate::{List, modrinth::Version}; +use crate::{List, modrinth::Version, db::userlist_add_disabled_versions, config::Cfg}; pub async fn download_versions(current_list: List, versions: Vec) -> Result> { @@ -12,7 +12,7 @@ pub async fn download_versions(current_list: List, versions: Vec) -> Re let primary_file = ver.files.into_iter().find(|file| file.primary).unwrap(); let mut splitname: Vec<&str> = primary_file.filename.split('.').collect(); let extension = splitname.pop().ok_or("NO_FILE_EXTENSION")?; - let filename = format!("{}.mr{}.{}", splitname.join("."), ver.id, extension); + let filename = format!("{}.mr.{}.{}.{}", splitname.join("."), ver.project_id, ver.id, extension); download_file(primary_file.url, current_list.clone().download_folder, filename).await?; } @@ -39,6 +39,18 @@ async fn download_file(url: String, path: String, name: String) -> Result<(), Bo Ok(()) } +pub fn disable_version(config: Cfg, current_list: List, versionid: String, mod_id: String) -> Result<(), Box> { + println!("Disabling version {} for mod {}", versionid, mod_id); + let file = get_file_path(current_list.clone(), String::from(&versionid))?; + let disabled = format!("{}.disabled", file); + + rename(file, disabled)?; + + userlist_add_disabled_versions(config, current_list.id, versionid, mod_id)?; + + Ok(()) +} + pub fn delete_version(list: List, version: String) -> Result<(), Box> { let file = get_file_path(list, version)?; @@ -58,24 +70,21 @@ pub fn get_file_path(list: List, versionid: String) -> Result Result, Box> { - let mut versions: Vec = vec![]; +pub fn get_downloaded_versions(list: List) -> Result, Box> { + let mut versions: HashMap = HashMap::new(); for file in read_dir(list.download_folder)? { let path = file?.path(); if path.is_file() && path.extension().ok_or("BAH")? == "jar" { let pathstr = path.to_str().ok_or("BAH")?; let namesplit: Vec<&str> = pathstr.split('.').collect(); - versions.push(String::from(namesplit[namesplit.len() - 2].split_at(2).1)); + versions.insert(String::from(namesplit[namesplit.len() - 3]), String::from(namesplit[namesplit.len() - 2])); } } - Ok(versions) } -- cgit v1.2.3