You've already forked AstralRinth
forked from didirus/AstralRinth
Add validator for file types + datapacks (#507)
* Add validator for file types + datapacks * Make it compile
This commit is contained in:
@@ -703,6 +703,7 @@ pub async fn upload_file(
|
|||||||
loaders.clone(),
|
loaders.clone(),
|
||||||
game_versions.clone(),
|
game_versions.clone(),
|
||||||
all_game_versions.clone(),
|
all_game_versions.clone(),
|
||||||
|
file_type,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
38
src/validate/datapack.rs
Normal file
38
src/validate/datapack.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
use crate::models::pack::PackFormat;
|
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::fabric::FabricValidator;
|
||||||
use crate::validate::forge::{ForgeValidator, LegacyForgeValidator};
|
use crate::validate::forge::{ForgeValidator, LegacyForgeValidator};
|
||||||
use crate::validate::liteloader::LiteLoaderValidator;
|
use crate::validate::liteloader::LiteLoaderValidator;
|
||||||
@@ -15,6 +16,7 @@ use std::io::Cursor;
|
|||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use zip::ZipArchive;
|
use zip::ZipArchive;
|
||||||
|
|
||||||
|
mod datapack;
|
||||||
mod fabric;
|
mod fabric;
|
||||||
mod forge;
|
mod forge;
|
||||||
mod liteloader;
|
mod liteloader;
|
||||||
@@ -80,7 +82,7 @@ pub trait Validator: Sync {
|
|||||||
) -> Result<ValidationResult, ValidationError>;
|
) -> Result<ValidationResult, ValidationError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALIDATORS: [&dyn Validator; 15] = [
|
static VALIDATORS: [&dyn Validator; 16] = [
|
||||||
&ModpackValidator,
|
&ModpackValidator,
|
||||||
&FabricValidator,
|
&FabricValidator,
|
||||||
&ForgeValidator,
|
&ForgeValidator,
|
||||||
@@ -96,21 +98,34 @@ static VALIDATORS: [&dyn Validator; 15] = [
|
|||||||
&CanvasShaderValidator,
|
&CanvasShaderValidator,
|
||||||
&ShaderValidator,
|
&ShaderValidator,
|
||||||
&CoreShaderValidator,
|
&CoreShaderValidator,
|
||||||
|
&DataPackValidator,
|
||||||
];
|
];
|
||||||
|
|
||||||
/// The return value is whether this file should be marked as primary or not, based on the analysis of the file
|
/// 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(
|
pub async fn validate_file(
|
||||||
data: bytes::Bytes,
|
data: bytes::Bytes,
|
||||||
file_extension: String,
|
file_extension: String,
|
||||||
project_type: String,
|
mut project_type: String,
|
||||||
loaders: Vec<Loader>,
|
mut loaders: Vec<Loader>,
|
||||||
game_versions: Vec<GameVersion>,
|
game_versions: Vec<GameVersion>,
|
||||||
all_game_versions: Vec<crate::database::models::categories::GameVersion>,
|
all_game_versions: Vec<crate::database::models::categories::GameVersion>,
|
||||||
|
file_type: Option<FileType>,
|
||||||
) -> Result<ValidationResult, ValidationError> {
|
) -> Result<ValidationResult, ValidationError> {
|
||||||
actix_web::web::block(move || {
|
actix_web::web::block(move || {
|
||||||
let reader = Cursor::new(data);
|
let reader = Cursor::new(data);
|
||||||
let mut zip = ZipArchive::new(reader)?;
|
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;
|
let mut visited = false;
|
||||||
for validator in &VALIDATORS {
|
for validator in &VALIDATORS {
|
||||||
if validator.get_project_types().contains(&&*project_type)
|
if validator.get_project_types().contains(&&*project_type)
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ impl super::Validator for PackValidator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_project_types(&self) -> &[&str] {
|
fn get_project_types(&self) -> &[&str] {
|
||||||
&["resourcepack", "datapack"]
|
&["resourcepack"]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_supported_loaders(&self) -> &[&str] {
|
fn get_supported_loaders(&self) -> &[&str] {
|
||||||
|
|||||||
Reference in New Issue
Block a user