From 6bea287838a4ce8c83a7935dc748d99c36947fff Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Mon, 25 Dec 2023 16:48:12 +0100 Subject: [PATCH] Ensure configuration is available in all processors --- src/api/api_manager_impl.cpp | 28 ++++++++++++++-------------- src/api/parsers/query.cpp | 12 ++++-------- src/api/parsers/query.h | 6 +----- src/api/processors/alignment.h | 9 +-------- src/api/processors/base.h | 10 ++++++++-- src/api/processors/orientation.h | 9 +-------- src/api/processors/rotation.h | 9 +-------- src/api/processors/thumbnail.h | 10 +--------- 8 files changed, 31 insertions(+), 62 deletions(-) diff --git a/src/api/api_manager_impl.cpp b/src/api/api_manager_impl.cpp index 6207c5c9..eb1c4ed1 100644 --- a/src/api/api_manager_impl.cpp +++ b/src/api/api_manager_impl.cpp @@ -150,7 +150,7 @@ utils::Status ApiManagerImpl::process(const std::string &query, const Source &source, const Target &target, const Config &config) { - auto query_holder = std::make_shared(query, config); + auto query_holder = std::make_shared(query); // Note: the disadvantage of pre-resize extraction behaviour is that none // of the very fast shrink-on-load tricks are possible. This can make @@ -161,23 +161,23 @@ utils::Status ApiManagerImpl::process(const std::string &query, auto stream = processors::Stream(query_holder, config); // Image processors - auto trim = processors::Trim(query_holder); + auto trim = processors::Trim(query_holder, config); auto thumbnail = processors::Thumbnail(query_holder, config); auto orientation = processors::Orientation(query_holder, config); auto alignment = processors::Alignment(query_holder, config); - auto crop = processors::Crop(query_holder); - auto embed = processors::Embed(query_holder); + auto crop = processors::Crop(query_holder, config); + auto embed = processors::Embed(query_holder, config); auto rotation = processors::Rotation(query_holder, config); - auto brightness = processors::Brightness(query_holder); - auto modulate = processors::Modulate(query_holder); - auto contrast = processors::Contrast(query_holder); - auto gamma = processors::Gamma(query_holder); - auto sharpen = processors::Sharpen(query_holder); - auto filter = processors::Filter(query_holder); - auto blur = processors::Blur(query_holder); - auto tint = processors::Tint(query_holder); - auto background = processors::Background(query_holder); - auto mask = processors::Mask(query_holder); + auto brightness = processors::Brightness(query_holder, config); + auto modulate = processors::Modulate(query_holder, config); + auto contrast = processors::Contrast(query_holder, config); + auto gamma = processors::Gamma(query_holder, config); + auto sharpen = processors::Sharpen(query_holder, config); + auto filter = processors::Filter(query_holder, config); + auto blur = processors::Blur(query_holder, config); + auto tint = processors::Tint(query_holder, config); + auto background = processors::Background(query_holder, config); + auto mask = processors::Mask(query_holder, config); // Create image from a source auto image = stream.new_from_source(source); diff --git a/src/api/parsers/query.cpp b/src/api/parsers/query.cpp index eddd6a79..6855fce8 100644 --- a/src/api/parsers/query.cpp +++ b/src/api/parsers/query.cpp @@ -17,8 +17,8 @@ using enums::Position; // `&[lossless]=true` constexpr size_t MAX_KEY_LENGTH = sizeof("lossless") - 1; -// A vector must not have more than 65536 elements. -const size_t MAX_VECTOR_SIZE = 65536; +// A vector must not have more than 10000 elements. +const size_t MAX_VECTOR_SIZE = 10000; // Note: We check crazy numbers within `numeric.h` @@ -202,11 +202,7 @@ void Query::add_value(const std::string &key, const std::string &value, } else if (type == typeid(Color)) { query_map_.emplace(key, parse(value)); } else if (key == "delay") { // type == typeid(std::vector) - // Limit to config_.max_pages - auto delays = tokenize(value, ",", - config_.max_pages > 0 - ? static_cast(config_.max_pages) - : MAX_VECTOR_SIZE); + auto delays = tokenize(value, ",", MAX_VECTOR_SIZE); query_map_.emplace(key, delays); } else if (key == "sharp") { // type == typeid(std::vector) auto params = tokenize(value, ",", 3); @@ -245,7 +241,7 @@ void Query::add_value(const std::string &key, const std::string &value, } } -Query::Query(const std::string &value, const Config &config) : config_(config) { +Query::Query(const std::string &value) { size_t pos = 0; size_t max_pos = value.size(); diff --git a/src/api/parsers/query.h b/src/api/parsers/query.h index 6d0c498f..1dad7aef 100644 --- a/src/api/parsers/query.h +++ b/src/api/parsers/query.h @@ -12,8 +12,6 @@ #include #include -#include - namespace weserv::api::parsers { using TypeMap = std::unordered_map; @@ -22,7 +20,7 @@ using NginxKeySet = std::unordered_set; class Query { public: - Query(const std::string &value, const Config &config); + explicit Query(const std::string &value); template ::value>::type> @@ -79,8 +77,6 @@ class Query { std::vector, std::vector>; std::unordered_map query_map_; - const Config &config_; - template std::vector tokenize(const std::string &data, const std::string &delimiters, size_t max_items); diff --git a/src/api/processors/alignment.h b/src/api/processors/alignment.h index e705d914..def1080b 100644 --- a/src/api/processors/alignment.h +++ b/src/api/processors/alignment.h @@ -6,16 +6,9 @@ namespace weserv::api::processors { class Alignment : ImageProcessor { public: - Alignment(std::shared_ptr query, const Config &config) - : ImageProcessor(std::move(query)), config_(config) {} + using ImageProcessor::ImageProcessor; VImage process(const VImage &image) const override; - - private: - /** - * Global config. - */ - const Config &config_; }; } // namespace weserv::api::processors diff --git a/src/api/processors/base.h b/src/api/processors/base.h index bd8c7e7c..1aff02d9 100644 --- a/src/api/processors/base.h +++ b/src/api/processors/base.h @@ -6,6 +6,7 @@ #include #include +#include namespace weserv::api::processors { @@ -13,8 +14,8 @@ using vips::VImage; class ImageProcessor { public: - explicit ImageProcessor(std::shared_ptr query) - : query_(std::move(query)) {} + ImageProcessor(std::shared_ptr query, const Config &config) + : query_(std::move(query)), config_(config) {} virtual VImage process(const VImage &image) const = 0; @@ -28,6 +29,11 @@ class ImageProcessor { * Query holder. */ const std::shared_ptr query_; + + /** + * Global config. + */ + const Config &config_; }; } // namespace weserv::api::processors diff --git a/src/api/processors/orientation.h b/src/api/processors/orientation.h index 2cc96042..78ed6be9 100644 --- a/src/api/processors/orientation.h +++ b/src/api/processors/orientation.h @@ -6,16 +6,9 @@ namespace weserv::api::processors { class Orientation : ImageProcessor { public: - Orientation(std::shared_ptr query, const Config &config) - : ImageProcessor(std::move(query)), config_(config) {} + using ImageProcessor::ImageProcessor; VImage process(const VImage &image) const override; - - private: - /** - * Global config. - */ - const Config &config_; }; } // namespace weserv::api::processors diff --git a/src/api/processors/rotation.h b/src/api/processors/rotation.h index 91c63996..eaba9cd7 100644 --- a/src/api/processors/rotation.h +++ b/src/api/processors/rotation.h @@ -6,16 +6,9 @@ namespace weserv::api::processors { class Rotation : ImageProcessor { public: - Rotation(std::shared_ptr query, const Config &config) - : ImageProcessor(std::move(query)), config_(config) {} + using ImageProcessor::ImageProcessor; VImage process(const VImage &image) const override; - - private: - /** - * Global config. - */ - const Config &config_; }; } // namespace weserv::api::processors diff --git a/src/api/processors/thumbnail.h b/src/api/processors/thumbnail.h index 677aaa72..8b120bfb 100644 --- a/src/api/processors/thumbnail.h +++ b/src/api/processors/thumbnail.h @@ -3,14 +3,11 @@ #include "../io/source.h" #include "base.h" -#include - namespace weserv::api::processors { class Thumbnail : ImageProcessor { public: - Thumbnail(std::shared_ptr query, const Config &config) - : ImageProcessor(std::move(query)), config_(config) {} + using ImageProcessor::ImageProcessor; /** * Use any shrink-on-load features available in the file import library. @@ -23,11 +20,6 @@ class Thumbnail : ImageProcessor { VImage process(const VImage &image) const override; private: - /** - * Global config. - */ - const Config &config_; - /** * Load a formatted image from a source for a specified image type. * @tparam ImageType Image type.