You've already forked AstralRinth
forked from didirus/AstralRinth
More project type validators (#383)
This commit is contained in:
@@ -679,7 +679,7 @@ impl Project {
|
||||
.donations
|
||||
.unwrap_or_default()
|
||||
.split(" ~~~~ ")
|
||||
.map(|d| {
|
||||
.flat_map(|d| {
|
||||
let strings: Vec<&str> = d.split(" |||| ").collect();
|
||||
|
||||
if strings.len() >= 3 {
|
||||
@@ -696,13 +696,12 @@ impl Project {
|
||||
None
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.collect(),
|
||||
gallery_items: m
|
||||
.gallery
|
||||
.unwrap_or_default()
|
||||
.split(" ~~~~ ")
|
||||
.map(|d| {
|
||||
.flat_map(|d| {
|
||||
let strings: Vec<&str> = d.split(" |||| ").collect();
|
||||
|
||||
if strings.len() >= 5 {
|
||||
@@ -726,7 +725,6 @@ impl Project {
|
||||
None
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.collect(),
|
||||
status: crate::models::projects::ProjectStatus::from_str(
|
||||
&m.status_name,
|
||||
@@ -822,7 +820,7 @@ impl Project {
|
||||
.gallery
|
||||
.unwrap_or_default()
|
||||
.split(" ~~~~ ")
|
||||
.map(|d| {
|
||||
.flat_map(|d| {
|
||||
let strings: Vec<&str> = d.split(" |||| ").collect();
|
||||
|
||||
if strings.len() >= 5 {
|
||||
@@ -838,13 +836,12 @@ impl Project {
|
||||
None
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.collect(),
|
||||
donation_urls: m
|
||||
.donations
|
||||
.unwrap_or_default()
|
||||
.split(" ~~~~ ")
|
||||
.map(|d| {
|
||||
.flat_map(|d| {
|
||||
let strings: Vec<&str> = d.split(" |||| ").collect();
|
||||
|
||||
if strings.len() >= 3 {
|
||||
@@ -859,7 +856,6 @@ impl Project {
|
||||
None
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.collect(),
|
||||
status: crate::models::projects::ProjectStatus::from_str(&m.status_name),
|
||||
license_id: m.short,
|
||||
|
||||
@@ -650,7 +650,7 @@ impl Version {
|
||||
.hashes
|
||||
.unwrap_or_default()
|
||||
.split(" ~~~~ ")
|
||||
.map(|f| {
|
||||
.flat_map(|f| {
|
||||
let hash: Vec<&str> = f.split(" |||| ").collect();
|
||||
|
||||
if hash.len() >= 3 {
|
||||
@@ -663,13 +663,12 @@ impl Version {
|
||||
None
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.collect();
|
||||
|
||||
v.files
|
||||
.unwrap_or_default()
|
||||
.split(" ~~~~ ")
|
||||
.map(|f| {
|
||||
.flat_map(|f| {
|
||||
let file: Vec<&str> = f.split(" |||| ").collect();
|
||||
|
||||
if file.len() >= 5 {
|
||||
@@ -698,7 +697,6 @@ impl Version {
|
||||
None
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.collect()
|
||||
},
|
||||
game_versions: {
|
||||
@@ -737,7 +735,7 @@ impl Version {
|
||||
.dependencies
|
||||
.unwrap_or_default()
|
||||
.split(" ~~~~ ")
|
||||
.map(|f| {
|
||||
.flat_map(|f| {
|
||||
let dependency: Vec<&str> = f.split(" |||| ").collect();
|
||||
|
||||
if dependency.len() >= 4 {
|
||||
@@ -767,7 +765,6 @@ impl Version {
|
||||
None
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.collect(),
|
||||
version_type: v.version_type,
|
||||
}))
|
||||
@@ -824,7 +821,7 @@ impl Version {
|
||||
date_published: v.date_published,
|
||||
downloads: v.downloads,
|
||||
files: {
|
||||
let hashes: Vec<(FileId, String, Vec<u8>)> = v.hashes.unwrap_or_default().split(" ~~~~ ").map(|f| {
|
||||
let hashes: Vec<(FileId, String, Vec<u8>)> = v.hashes.unwrap_or_default().split(" ~~~~ ").flat_map(|f| {
|
||||
let hash: Vec<&str> = f.split(" |||| ").collect();
|
||||
|
||||
if hash.len() >= 3 {
|
||||
@@ -836,9 +833,9 @@ impl Version {
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}).flatten().collect();
|
||||
}).collect();
|
||||
|
||||
v.files.unwrap_or_default().split(" ~~~~ ").map(|f| {
|
||||
v.files.unwrap_or_default().split(" ~~~~ ").flat_map(|f| {
|
||||
let file: Vec<&str> = f.split(" |||| ").collect();
|
||||
|
||||
if file.len() >= 5 {
|
||||
@@ -862,7 +859,7 @@ impl Version {
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}).flatten().collect()
|
||||
}).collect()
|
||||
},
|
||||
game_versions: {
|
||||
let game_versions = v
|
||||
@@ -893,7 +890,7 @@ impl Version {
|
||||
dependencies: v.dependencies
|
||||
.unwrap_or_default()
|
||||
.split(" ~~~~ ")
|
||||
.map(|f| {
|
||||
.flat_map(|f| {
|
||||
let dependency: Vec<&str> = f.split(" |||| ").collect();
|
||||
|
||||
if dependency.len() >= 4 {
|
||||
@@ -922,7 +919,7 @@ impl Version {
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}).flatten().collect(),
|
||||
}).collect(),
|
||||
version_type: v.version_type
|
||||
}
|
||||
))
|
||||
|
||||
@@ -27,7 +27,7 @@ impl DownloadQueue {
|
||||
pub async fn index(&self, pool: &PgPool) -> Result<(), DatabaseError> {
|
||||
let queue = self.take().await;
|
||||
|
||||
if queue.len() > 0 {
|
||||
if !queue.is_empty() {
|
||||
let mut transaction = pool.begin().await?;
|
||||
|
||||
for (project_id, version_id) in queue {
|
||||
|
||||
@@ -89,7 +89,7 @@ where
|
||||
.to_str()
|
||||
.map_err(|_| AuthenticationError::InvalidCredentials)?;
|
||||
|
||||
Ok(get_user_from_token(token, executor).await?)
|
||||
get_user_from_token(token, executor).await
|
||||
}
|
||||
|
||||
pub async fn check_is_moderator_from_headers<'a, 'b, E>(
|
||||
|
||||
@@ -4,7 +4,9 @@ use crate::validate::fabric::FabricValidator;
|
||||
use crate::validate::forge::{ForgeValidator, LegacyForgeValidator};
|
||||
use crate::validate::liteloader::LiteLoaderValidator;
|
||||
use crate::validate::pack::PackValidator;
|
||||
use crate::validate::plugin::PluginValidator;
|
||||
use crate::validate::quilt::QuiltValidator;
|
||||
use crate::validate::resourcepack::ResourcePackValidator;
|
||||
use std::io::Cursor;
|
||||
use thiserror::Error;
|
||||
use time::OffsetDateTime;
|
||||
@@ -14,7 +16,9 @@ mod fabric;
|
||||
mod forge;
|
||||
mod liteloader;
|
||||
mod pack;
|
||||
mod plugin;
|
||||
mod quilt;
|
||||
mod resourcepack;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ValidationError {
|
||||
@@ -22,7 +26,7 @@ pub enum ValidationError {
|
||||
Zip(#[from] zip::result::ZipError),
|
||||
#[error("IO Error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("Error while validating JSON: {0}")]
|
||||
#[error("Error while validating JSON for uploaded file: {0}")]
|
||||
SerDe(#[from] serde_json::Error),
|
||||
#[error("Invalid Input: {0}")]
|
||||
InvalidInput(std::borrow::Cow<'static, str>),
|
||||
@@ -72,13 +76,15 @@ pub trait Validator: Sync {
|
||||
) -> Result<ValidationResult, ValidationError>;
|
||||
}
|
||||
|
||||
static VALIDATORS: [&dyn Validator; 6] = [
|
||||
static VALIDATORS: [&dyn Validator; 8] = [
|
||||
&PackValidator,
|
||||
&FabricValidator,
|
||||
&ForgeValidator,
|
||||
&LegacyForgeValidator,
|
||||
&QuiltValidator,
|
||||
&LiteLoaderValidator,
|
||||
&ResourcePackValidator,
|
||||
&PluginValidator,
|
||||
];
|
||||
|
||||
/// The return value is whether this file should be marked as primary or not, based on the analysis of the file
|
||||
|
||||
38
src/validate/plugin.rs
Normal file
38
src/validate/plugin.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use crate::validate::{
|
||||
SupportedGameVersions, ValidationError, ValidationResult,
|
||||
};
|
||||
use std::io::Cursor;
|
||||
use zip::ZipArchive;
|
||||
|
||||
pub struct PluginValidator;
|
||||
|
||||
impl super::Validator for PluginValidator {
|
||||
fn get_file_extensions(&self) -> &[&str] {
|
||||
&["zip", "jar"]
|
||||
}
|
||||
|
||||
fn get_project_types(&self) -> &[&str] {
|
||||
&["mod"]
|
||||
}
|
||||
|
||||
fn get_supported_loaders(&self) -> &[&str] {
|
||||
&["bukkit", "spigot", "paper", "purpur"]
|
||||
}
|
||||
|
||||
fn get_supported_game_versions(&self) -> SupportedGameVersions {
|
||||
SupportedGameVersions::All
|
||||
}
|
||||
|
||||
fn validate(
|
||||
&self,
|
||||
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
|
||||
) -> Result<ValidationResult, ValidationError> {
|
||||
archive.by_name("plugin.yml").map_err(|_| {
|
||||
ValidationError::InvalidInput(
|
||||
"No plugin.yml present for plugin file.".into(),
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(ValidationResult::Pass)
|
||||
}
|
||||
}
|
||||
38
src/validate/resourcepack.rs
Normal file
38
src/validate/resourcepack.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use crate::validate::{
|
||||
SupportedGameVersions, ValidationError, ValidationResult,
|
||||
};
|
||||
use std::io::Cursor;
|
||||
use zip::ZipArchive;
|
||||
|
||||
pub struct ResourcePackValidator;
|
||||
|
||||
impl super::Validator for ResourcePackValidator {
|
||||
fn get_file_extensions(&self) -> &[&str] {
|
||||
&["zip"]
|
||||
}
|
||||
|
||||
fn get_project_types(&self) -> &[&str] {
|
||||
&["resourcepack"]
|
||||
}
|
||||
|
||||
fn get_supported_loaders(&self) -> &[&str] {
|
||||
&["minecraft"]
|
||||
}
|
||||
|
||||
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 resourcepack file.".into(),
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(ValidationResult::Pass)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user