Add mod URL download support

This commit is contained in:
Danielle Hutzley
2021-12-14 08:00:06 -08:00
parent fc076c2e54
commit c8c69b4425
2 changed files with 39 additions and 2 deletions

View File

@@ -59,6 +59,9 @@ pub enum ModpackError {
#[error("Error downloading file: {0}")]
FetchError(#[from] reqwest::Error),
#[error("Invalid modpack source: {0} (set the WHITELISTED_MODPACK_DOMAINS environment variable to override)")]
SourceWhitelistError(String),
}
type ModpackResult<T> = Result<T, ModpackError>;

View File

@@ -10,10 +10,18 @@ use tokio::fs;
use super::{
modrinth_api::{self, ModrinthV1},
ModpackResult,
ModpackResult, ModpackError,
};
use crate::launcher::ModLoader;
pub const MODRINTH_DEFAULT_MODPACK_DOMAINS: &'static [&'static str] = &[
"cdn.modrinth.com",
"edge.forgecdn.net",
"github.com",
"raw.githubusercontent.com",
];
pub const MODRINTH_MODPACK_DOMAIN_WHITELIST_VAR: &'static str = "WHITELISTED_MODPACK_DOMAINS";
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct Modpack {
pub game: ModpackGame,
@@ -95,6 +103,32 @@ impl Modpack {
self.files.extend(files);
Ok(())
}
pub async fn add_file(&mut self, source: reqwest::Url, dest: &Path, hashes: Option<ModpackFileHashes>, env: Option<ModpackEnv>) -> ModpackResult<()> {
let whitelisted_domains = std::env::var(MODRINTH_MODPACK_DOMAIN_WHITELIST_VAR)
.map(|it| serde_json::from_str::<Vec<String>>(&it).ok().unwrap())
.unwrap_or(
MODRINTH_DEFAULT_MODPACK_DOMAINS
.iter()
.cloned()
.map(String::from)
.collect::<Vec<String>>(),
);
if (whitelisted_domains.iter().find(String::from(source.host_str().unwrap())).is_none()) {
return Err(ModpackError::SourceWhitelistError(String::from(source.host_str().unwrap())));
}
let file = ModpackFile {
path: dest,
hashes,
env: env.unwrap_or(ModpackEnv::Both),
downloads: HashSet::from([String::from(source)])
};
self.files.insert(file);
Ok(())
}
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
@@ -107,7 +141,7 @@ pub enum ModpackGame {
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct ModpackFile {
pub path: PathBuf,
pub hashes: ModpackFileHashes,
pub hashes: Option<ModpackFileHashes>,
pub env: ModpackEnv,
pub downloads: HashSet<String>,
}