Update cli flags

This commit is contained in:
Aode (Lion) 2021-10-18 23:51:04 -05:00
parent 3fe66129be
commit 9559a8b202
3 changed files with 21 additions and 23 deletions

View file

@ -26,7 +26,10 @@ OPTIONS:
--api-key <api-key> --api-key <api-key>
An optional string to be checked on requests to privileged endpoints [env: PICTRS_API_KEY=] An optional string to be checked on requests to privileged endpoints [env: PICTRS_API_KEY=]
-f, --format <format> -f, --filters <filters>...
An optional list of filters to permit, supports 'identity', 'thumbnail', 'resize', 'crop', and 'blur' [env:
PICTRS_ALLOWED_FILTERS=]
-i, --image-format <image-format>
An optional image format to convert all uploaded files into, supports 'jpg', 'png', and 'webp' [env: An optional image format to convert all uploaded files into, supports 'jpg', 'png', and 'webp' [env:
PICTRS_FORMAT=] PICTRS_FORMAT=]
-m, --max-file-size <max-file-size> -m, --max-file-size <max-file-size>
@ -42,9 +45,6 @@ OPTIONS:
Enable OpenTelemetry Tracing exports to the given OpenTelemetry collector [env: PICTRS_OPENTELEMETRY_URL=] Enable OpenTelemetry Tracing exports to the given OpenTelemetry collector [env: PICTRS_OPENTELEMETRY_URL=]
-p, --path <path> The path to the data directory, e.g. data/ [env: PICTRS_PATH=] -p, --path <path> The path to the data directory, e.g. data/ [env: PICTRS_PATH=]
-w, --whitelist <whitelist>...
An optional list of filters to whitelist, supports 'identity', 'thumbnail', and 'blur' [env:
PICTRS_FILTER_WHITELIST=]
``` ```
#### Example: #### Example:

View file

@ -33,15 +33,15 @@ pub(crate) struct Config {
env = "PICTRS_FORMAT", env = "PICTRS_FORMAT",
help = "An optional image format to convert all uploaded files into, supports 'jpg', 'png', and 'webp'" help = "An optional image format to convert all uploaded files into, supports 'jpg', 'png', and 'webp'"
)] )]
format: Option<Format>, image_format: Option<Format>,
#[structopt( #[structopt(
short, short,
long, long,
env = "PICTRS_FILTER_WHITELIST", env = "PICTRS_ALLOWED_FILTERS",
help = "An optional list of filters to whitelist, supports 'identity', 'thumbnail', and 'blur'" help = "An optional list of filters to permit, supports 'identity', 'thumbnail', 'resize', 'crop', and 'blur'"
)] )]
whitelist: Option<Vec<String>>, filters: Option<Vec<String>>,
#[structopt( #[structopt(
short, short,
@ -94,13 +94,11 @@ impl Config {
} }
pub(crate) fn format(&self) -> Option<Format> { pub(crate) fn format(&self) -> Option<Format> {
self.format.clone() self.image_format.clone()
} }
pub(crate) fn filter_whitelist(&self) -> Option<HashSet<String>> { pub(crate) fn allowed_filters(&self) -> Option<HashSet<String>> {
self.whitelist self.filters.as_ref().map(|wl| wl.iter().cloned().collect())
.as_ref()
.map(|wl| wl.iter().cloned().collect())
} }
pub(crate) fn validate_imports(&self) -> bool { pub(crate) fn validate_imports(&self) -> bool {

View file

@ -408,7 +408,7 @@ async fn prepare_process(
query: web::Query<ProcessQuery>, query: web::Query<ProcessQuery>,
ext: &str, ext: &str,
manager: &UploadManager, manager: &UploadManager,
whitelist: &Option<HashSet<String>>, filters: &Option<HashSet<String>>,
) -> Result<(Format, String, PathBuf, Vec<String>), Error> { ) -> Result<(Format, String, PathBuf, Vec<String>), Error> {
let (alias, operations) = let (alias, operations) =
query query
@ -429,10 +429,10 @@ async fn prepare_process(
let name = manager.from_alias(alias).await?; let name = manager.from_alias(alias).await?;
let operations = if let Some(whitelist) = whitelist.as_ref() { let operations = if let Some(filters) = filters.as_ref() {
operations operations
.into_iter() .into_iter()
.filter(|(k, _)| whitelist.contains(&k.to_lowercase())) .filter(|(k, _)| filters.contains(&k.to_lowercase()))
.collect() .collect()
} else { } else {
operations operations
@ -450,15 +450,15 @@ async fn prepare_process(
Ok((format, name, thumbnail_path, thumbnail_args)) Ok((format, name, thumbnail_path, thumbnail_args))
} }
#[instrument(name = "Fetching derived details", skip(manager, whitelist))] #[instrument(name = "Fetching derived details", skip(manager, filters))]
async fn process_details( async fn process_details(
query: web::Query<ProcessQuery>, query: web::Query<ProcessQuery>,
ext: web::Path<String>, ext: web::Path<String>,
manager: web::Data<UploadManager>, manager: web::Data<UploadManager>,
whitelist: web::Data<Option<HashSet<String>>>, filters: web::Data<Option<HashSet<String>>>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let (_, name, thumbnail_path, _) = let (_, name, thumbnail_path, _) =
prepare_process(query, ext.as_str(), &manager, &whitelist).await?; prepare_process(query, ext.as_str(), &manager, &filters).await?;
let real_path = manager let real_path = manager
.variant_path(&thumbnail_path, &name) .variant_path(&thumbnail_path, &name)
@ -473,16 +473,16 @@ async fn process_details(
} }
/// Process files /// Process files
#[instrument(name = "Serving processed image", skip(manager, whitelist))] #[instrument(name = "Serving processed image", skip(manager, filters))]
async fn process( async fn process(
range: Option<range::RangeHeader>, range: Option<range::RangeHeader>,
query: web::Query<ProcessQuery>, query: web::Query<ProcessQuery>,
ext: web::Path<String>, ext: web::Path<String>,
manager: web::Data<UploadManager>, manager: web::Data<UploadManager>,
whitelist: web::Data<Option<HashSet<String>>>, filters: web::Data<Option<HashSet<String>>>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let (format, name, thumbnail_path, thumbnail_args) = let (format, name, thumbnail_path, thumbnail_args) =
prepare_process(query, ext.as_str(), &manager, &whitelist).await?; prepare_process(query, ext.as_str(), &manager, &filters).await?;
let real_path_opt = manager.variant_path(&thumbnail_path, &name).await?; let real_path_opt = manager.variant_path(&thumbnail_path, &name).await?;
@ -948,7 +948,7 @@ async fn main() -> Result<(), anyhow::Error> {
.wrap(Deadline) .wrap(Deadline)
.app_data(web::Data::new(manager.clone())) .app_data(web::Data::new(manager.clone()))
.app_data(web::Data::new(client)) .app_data(web::Data::new(client))
.app_data(web::Data::new(CONFIG.filter_whitelist())) .app_data(web::Data::new(CONFIG.allowed_filters()))
.service( .service(
web::scope("/image") web::scope("/image")
.service( .service(