-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSignature.java
More file actions
430 lines (375 loc) · 16.1 KB
/
Signature.java
File metadata and controls
430 lines (375 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package io.rocketbase.asset.imgproxy;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;
import io.rocketbase.asset.imgproxy.options.GravityType;
import io.rocketbase.asset.imgproxy.options.ImageType;
import io.rocketbase.asset.imgproxy.options.ResizeType;
import io.rocketbase.asset.imgproxy.options.WatermarkPositionType;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* Format definition
* The advanced URL should contain the signature, processing options, and source URL, like this:
* <p>
* /%signature/%processing_options/plain/%source_url@%extension
* /%signature/%processing_options/%encoded_source_url.%extension
* <p>
* Example
* Signed imgproxy URL that uses sharp preset, resizes http://example.com/images/curiosity.jpg to fill 300x400 area with smart gravity without enlarging, and then converts the image to png:
* <p>
* http://imgproxy.example.com/AfrOrF3gWeDA6VOlDG4TzxMv39O7MXnF4CXpKUwGqRM/preset:sharp/resize:fill:300:400:0/gravity:sm/plain/http://example.com/images/curiosity.jpg@png
* The same URL with shortcuts will look like this:
* <p>
* http://imgproxy.example.com/AfrOrF3gWeDA6VOlDG4TzxMv39O7MXnF4CXpKUwGqRM/pr:sharp/rs:fill:300:400:0/g:sm/plain/http://example.com/images/curiosity.jpg@png
* The same URL with Base64-encoded source URL will look like this:
* <p>
* http://imgproxy.example.com/AfrOrF3gWeDA6VOlDG4TzxMv39O7MXnF4CXpKUwGqRM/pr:sharp/rs:fill:300:400:0/g:sm/aHR0cDovL2V4YW1w/bGUuY29tL2ltYWdl/cy9jdXJpb3NpdHku/anBn.png
*/
@SuppressWarnings("WeakerAccess")
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class Signature {
public static final Pattern HEXCOLOR_PATTERN = Pattern.compile("[0-9A-Fa-f]{6}");
private final SignatureConfiguration configuration;
private List<String> processingOptions = new ArrayList<String>();
public static Signature of(SignatureConfiguration configuration) {
return new Signature(configuration);
}
/**
* Meta-option that defines the width, height, enlarge, and extend. All arguments are optional and can be omited to use their default values.
*/
public Signature size(int width, int height, boolean enlarge, boolean extend) {
processingOptions.add(createProcessingOption("s", width, height, enlarge, extend));
return this;
}
/**
* Meta-option that defines the width, height, enlarge, and extend. All arguments are optional and can be omited to use their default values.
*/
public Signature size(int width, int height, boolean enlarge) {
processingOptions.add(createProcessingOption("s", width, height, enlarge));
return this;
}
/**
* Meta-option that defines the width, height, enlarge, and extend. All arguments are optional and can be omited to use their default values.
*/
public Signature size(int width, int height) {
processingOptions.add(createProcessingOption("s", width, height));
return this;
}
/**
* Meta-option that defines the resizing type, width, height, enlarge, and extend.
*/
public Signature resize(ResizeType resizeType, int width, int height, boolean enlarge, boolean extend) {
processingOptions.add(createProcessingOption("rs", resizeType.name(), width, height, enlarge, extend));
return this;
}
/**
* Meta-option that defines the resizing type, width, height, enlarge, and extend.
*/
public Signature resize(ResizeType resizeType, int width, int height, boolean enlarge) {
processingOptions.add(createProcessingOption("rs", resizeType.name(), width, height, enlarge));
return this;
}
/**
* Meta-option that defines the resizing type, width, height, enlarge, and extend.
*/
public Signature resize(ResizeType resizeType, int width, int height) {
processingOptions.add(createProcessingOption("rs", resizeType.name(), width, height));
return this;
}
/**
* Defines how imgproxy will resize the source image.
*/
public Signature resize(ResizeType resizeType) {
processingOptions.add(createProcessingOption("rt", resizeType.name()));
return this;
}
/**
* Defines how imgproxy will min-width the source image.
*/
public Signature minWidth(int minWidth){
processingOptions.add(createProcessingOption("mw", minWidth));
return this;
}
/**
* Defines how imgproxy will min-height the source image.
*/
public Signature minHeight(int minHeight){
processingOptions.add(createProcessingOption("mh", minHeight));
return this;
}
/**
* Defines the width of the resulting image. When set to 0, imgproxy will calculate the resulting width using the defined height and source aspect ratio.
* Default: 0
*/
public Signature width(int width) {
processingOptions.add(createProcessingOption("w", width));
return this;
}
/**
* Defines the height of the resulting image. When set to 0, imgproxy will calculate resulting height using the defined width and source aspect ratio.
* Default: 0
*/
public Signature height(int height) {
processingOptions.add(createProcessingOption("h", height));
return this;
}
/**
* When set, imgproxy will multiply the image dimensions according to this factor for HiDPI (Retina) devices.
* Default: true
*/
public Signature dpr(boolean useDpr) {
processingOptions.add(createProcessingOption("dpr", useDpr));
return this;
}
/**
* If set to false, imgproxy will not enlarge the image if it is smaller than the given size. With any other value, imgproxy will enlarge the image.
* Default: false
*/
public Signature enlarge(boolean enlarge) {
processingOptions.add(createProcessingOption("el", enlarge));
return this;
}
/**
* If set to false, imgproxy will not extend the image if the resizing result is smaller than the given size. With any other value, imgproxy will extend the image to the given size.
* Default: false
*/
public Signature extend(boolean extend) {
processingOptions.add(createProcessingOption("ex", extend));
return this;
}
/**
* When imgproxy needs to cut some parts of the image, it is guided by the gravity.
* Specify gravity offset by X and Y axes.
*/
public Signature gravity(GravityType gravityType, int offsetX, int offsetY) {
if (!gravityType.isFocalPointAllowed()) {
throw new IllegalArgumentException(gravityType + " is not allowed with offset");
}
processingOptions.add(createProcessingOption("g", gravityType.name(), offsetX, offsetY));
return this;
}
/**
* When imgproxy needs to cut some parts of the image, it is guided by the gravity.
*/
public Signature gravity(GravityType gravityType) {
processingOptions.add(createProcessingOption("g", gravityType.name()));
return this;
}
/**
* focus point gravity. x and y are floating point numbers between 0 and 1 that define the coordinates of the center of the resulting image. Treat 0 and 1 as right/left for x and top/bottom for y
*/
public Signature gravity(double focalX, double focalY) {
processingOptions.add(createProcessingOption("g", "fp", focalX, focalY));
return this;
}
/**
* Defines an area of the image to be processed (crop before resize).
* <p>
* width and height define the size of the area. When width or height is set to 0, imgproxy will use the full width/height of the source image.
* gravity accepts the same values as gravity option.
*/
public Signature crop(int width, int height, GravityType gravityType, int offsetX, int offsetY) {
if (!gravityType.isFocalPointAllowed()) {
throw new IllegalArgumentException(gravityType + " is not allowed with offset");
}
processingOptions.add(createProcessingOption("c", width, height, gravityType.name(), offsetX, offsetY));
return this;
}
/**
* Defines an area of the image to be processed (crop before resize).
* <p>
* width and height define the size of the area. When width or height is set to 0, imgproxy will use the full width/height of the source image.
* gravity accepts the same values as gravity option.
*/
public Signature crop(int width, int height, GravityType gravityType) {
processingOptions.add(createProcessingOption("c", width, height, gravityType.name()));
return this;
}
/**
* Defines an area of the image to be processed (crop before resize).
* <p>
* width and height define the size of the area. When width or height is set to 0, imgproxy will use the full width/height of the source image.
* gravity accepts the same values as gravity option.
*/
public Signature crop(int width, int height, double focalX, double focalY) {
processingOptions.add(createProcessingOption("c", width, height, "fp", focalX, focalY));
return this;
}
/**
* Defines an area of the image to be processed (crop before resize).
* <p>
* width and height define the size of the area. When width or height is set to 0, imgproxy will use the full width/height of the source image.
* Imgproxy will use the value of the gravity option.
*/
public Signature crop(int width, int height) {
processingOptions.add(createProcessingOption("c", width, height));
return this;
}
/**
* Redefines quality of the resulting image, percentage.
* Default: value from the environment variable.
*/
public Signature quality(int percentage) {
if (percentage < 1 || percentage > 100) {
throw new IllegalArgumentException("quality percentage must be between 1 and 100 inclusively");
}
processingOptions.add(createProcessingOption("q", percentage));
return this;
}
/**
* When set, imgproxy will fill the resulting image background with the specified color. R, G, and B are red, green and blue channel values of the background color (0-255).
* Useful when you convert an image with alpha-channel to JPEG.
* Default: disabled
*/
public Signature background(int r, int g, int b) {
if (!isByte(r) || !isByte(g) || !isByte(b)) {
throw new IllegalArgumentException("r,g and b values must be between 0 and 255 inclusively");
}
processingOptions.add(createProcessingOption("bg", r, g, b));
return this;
}
/**
* When set, imgproxy will fill the resulting image background with the specified color.
* hex_color is a hex-coded value of the color. Useful when you convert an image with alpha-channel to JPEG.
* Default: disabled
*/
public Signature background(String hexColor) {
if (!isHexColor(hexColor)) {
throw new IllegalArgumentException("hexcolor must be a hexadecimalencoded string for 3 bytes like ffffff for white");
}
processingOptions.add(createProcessingOption("bg", hexColor));
return this;
}
public Signature blur(int sigma) {
processingOptions.add(createProcessingOption("bl", sigma));
return this;
}
public Signature sharpen(int sigma) {
processingOptions.add(createProcessingOption("sh", sigma));
return this;
}
/**
* Puts watermark on the processed image
*/
public Signature watermark(double opacity, WatermarkPositionType position, int offsetX, int offsetY, double scale) {
processingOptions.add(createProcessingOption("wm", opacity, position.name(), offsetX, offsetY, scale));
return this;
}
/**
* Puts watermark on the processed image
*/
public Signature watermark(double opacity, WatermarkPositionType position, int offsetX, int offsetY) {
processingOptions.add(createProcessingOption("wm", opacity, position.name(), offsetX, offsetY));
return this;
}
/**
* Puts watermark on the processed image
*/
public Signature watermark(double opacity, WatermarkPositionType position) {
processingOptions.add(createProcessingOption("wm", opacity, position.name()));
return this;
}
/**
* Puts watermark on the processed image
*/
public Signature watermark(double opacity) {
processingOptions.add(createProcessingOption("wm", opacity));
return this;
}
/**
* Defines a list of presets to be used by imgproxy. Feel free to use as many presets in a single URL as you need.
*/
public Signature preset(String... presetNames) {
processingOptions.add(createProcessingOption("pr", (Object[]) presetNames));
return this;
}
/**
* Cache buster doesn’t affect image processing but it’s changing allows to bypass CDN, proxy server and browser cache.
* Useful when you have changed some things that are not reflected in the URL like image quality settings, presets or watermark data.
* <p>
* It’s highly recommended to prefer cachebuster option over URL query string because the option can be properly signed.
*/
public Signature cachebuster(String version) {
processingOptions.add(createProcessingOption("cb", version));
return this;
}
/**
* Defines a filename for Content-Disposition header. When not specified, imgproxy will get filename from the source url.
*/
public Signature filename(String filename) {
processingOptions.add(createProcessingOption("fn", filename));
return this;
}
/**
* Specifies the resulting image format. Alias for extension URL part.
*/
public Signature format(String extension) {
processingOptions.add(createProcessingOption("f", extension));
return this;
}
public String url(String sourceUrl) {
return url(sourceUrl, null);
}
@SneakyThrows
public String url(String sourceUrl, ImageType imageType) {
StringBuilder builder = new StringBuilder();
for (String processingOption : processingOptions) {
builder.append("/")
.append(processingOption);
}
String url = BaseEncoding.base64Url()
.omitPadding()
.encode(sourceUrl.getBytes());
builder.append("/")
.append(url);
if (imageType != null) {
builder.append(".")
.append(imageType.name());
}
String path = builder.toString();
String signature = signUrl(path, configuration.getKey(), configuration.getSalt(), configuration.getNumberOfSignatureBytes());
return configuration.getBaseurl() + "/" +
signature +
path;
}
static String signUrl(String path, String key, String salt, int numberOfSignatureBytes) throws NoSuchAlgorithmException, InvalidKeyException {
if (key == null || salt == null) {
return "notset";
}
String encodeString = salt + path;
HashCode hashCode = Hashing.hmacSha256(key.getBytes())
.hashBytes(encodeString.getBytes());
return BaseEncoding.base64Url()
.omitPadding()
.encode(hashCode.asBytes(), 0, numberOfSignatureBytes);
}
static String createProcessingOption(String command, Object... args) {
StringBuilder builder = new StringBuilder(command);
if (args != null) {
for (Object arg : args) {
builder.append(":");
if (arg instanceof Boolean) {
builder.append((Boolean) arg ? 1 : 0);
} else {
builder.append(arg);
}
}
}
return builder.toString();
}
static boolean isHexColor(String hexColor) {
return HEXCOLOR_PATTERN
.matcher(hexColor)
.matches();
}
static boolean isByte(int r) {
return r >= 0 && r <= 255;
}
}