You've already forked AstralRinth
forked from didirus/AstralRinth
Move validators to seperate thread, other fixes (#253)
* Move validators to seperate thread, other fixes * Update rust version in Dockerfile * Fix notifs not working * Fix pack validator not enforcing files
This commit is contained in:
@@ -1,19 +1,60 @@
|
||||
use crate::models::projects::SideType;
|
||||
use crate::validate::{SupportedGameVersions, ValidationError, ValidationResult};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::{Cursor, Read};
|
||||
use zip::ZipArchive;
|
||||
use validator::Validate;
|
||||
use crate::util::validate::validation_errors_to_string;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Validate)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PackFormat<'a> {
|
||||
pub game: &'a str,
|
||||
pub format_version: i32,
|
||||
#[validate(length(min = 3, max = 512))]
|
||||
pub version_id: &'a str,
|
||||
#[validate(length(min = 3, max = 512))]
|
||||
pub name: &'a str,
|
||||
#[validate(length(max = 2048))]
|
||||
pub summary: Option<&'a str>,
|
||||
#[validate]
|
||||
pub files: Vec<PackFile<'a>>,
|
||||
pub dependencies: std::collections::HashMap<PackDependency, &'a str>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Validate)]
|
||||
pub struct PackFile<'a> {
|
||||
pub path: &'a str,
|
||||
pub hashes: std::collections::HashMap<FileHash, &'a str>,
|
||||
pub env: std::collections::HashMap<EnvType, SideType>,
|
||||
#[validate(custom(function = "validate_download_url"))]
|
||||
pub downloads: Vec<&'a str>,
|
||||
}
|
||||
|
||||
fn validate_download_url(values: &Vec<&str>) -> Result<(), validator::ValidationError> {
|
||||
for value in values {
|
||||
if !validator::validate_url(*value) {
|
||||
return Err(validator::ValidationError::new("invalid URL"));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Eq, PartialEq, Hash)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum FileHash {
|
||||
Sha1,
|
||||
Sha512,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Eq, PartialEq, Hash)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum EnvType {
|
||||
Client,
|
||||
Server,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum PackDependency {
|
||||
@@ -60,7 +101,7 @@ impl super::Validator for PackValidator {
|
||||
|
||||
fn validate(
|
||||
&self,
|
||||
archive: &mut ZipArchive<Cursor<&[u8]>>,
|
||||
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
|
||||
) -> Result<ValidationResult, ValidationError> {
|
||||
let mut file = archive
|
||||
.by_name("index.json")
|
||||
@@ -71,6 +112,10 @@ impl super::Validator for PackValidator {
|
||||
|
||||
let pack: PackFormat = serde_json::from_str(&contents)?;
|
||||
|
||||
pack
|
||||
.validate()
|
||||
.map_err(|err| ValidationError::InvalidInputError(validation_errors_to_string(err, None).into()))?;
|
||||
|
||||
if pack.game != "minecraft" {
|
||||
return Err(ValidationError::InvalidInputError(
|
||||
format!("Game {0} does not exist!", pack.game).into(),
|
||||
|
||||
Reference in New Issue
Block a user