Skip to content

Commit

Permalink
mowdwinth uwu
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Sep 17, 2023
1 parent ddfdb73 commit b99143a
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 134 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ pub enum FileSource {

Cached {
path: PathBuf,
filename: String,
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/sources/curserinth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ impl<'a> CurserinthAPI<'a> {
let cached_file_path = format!("{id}/{}/{}", version.id, file.filename);

if self.0.has_in_cache("curserinth", &cached_file_path) {
Ok(FileSource::Cached { path: self.0.get_cache("curserinth").unwrap().0.join(cached_file_path) })
Ok(FileSource::Cached {
path: self.0.get_cache("curserinth").unwrap().0.join(cached_file_path),
filename: file.filename,
})
} else {
Ok(FileSource::Download {
url: file.url,
Expand Down
5 changes: 4 additions & 1 deletion src/sources/fabric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ impl<'a> FabricAPI<'a> {
let cached_file_path = format!("fabric-server-{}-{installer}-{loader}.jar", self.0.mc_version());

if self.0.has_in_cache("fabric", &cached_file_path) {
Ok(FileSource::Cached { path: self.0.get_cache("fabric").unwrap().0.join(cached_file_path) })
Ok(FileSource::Cached {
path: self.0.get_cache("fabric").unwrap().0.join(cached_file_path),
filename: cached_file_path.clone(),

Check failure on line 43 in src/sources/fabric.rs

View workflow job for this annotation

GitHub Actions / clippy

borrow of moved value: `cached_file_path`

error[E0382]: borrow of moved value: `cached_file_path` --> src/sources/fabric.rs:43:27 | 38 | let cached_file_path = format!("fabric-server-{}-{installer}-{loader}.jar", self.0.mc_version()); | ---------------- move occurs because `cached_file_path` has type `std::string::String`, which does not implement the `Copy` trait ... 42 | path: self.0.get_cache("fabric").unwrap().0.join(cached_file_path), | ---------------- value moved here 43 | filename: cached_file_path.clone(), | ^^^^^^^^^^^^^^^^^^^^^^^^ value borrowed here after move
})
} else {
Ok(FileSource::Download {
url: format!(
Expand Down
3 changes: 2 additions & 1 deletion src/sources/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ impl<'a> GithubAPI<'a> {

if has_in_cache {
Ok(FileSource::Cached {
path: self.0.get_cache(CACHE_DIR).unwrap().0.join(cached_file_path)
path: self.0.get_cache(CACHE_DIR).unwrap().0.join(cached_file_path),
filename: asset.name,
})
} else {
Ok(FileSource::Download {
Expand Down
61 changes: 42 additions & 19 deletions src/sources/hangar.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
use std::collections::HashMap;

use anyhow::{anyhow, Result, Context};
use mcapi::hangar::{VersionsFilter, ProjectVersion, Platform};
use anyhow::{anyhow, Context, Result};
use mcapi::hangar::{Platform, ProjectVersion};

use crate::{App, FileSource, CacheStrategy};
use crate::{App, CacheStrategy, FileSource};

pub struct HangarAPI<'a>(&'a App);

impl<'a> HangarAPI<'a> {
pub async fn fetch_hangar_version(&self, id: &str, version: &str) -> Result<ProjectVersion> {
let filter = self.0.server.jar.get_hangar_versions_filter(&self.0.server.mc_version);
let filter = self
.0
.server
.jar
.get_hangar_versions_filter(&self.0.server.mc_version);

let version = if version == "latest" {
let versions = mcapi::hangar::fetch_project_versions(&self.0.http_client, id, Some(filter)).await?;

let versions =
mcapi::hangar::fetch_project_versions(&self.0.http_client, id, Some(filter))
.await?;

versions
.result
.iter()
.next()
.ok_or(anyhow!("No compatible versions for Hangar project '{id}'"))?
.clone()
} else if version.contains('$') {
let versions = mcapi::hangar::fetch_project_versions(&self.0.http_client, id, Some(filter)).await?;

let versions =
mcapi::hangar::fetch_project_versions(&self.0.http_client, id, Some(filter))
.await?;

let version = version
.replace("${mcver}", &self.0.mc_version())
.replace("${mcversion}", &self.0.mc_version());

versions
.result
.iter()
Expand All @@ -43,39 +51,54 @@ impl<'a> HangarAPI<'a> {
} else {
mcapi::hangar::fetch_project_version(&self.0.http_client, id, version).await?
};

Ok(version)
}

pub async fn resolve_source(&self, id: &str, version: &str) -> Result<FileSource> {
let version = self.fetch_hangar_version(id, version)
.await.context("Fetching project version")?;
let version = self
.fetch_hangar_version(id, version)
.await
.context("Fetching project version")?;

let download = version
.downloads
.get(&self.0.server.jar.get_hangar_platform().unwrap_or(Platform::Paper))
.get(
&self
.0
.server
.jar
.get_hangar_platform()
.unwrap_or(Platform::Paper),
)
.ok_or(anyhow!(
"Platform unsupported for Hangar project '{id}' version '{}'",
version.name
))?;

let cached_file_path = format!("{id}/{}/{}", version.name, download.get_file_info().name);

if self.0.has_in_cache("hangar", &cached_file_path) {
Ok(FileSource::Cached { path: self.0.get_cache("hangar").unwrap().0.join(cached_file_path) })
Ok(FileSource::Cached {
path: self.0.get_cache("hangar").unwrap().0.join(cached_file_path),
filename: download.get_file_info().name,
})
} else {
Ok(FileSource::Download {
url: download.get_url(),
filename: download.get_file_info().name,
cache: if let Some(cache) = self.0.get_cache("hangar") {
CacheStrategy::File { path: cache.0.join(cached_file_path) }
CacheStrategy::File {
path: cache.0.join(cached_file_path),
}
} else {
CacheStrategy::None
},
size: Some(download.get_file_info().size_bytes as i32),
hashes: HashMap::from([
("sha256".to_owned(), download.get_file_info().sha256_hash)
])
hashes: HashMap::from([(
"sha256".to_owned(),
download.get_file_info().sha256_hash,
)]),
})
}
}
Expand Down
25 changes: 15 additions & 10 deletions src/sources/jenkins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::HashMap;

use anyhow::{anyhow, Result};

use crate::{util::match_artifact_name, FileSource, CacheStrategy, App};
use crate::{util::match_artifact_name, App, CacheStrategy, FileSource};

//pub static API_MAGIC_JOB: &str = "/api/json?tree=url,name,builds[*[url,number,result,artifacts[relativePath,fileName]]]";
static API_MAGIC_JOB: &str = "/api/json?tree=builds[*[url,number,result]]";
Expand All @@ -32,24 +32,27 @@ pub async fn resolve_source(
let cached_file_path = format!("{folder}/{job}/{build_number}/{filename}");

if app.has_in_cache("jenkins", &cached_file_path) {
Ok(FileSource::Cached { path: app.get_cache("jenkins").unwrap().0.join(cached_file_path) })
Ok(FileSource::Cached {
path: app.get_cache("jenkins").unwrap().0.join(cached_file_path),
filename,
})
} else {
Ok(FileSource::Download {
url: format!("{build_url}artifact/{relative_path}"),
filename,
cache: if let Some(cache) = app.get_cache("jenkins") {
CacheStrategy::File { path: cache.0.join(cached_file_path) }
CacheStrategy::File {
path: cache.0.join(cached_file_path),
}
} else {
CacheStrategy::None
},
size: None,
hashes: if let Some(md5) = md5hash {
HashMap::from([
("md5".to_owned(), md5.clone())
])
HashMap::from([("md5".to_owned(), md5.clone())])
} else {
HashMap::new()
}
},
})
}
}
Expand Down Expand Up @@ -135,7 +138,9 @@ pub async fn get_jenkins_filename(
))?;

let md5hash = if let Some(serde_json::Value::Array(values)) = matched_build.get("fingerprint") {
values.iter().find(|v| v["fileName"].as_str().unwrap() == artifact["fileName"].as_str().unwrap())
values
.iter()
.find(|v| v["fileName"].as_str().unwrap() == artifact["fileName"].as_str().unwrap())
.map(|v| v["hash"].as_str().unwrap().to_owned())
} else {
None
Expand All @@ -146,7 +151,7 @@ pub async fn get_jenkins_filename(
artifact["fileName"].as_str().unwrap().to_owned(),
artifact["relativePath"].as_str().unwrap().to_owned(),
matched_build["number"].as_i64().unwrap(),
md5hash
md5hash,
))
}

Expand All @@ -157,7 +162,7 @@ pub async fn get_jenkins_download_url(
build: &str,
artifact: &str,
) -> Result<String> {
let (build_url, _, relative_path, _build_number) =
let (build_url, _, relative_path, _build_number, _md5) =
get_jenkins_filename(client, url, job, build, artifact).await?;

Ok(build_url + "artifact/" + &relative_path)
Expand Down
Loading

0 comments on commit b99143a

Please sign in to comment.