Skip to content

Commit

Permalink
Ensure configuration is available in all processors
Browse files Browse the repository at this point in the history
  • Loading branch information
kleisauke committed Dec 25, 2023
1 parent a024063 commit 6bea287
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 62 deletions.
28 changes: 14 additions & 14 deletions src/api/api_manager_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<parsers::Query>(query, config);
auto query_holder = std::make_shared<parsers::Query>(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
Expand All @@ -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);
Expand Down
12 changes: 4 additions & 8 deletions src/api/parsers/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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<Color>(value));
} else if (key == "delay") { // type == typeid(std::vector<int>)
// Limit to config_.max_pages
auto delays = tokenize<int>(value, ",",
config_.max_pages > 0
? static_cast<size_t>(config_.max_pages)
: MAX_VECTOR_SIZE);
auto delays = tokenize<int>(value, ",", MAX_VECTOR_SIZE);
query_map_.emplace(key, delays);
} else if (key == "sharp") { // type == typeid(std::vector<float>)
auto params = tokenize<float>(value, ",", 3);
Expand Down Expand Up @@ -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();

Expand Down
6 changes: 1 addition & 5 deletions src/api/parsers/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include <variant>
#include <vector>

#include <weserv/config.h>

namespace weserv::api::parsers {

using TypeMap = std::unordered_map<std::string, std::type_index>;
Expand All @@ -22,7 +20,7 @@ using NginxKeySet = std::unordered_set<std::string>;

class Query {
public:
Query(const std::string &value, const Config &config);
explicit Query(const std::string &value);

template <typename E,
typename = typename std::enable_if<std::is_enum<E>::value>::type>
Expand Down Expand Up @@ -79,8 +77,6 @@ class Query {
std::vector<int>, std::vector<float>>;
std::unordered_map<std::string, QueryVariant> query_map_;

const Config &config_;

template <typename T>
std::vector<T> tokenize(const std::string &data,
const std::string &delimiters, size_t max_items);
Expand Down
9 changes: 1 addition & 8 deletions src/api/processors/alignment.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@ namespace weserv::api::processors {

class Alignment : ImageProcessor {
public:
Alignment(std::shared_ptr<parsers::Query> 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
10 changes: 8 additions & 2 deletions src/api/processors/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
#include <utility>

#include <vips/vips8>
#include <weserv/config.h>

namespace weserv::api::processors {

using vips::VImage;

class ImageProcessor {
public:
explicit ImageProcessor(std::shared_ptr<parsers::Query> query)
: query_(std::move(query)) {}
ImageProcessor(std::shared_ptr<parsers::Query> query, const Config &config)
: query_(std::move(query)), config_(config) {}

virtual VImage process(const VImage &image) const = 0;

Expand All @@ -28,6 +29,11 @@ class ImageProcessor {
* Query holder.
*/
const std::shared_ptr<parsers::Query> query_;

/**
* Global config.
*/
const Config &config_;
};

} // namespace weserv::api::processors
9 changes: 1 addition & 8 deletions src/api/processors/orientation.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@ namespace weserv::api::processors {

class Orientation : ImageProcessor {
public:
Orientation(std::shared_ptr<parsers::Query> 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
9 changes: 1 addition & 8 deletions src/api/processors/rotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@ namespace weserv::api::processors {

class Rotation : ImageProcessor {
public:
Rotation(std::shared_ptr<parsers::Query> 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
10 changes: 1 addition & 9 deletions src/api/processors/thumbnail.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
#include "../io/source.h"
#include "base.h"

#include <weserv/config.h>

namespace weserv::api::processors {

class Thumbnail : ImageProcessor {
public:
Thumbnail(std::shared_ptr<parsers::Query> 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.
Expand All @@ -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.
Expand Down

0 comments on commit 6bea287

Please sign in to comment.