2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-10 06:25:00 +00:00
pict-rs/src/config.rs

159 lines
3.7 KiB
Rust
Raw Normal View History

2020-06-07 19:12:19 +00:00
use std::{collections::HashSet, net::SocketAddr, path::PathBuf};
2020-06-07 00:54:06 +00:00
2020-06-07 19:12:19 +00:00
#[derive(Clone, Debug, structopt::StructOpt)]
2020-06-07 01:44:26 +00:00
pub(crate) struct Config {
#[structopt(
short,
long,
help = "Whether to skip validating images uploaded via the internal import API"
)]
skip_validate_imports: bool,
2020-06-07 00:54:06 +00:00
#[structopt(
short,
long,
2020-06-07 20:21:25 +00:00
env = "PICTRS_ADDR",
default_value = "0.0.0.0:8080",
help = "The address and port the server binds to."
2020-06-07 00:54:06 +00:00
)]
addr: SocketAddr,
2020-06-07 20:21:25 +00:00
#[structopt(
short,
long,
env = "PICTRS_PATH",
help = "The path to the data directory, e.g. data/"
)]
2020-06-07 00:54:06 +00:00
path: PathBuf,
2020-06-07 01:44:26 +00:00
#[structopt(
short,
long,
2020-06-07 20:21:25 +00:00
env = "PICTRS_FORMAT",
2020-06-16 20:55:24 +00:00
help = "An optional image format to convert all uploaded files into, supports 'jpg', 'png', and 'webp'"
2020-06-07 01:44:26 +00:00
)]
format: Option<Format>,
2020-06-07 19:12:19 +00:00
#[structopt(
short,
long,
env = "PICTRS_FILTER_WHITELIST",
2020-06-07 19:12:19 +00:00
help = "An optional list of filters to whitelist, supports 'identity', 'thumbnail', and 'blur'"
)]
whitelist: Option<Vec<String>>,
#[structopt(
short,
long,
env = "PICTRS_MAX_FILE_SIZE",
help = "Specify the maximum allowed uploaded file size (in Megabytes)",
default_value = "40"
)]
max_file_size: usize,
2020-07-11 21:28:49 +00:00
#[structopt(
long,
env = "PICTRS_MAX_IMAGE_WIDTH",
help = "Specify the maximum width in pixels allowed on an image",
default_value = "10000"
)]
max_image_width: usize,
#[structopt(
long,
env = "PICTRS_MAX_IMAGE_HEIGHT",
help = "Specify the maximum width in pixels allowed on an image",
default_value = "10000"
)]
max_image_height: usize,
2020-07-11 21:28:49 +00:00
#[structopt(
long,
env = "PICTRS_API_KEY",
help = "An optional string to be checked on requests to privileged endpoints"
)]
api_key: Option<String>,
2020-06-07 00:54:06 +00:00
}
impl Config {
pub(crate) fn bind_address(&self) -> SocketAddr {
self.addr
}
pub(crate) fn data_dir(&self) -> PathBuf {
self.path.clone()
}
2020-06-07 01:44:26 +00:00
pub(crate) fn format(&self) -> Option<Format> {
self.format.clone()
}
2020-06-07 19:12:19 +00:00
pub(crate) fn filter_whitelist(&self) -> Option<HashSet<String>> {
self.whitelist
.as_ref()
.map(|wl| wl.iter().cloned().collect())
}
pub(crate) fn validate_imports(&self) -> bool {
!self.skip_validate_imports
}
pub(crate) fn max_file_size(&self) -> usize {
self.max_file_size
}
2020-07-11 21:28:49 +00:00
pub(crate) fn max_width(&self) -> usize {
self.max_image_width
}
pub(crate) fn max_height(&self) -> usize {
self.max_image_height
}
2020-07-11 21:28:49 +00:00
pub(crate) fn api_key(&self) -> Option<&str> {
self.api_key.as_deref()
2020-07-11 21:28:49 +00:00
}
2020-06-07 01:44:26 +00:00
}
#[derive(Debug, thiserror::Error)]
#[error("Invalid format supplied, {0}")]
pub(crate) struct FormatError(String);
#[derive(Clone, Debug)]
pub(crate) enum Format {
Jpeg,
Png,
2020-06-16 20:55:24 +00:00
Webp,
2020-06-07 01:44:26 +00:00
}
2020-06-16 21:34:37 +00:00
impl Format {
pub(crate) fn to_mime(&self) -> mime::Mime {
match self {
Format::Jpeg => mime::IMAGE_JPEG,
Format::Png => mime::IMAGE_PNG,
Format::Webp => "image/webp".parse().unwrap(),
}
}
pub(crate) fn to_magick_format(&self) -> &'static str {
match self {
Format::Jpeg => "JPEG",
Format::Png => "PNG",
Format::Webp => "WEBP",
}
}
}
2020-06-07 01:44:26 +00:00
impl std::str::FromStr for Format {
type Err = FormatError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"png" => Ok(Format::Png),
"jpg" => Ok(Format::Jpeg),
2020-06-16 20:55:24 +00:00
"webp" => Ok(Format::Webp),
2020-06-07 01:44:26 +00:00
other => Err(FormatError(other.to_string())),
}
}
2020-06-07 00:54:06 +00:00
}