Add deps list for override mods, fix version editing for packs (#363)

This commit is contained in:
Geometrically
2022-06-05 10:42:33 -07:00
committed by GitHub
parent f860f57363
commit e497af4c26
4 changed files with 59 additions and 32 deletions

View File

@@ -696,9 +696,13 @@ pub async fn upload_file(
) )
.await?; .await?;
if let ValidationResult::PassWithPackData(ref data) = validation_result { if let ValidationResult::PassWithPackDataAndFiles {
ref format,
ref files,
} = validation_result
{
if dependencies.is_empty() { if dependencies.is_empty() {
let hashes: Vec<Vec<u8>> = data let hashes: Vec<Vec<u8>> = format
.files .files
.iter() .iter()
.filter_map(|x| x.hashes.get(&PackFileHash::Sha1)) .filter_map(|x| x.hashes.get(&PackFileHash::Sha1))
@@ -716,7 +720,7 @@ pub async fn upload_file(
) )
.fetch_all(&mut *transaction).await?; .fetch_all(&mut *transaction).await?;
for file in &data.files { for file in &format.files {
if let Some(dep) = res.iter().find(|x| { if let Some(dep) = res.iter().find(|x| {
x.hash.as_deref() x.hash.as_deref()
== file == file
@@ -735,24 +739,30 @@ pub async fn upload_file(
}); });
} }
} }
} else { } else if let Some(first_download) = file.downloads.first() {
if let Some(first_download) = file.downloads.first() { dependencies.push(DependencyBuilder {
dependencies.push(DependencyBuilder { project_id: None,
project_id: None, version_id: None,
version_id: None, file_name: Some(
file_name: Some( first_download
first_download .rsplit('/')
.rsplit('/') .next()
.next() .unwrap_or(first_download)
.unwrap_or(first_download)
.to_string(),
),
dependency_type: DependencyType::Required
.to_string(), .to_string(),
}); ),
} dependency_type: DependencyType::Required.to_string(),
});
} }
} }
for file in files {
dependencies.push(DependencyBuilder {
project_id: None,
version_id: None,
file_name: Some(file.to_string()),
dependency_type: DependencyType::Required.to_string(),
});
}
} }
} }

View File

@@ -72,11 +72,12 @@ pub fn validate_deps(
.iter() .iter()
.duplicates_by(|x| { .duplicates_by(|x| {
format!( format!(
"{}-{}", "{}-{}-{}",
x.version_id x.version_id
.unwrap_or(crate::models::projects::VersionId(0)), .unwrap_or(crate::models::projects::VersionId(0)),
x.project_id x.project_id
.unwrap_or(crate::models::projects::ProjectId(0)) .unwrap_or(crate::models::projects::ProjectId(0)),
x.file_name.as_deref().unwrap_or_default()
) )
}) })
.next() .next()

View File

@@ -33,7 +33,10 @@ pub enum ValidationError {
#[derive(Eq, PartialEq)] #[derive(Eq, PartialEq)]
pub enum ValidationResult { pub enum ValidationResult {
/// File should be marked as primary with pack file data /// File should be marked as primary with pack file data
PassWithPackData(PackFormat), PassWithPackDataAndFiles {
format: PackFormat,
files: Vec<String>,
},
/// File should be marked as primary /// File should be marked as primary
Pass, Pass,
/// File should not be marked primary, the reason for which is inside the String /// File should not be marked primary, the reason for which is inside the String
@@ -43,7 +46,7 @@ pub enum ValidationResult {
impl ValidationResult { impl ValidationResult {
pub fn is_passed(&self) -> bool { pub fn is_passed(&self) -> bool {
match self { match self {
ValidationResult::PassWithPackData(_) => true, ValidationResult::PassWithPackDataAndFiles { .. } => true,
ValidationResult::Pass => true, ValidationResult::Pass => true,
ValidationResult::Warning(_) => false, ValidationResult::Warning(_) => false,
} }

View File

@@ -31,17 +31,19 @@ impl super::Validator for PackValidator {
&self, &self,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>, archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> { ) -> Result<ValidationResult, ValidationError> {
let mut file = let pack: PackFormat = {
archive.by_name("modrinth.index.json").map_err(|_| { let mut file =
ValidationError::InvalidInput( archive.by_name("modrinth.index.json").map_err(|_| {
"Pack manifest is missing.".into(), ValidationError::InvalidInput(
) "Pack manifest is missing.".into(),
})?; )
})?;
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents)?; file.read_to_string(&mut contents)?;
let pack: PackFormat = serde_json::from_str(&contents)?; serde_json::from_str(&contents)?
};
pack.validate().map_err(|err| { pack.validate().map_err(|err| {
ValidationError::InvalidInput( ValidationError::InvalidInput(
@@ -87,6 +89,17 @@ impl super::Validator for PackValidator {
}; };
} }
Ok(ValidationResult::PassWithPackData(pack)) Ok(ValidationResult::PassWithPackDataAndFiles {
format: pack,
files: archive
.file_names()
.filter(|x| {
x.starts_with("overrides/mods")
|| x.starts_with("client-overrides/mods")
|| x.starts_with("server-overrides/mods")
})
.flat_map(|x| x.rsplit('/').next().map(|x| x.to_string()))
.collect::<Vec<String>>(),
})
} }
} }