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/commands/download.rs | 69 ++++++++++++++++++++++++------------------------ src/commands/update.rs | 41 +++++++++++----------------- src/files.rs | 31 ++++++++++++++-------- 3 files changed, 71 insertions(+), 70 deletions(-) (limited to 'src') diff --git a/src/commands/download.rs b/src/commands/download.rs index 421f058..9f70499 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,50 +1,51 @@ -use crate::{files::get_downloaded_versions, db::{userlist_get_all_applicable_versions_with_mods, userlist_get_all_current_versions_with_mods}}; +use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version}, db::userlist_get_all_current_versions_with_mods, modrinth::get_raw_versions}; #[allow(unused_imports)] use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input}; -pub async fn download(config: Cfg, _input: Input) -> Result<(), Box> { +pub async fn download(config: Cfg, input: Input) -> Result<(), Box> { let current_list = get_current_list(config.clone())?; - println!("NO IMPLEMENTATION FOR DOWNLOAD YET"); - let downloaded_versions = get_downloaded_versions(current_list.clone())?; - println!("DL: {:?}", downloaded_versions); - let current_version_ids = userlist_get_all_current_versions_with_mods(config.clone(), String::from(¤t_list.id))?; - println!("CU: {:?}", current_version_ids); - let applicable_version_ids = userlist_get_all_applicable_versions_with_mods(config, current_list.id)?; - println!("AP: {:?}", applicable_version_ids); - - /* - let list = get_current_list(config.clone())?; + let mut to_download: Vec = vec![]; + //(mod_id, version_id) + let mut to_disable: Vec<(String, String)> = vec![]; - let links = userlist_get_all_downloads(config.clone(), list.clone().id)?; + for version in current_version_ids { + let mod_id = version.0; + let current_version = version.1; - download_links(config, input, list, links).await?; - */ - Ok(()) -} + let current_download = downloaded_versions.get(&mod_id); -#[allow(dead_code)] -async fn download_links(_config: Cfg, _input: Input, _current_list: List, _links: Vec) -> Result> { - println!("NO DL IMPLEMENTATION FOR DOWNLOAD YET"); - //TODO copy dl from update if possible - /* - let dl_path = String::from(¤t_list.download_folder); - - if input.clean { - let dl_path = ¤t_list.download_folder; - println!("Cleaning {}", dl_path); - for entry in std::fs::read_dir(dl_path)? { - let entry = entry?; - std::fs::remove_file(entry.path())?; + if current_download.is_none() { + to_download.push(current_version); + } else { + let downloaded_version = current_download.ok_or("SOMETHING_HAS_REALLY_GONE_WRONG")?; + if ¤t_version != downloaded_version { + to_disable.push((mod_id.clone(), String::from(downloaded_version))); + to_download.push(current_version); + } + } + } + + if !to_download.is_empty() { + download_versions(current_list.clone(), get_raw_versions(String::from(&config.apis.modrinth), to_download).await).await?; + } else { + println!("There are no new versions to download"); + } + + if !to_disable.is_empty() { + for ver in to_disable { + if input.delete_old { + println!("Deleting version {} for mod {}", ver.1, ver.0); + delete_version(current_list.clone(), ver.1)?; + } else { + disable_version(config.clone(), current_list.clone(), ver.1, ver.0)?; + }; } } - */ - Ok(String::new()) + Ok(()) } - - diff --git a/src/commands/update.rs b/src/commands/update.rs index eeb5a4d..498b6a9 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -1,6 +1,6 @@ -use std::{io::{Error, ErrorKind}, fs::rename}; +use std::io::{Error, ErrorKind}; -use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, userlist_add_disabled_versions, mods_change_versions}, List, input::Input, files::{get_file_path, delete_version, download_versions}}; +use crate::{config::Cfg, modrinth::{projects, Project, versions, extract_current_version, Version}, get_current_list, db::{userlist_get_all_ids, mods_get_versions, userlist_get_applicable_versions, userlist_change_versions, lists_get_all_ids, lists_get, userlist_get_current_version, mods_change_versions}, List, input::Input, files::{delete_version, download_versions, disable_version}}; pub async fn update(config: Cfg, input: Input) -> Result<(), Box> { @@ -69,18 +69,21 @@ pub async fn update(config: Cfg, input: Input) -> Result<(), Box 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(()) -} - #[tokio::test] async fn download_updates_test() { 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