Add better validations

This commit is contained in:
Harrison Burt 2022-03-30 21:49:42 +01:00
parent b5a34859b4
commit 0ba671be56
2 changed files with 34 additions and 4 deletions

View File

@ -36,6 +36,7 @@ pub async fn init(config_file: &Path) -> Result<()> {
_ => return Err(anyhow!("Config file must have an extension of either `.json`,`.yaml` or `.yml`"))
};
validate(&cfg)?;
let _ = CONFIG.set(cfg);
Ok(())
} else {
@ -44,6 +45,32 @@ pub async fn init(config_file: &Path) -> Result<()> {
}
fn validate(cfg: &RuntimeConfig) -> Result<()> {
for (name, cfg) in cfg.buckets.iter() {
if !cfg.formats.png
&& !cfg.formats.jpeg
&& !cfg.formats.gif
&& !cfg.formats.webp
{
return Err(anyhow!("Bucket {} is invalid: At least one encoding format must be enabled.", name))
}
if let Some(ref def) = cfg.default_serving_preset {
if !cfg.presets.contains_key(def) {
return Err(anyhow!("Bucket {} is invalid: Default serving preset does not exist.", name))
}
}
if let Some(default_format) = cfg.default_serving_format {
if !cfg.formats.is_enabled(default_format) {
return Err(anyhow!("Bucket {} is invalid: Default serving format is not an enabled encoding format.", name))
}
}
}
Ok(())
}
#[derive(Debug, Deserialize)]
pub struct RuntimeConfig {
@ -124,11 +151,10 @@ pub struct BucketConfig {
/// Defaults to the first enabled encoding format.
pub default_serving_format: Option<ImageKind>,
#[serde(default = "default_preset")]
/// The default resizing preset to serve images as.
///
/// Defaults to "original".
pub default_serving_preset: String,
/// Defaults to the original image size.
pub default_serving_preset: Option<String>,
#[serde(default)]
/// A set of resizing presets, this allows resizing dimensions to be accessed

View File

@ -162,7 +162,11 @@ impl BucketController {
) -> anyhow::Result<Option<StoreEntry>> {
let _permit = get_optional_permit(&self.global_limiter, &self.limiter).await?;
let sizing_id = size_preset.map(crate::utils::crc_hash).unwrap_or(0);
let sizing_id = size_preset
.map(Some)
.unwrap_or_else(|| self.config.default_serving_preset.clone())
.map(crate::utils::crc_hash)
.unwrap_or(0);
// In real time situations
let fetch_kind = if self.config.mode == ProcessingMode::Realtime {