Add validator for file types + datapacks (#507)

* Add validator for file types + datapacks

* Make it compile
This commit is contained in:
Geometrically
2022-12-23 19:49:47 -07:00
committed by GitHub
parent fe256d6a62
commit 5da42575fd
4 changed files with 59 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
use crate::models::pack::PackFormat;
use crate::models::projects::{GameVersion, Loader};
use crate::models::projects::{FileType, GameVersion, Loader};
use crate::validate::datapack::DataPackValidator;
use crate::validate::fabric::FabricValidator;
use crate::validate::forge::{ForgeValidator, LegacyForgeValidator};
use crate::validate::liteloader::LiteLoaderValidator;
@@ -15,6 +16,7 @@ use std::io::Cursor;
use thiserror::Error;
use zip::ZipArchive;
mod datapack;
mod fabric;
mod forge;
mod liteloader;
@@ -80,7 +82,7 @@ pub trait Validator: Sync {
) -> Result<ValidationResult, ValidationError>;
}
static VALIDATORS: [&dyn Validator; 15] = [
static VALIDATORS: [&dyn Validator; 16] = [
&ModpackValidator,
&FabricValidator,
&ForgeValidator,
@@ -96,21 +98,34 @@ static VALIDATORS: [&dyn Validator; 15] = [
&CanvasShaderValidator,
&ShaderValidator,
&CoreShaderValidator,
&DataPackValidator,
];
/// The return value is whether this file should be marked as primary or not, based on the analysis of the file
pub async fn validate_file(
data: bytes::Bytes,
file_extension: String,
project_type: String,
loaders: Vec<Loader>,
mut project_type: String,
mut loaders: Vec<Loader>,
game_versions: Vec<GameVersion>,
all_game_versions: Vec<crate::database::models::categories::GameVersion>,
file_type: Option<FileType>,
) -> Result<ValidationResult, ValidationError> {
actix_web::web::block(move || {
let reader = Cursor::new(data);
let mut zip = ZipArchive::new(reader)?;
if let Some(file_type) = file_type {
match file_type {
FileType::RequiredResourcePack
| FileType::OptionalResourcePack => {
project_type = "resourcepack".to_string();
loaders = vec![Loader("minecraft".to_string())];
}
FileType::Unknown => {}
}
}
let mut visited = false;
for validator in &VALIDATORS {
if validator.get_project_types().contains(&&*project_type)