Skip to content

Commit 324c2f4

Browse files
authored
Add support for web assembly and lambda functions in transformations
1 parent 8fabd29 commit 324c2f4

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.cloudinary;
2+
3+
import com.cloudinary.utils.StringUtils;
4+
5+
import java.util.List;
6+
7+
public class BaseParam {
8+
private String param;
9+
10+
protected BaseParam(List<String> components) {
11+
this.param = StringUtils.join(components, ":");
12+
}
13+
14+
protected BaseParam(String... components) {
15+
this.param = StringUtils.join(components, ":");
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return param;
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.cloudinary;
2+
3+
import com.cloudinary.utils.Base64Coder;
4+
5+
/**
6+
* Helper class to generate a custom action params to be used in {@link Transformation#customAction(CustomAction)}.
7+
*/
8+
public class CustomAction extends BaseParam{
9+
10+
private CustomAction(String... components) {
11+
super(components);
12+
}
13+
14+
/**
15+
* Generate a web-assembly custom action param to send to {@link Transformation#customAction(CustomAction)}
16+
* @param publicId The public id of the web-assembly file
17+
* @return A new instance of custom action param
18+
*/
19+
public static CustomAction wasm(String publicId){
20+
return new CustomAction("wasm", publicId);
21+
}
22+
23+
/**
24+
* Generate a remote lambda custom action param to send to {@link Transformation#customAction(CustomAction)}
25+
* @param url The public url of the aws lambda function
26+
* @return A new instance of custom action param
27+
*/
28+
public static CustomAction remote(String url){
29+
return new CustomAction("remote", Base64Coder.encodeURLSafeString(url));
30+
}
31+
}

cloudinary-core/src/main/java/com/cloudinary/Transformation.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ public String generate(Map options) {
634634
"dl", "delay",
635635
"dn", "density",
636636
"f", "fetch_format",
637+
"fn", "custom_action",
637638
"fps", "fps",
638639
"g", "gravity",
639640
"l", "overlay",
@@ -841,4 +842,12 @@ public T variables(Expression...variables) {
841842
return param("variables", variables);
842843
}
843844

845+
/**
846+
* Set a custom action, such as a call to a lambda function or a web-assembly function.
847+
* @param action The custom action to perform, see {@link CustomAction}.
848+
* @return The transformation for chaining
849+
*/
850+
public T customAction(CustomAction action) {
851+
return param("custom_action", action.toString());
852+
}
844853
}

cloudinary-core/src/main/java/com/cloudinary/utils/Base64Coder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ public static byte[] decode(char[] in, int iOff, int iLen) {
280280
private Base64Coder() {
281281
}
282282

283+
public static String encodeURLSafeString(String s) {
284+
return encodeURLSafeString(s.getBytes());
285+
}
286+
283287
public static String encodeURLSafeString(byte[] digest) {
284288
char[] encode = encode(digest);
285289
for (int i = 0; i < encode.length; i++) {

cloudinary-core/src/test/java/com/cloudinary/test/CloudinaryTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.regex.Matcher;
2626
import java.util.regex.Pattern;
2727

28+
import static com.cloudinary.CustomAction.remote;
29+
import static com.cloudinary.CustomAction.wasm;
2830
import static com.cloudinary.utils.ObjectUtils.asMap;
2931
import static com.cloudinary.utils.ObjectUtils.emptyMap;
3032
import static org.junit.Assert.*;
@@ -1101,6 +1103,13 @@ public void testKeyframeInterval(){
11011103
assertEquals("", new Transformation().keyframeInterval(null).generate());
11021104
}
11031105

1106+
@Test
1107+
public void testCustomAction(){
1108+
assertEquals("fn_wasm:blur_wasm", new Transformation().customAction(wasm("blur_wasm")).generate());
1109+
assertEquals("fn_remote:aHR0cHM6Ly9kZjM0cmE0YS5leGVjdXRlLWFwaS51cy13ZXN0LTIuYW1hem9uYXdzLmNvbS9kZWZhdWx0L2Nsb3VkaW5hcnlBY3Rpb24=",
1110+
new Transformation().customAction(remote("https://df34ra4a.execute-api.us-west-2.amazonaws.com/default/cloudinaryAction")).generate());
1111+
}
1112+
11041113
public static Map<String, String> getUrlParameters(URI uri) throws UnsupportedEncodingException {
11051114
Map<String, String> params = new HashMap<String, String>();
11061115
for (String param : uri.getRawQuery().split("&")) {

0 commit comments

Comments
 (0)