|
1 | 1 | require "anyway_config" |
2 | 2 |
|
| 3 | +require "imgproxy/service_config" |
3 | 4 | require "imgproxy/url_adapters" |
4 | 5 |
|
5 | 6 | module Imgproxy |
6 | 7 | # Imgproxy config |
7 | 8 | # |
8 | | - # @!attribute endpoint |
9 | | - # imgproxy endpoint |
10 | | - # @return [String] |
11 | | - # @!attribute key |
12 | | - # imgproxy hex-encoded signature key |
13 | | - # @return [String] |
14 | | - # @!attribute salt |
15 | | - # imgproxy hex-encoded signature salt |
16 | | - # @return [String] |
17 | | - # @!attribute raw_key |
18 | | - # Decoded signature key |
19 | | - # @return [String] |
20 | | - # @!attribute raw_salt |
21 | | - # Decoded signature salt |
22 | | - # @return [String] |
23 | | - # @!attribute signature_size |
24 | | - # imgproxy signature size. Defaults to 32 |
25 | | - # @return [String] |
26 | 9 | # @!attribute use_short_options |
27 | 10 | # Use short processing option names (+rs+ for +resize+, +g+ for +gravity+, etc). |
28 | 11 | # Defaults to true |
@@ -50,49 +33,81 @@ module Imgproxy |
50 | 33 | # @see https://github.com/palkan/anyway_config anyway_config |
51 | 34 | class Config < Anyway::Config |
52 | 35 | attr_config( |
53 | | - :endpoint, |
54 | | - :key, |
55 | | - :salt, |
56 | | - :raw_key, |
57 | | - :raw_salt, |
58 | | - signature_size: 32, |
59 | 36 | use_short_options: true, |
60 | 37 | base64_encode_urls: false, |
61 | 38 | always_escape_plain_urls: false, |
62 | 39 | use_s3_urls: false, |
63 | 40 | use_gcs_urls: false, |
64 | 41 | gcs_bucket: nil, |
65 | 42 | shrine_host: nil, |
| 43 | + services: {}, |
66 | 44 | ) |
67 | 45 |
|
68 | | - alias_method :set_key, :key= |
69 | | - alias_method :set_raw_key, :raw_key= |
70 | | - alias_method :set_salt, :salt= |
71 | | - alias_method :set_raw_salt, :raw_salt= |
72 | | - private :set_key, :set_raw_key, :set_salt, :set_raw_salt |
| 46 | + def endpoint |
| 47 | + service(:default).endpoint |
| 48 | + end |
| 49 | + |
| 50 | + def endpoint=(value) |
| 51 | + service(:default).endpoint = value |
| 52 | + end |
| 53 | + |
| 54 | + def key |
| 55 | + service(:default).key |
| 56 | + end |
73 | 57 |
|
74 | 58 | def key=(value) |
75 | | - value = value&.to_s |
76 | | - super(value) |
77 | | - set_raw_key(value && [value].pack("H*")) |
| 59 | + service(:default).key = value |
| 60 | + end |
| 61 | + |
| 62 | + def raw_key |
| 63 | + service(:default).raw_key |
78 | 64 | end |
79 | 65 |
|
80 | 66 | def raw_key=(value) |
81 | | - value = value&.to_s |
82 | | - super(value) |
83 | | - set_key(value&.unpack("H*")&.first) |
| 67 | + service(:default).raw_key = value |
| 68 | + end |
| 69 | + |
| 70 | + def salt |
| 71 | + service(:default).salt |
84 | 72 | end |
85 | 73 |
|
86 | 74 | def salt=(value) |
87 | | - value = value&.to_s |
88 | | - super(value) |
89 | | - set_raw_salt(value && [value].pack("H*")) |
| 75 | + service(:default).salt = value |
| 76 | + end |
| 77 | + |
| 78 | + def raw_salt |
| 79 | + service(:default).raw_salt |
90 | 80 | end |
91 | 81 |
|
92 | 82 | def raw_salt=(value) |
93 | | - value = value&.to_s |
94 | | - super(value) |
95 | | - set_salt(value&.unpack("H*")&.first) |
| 83 | + service(:default).raw_salt = value |
| 84 | + end |
| 85 | + |
| 86 | + def signature_size |
| 87 | + service(:default).signature_size |
| 88 | + end |
| 89 | + |
| 90 | + def signature_size=(value) |
| 91 | + service(:default).signature_size = value |
| 92 | + end |
| 93 | + |
| 94 | + def service(name) |
| 95 | + services[name.to_sym] ||= ServiceConfig.new(services[:default].to_h) |
| 96 | + yield services[name.to_sym] if block_given? |
| 97 | + |
| 98 | + services[name.to_sym] |
| 99 | + end |
| 100 | + |
| 101 | + def services |
| 102 | + @services ||= {}.tap do |s| |
| 103 | + s[:default] = ServiceConfig.new |
| 104 | + |
| 105 | + super.each do |name, data| |
| 106 | + s[name.to_sym] = ServiceConfig.new( |
| 107 | + s[:default].to_h.merge(data.symbolize_keys), |
| 108 | + ) |
| 109 | + end |
| 110 | + end |
96 | 111 | end |
97 | 112 |
|
98 | 113 | # @deprecated Please use {#key} instead |
|
0 commit comments