1
0

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

@@ -703,6 +703,7 @@ pub async fn upload_file(
loaders.clone(),
game_versions.clone(),
all_game_versions.clone(),
file_type,
)
.await?;

38
src/validate/datapack.rs Normal file
View File

@@ -0,0 +1,38 @@
use crate::validate::{
SupportedGameVersions, ValidationError, ValidationResult,
};
use std::io::Cursor;
use zip::ZipArchive;
pub struct DataPackValidator;
impl super::Validator for DataPackValidator {
fn get_file_extensions(&self) -> &[&str] {
&["zip"]
}
fn get_project_types(&self) -> &[&str] {
&["mod"]
}
fn get_supported_loaders(&self) -> &[&str] {
&["datapack"]
}
fn get_supported_game_versions(&self) -> SupportedGameVersions {
SupportedGameVersions::All
}
fn validate(
&self,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
archive.by_name("pack.mcmeta").map_err(|_| {
ValidationError::InvalidInput(
"No pack.mcmeta present for datapack file. Tip: Make sure pack.mcmeta is in the root directory of your datapack!".into(),
)
})?;
Ok(ValidationResult::Pass)
}
}

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)

View File

@@ -13,7 +13,7 @@ impl super::Validator for PackValidator {
}
fn get_project_types(&self) -> &[&str] {
&["resourcepack", "datapack"]
&["resourcepack"]
}
fn get_supported_loaders(&self) -> &[&str] {