MediaUtil.java
3.3 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* javax.jcr.RepositoryException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.dps;
import com.day.cq.wcm.api.Page;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MediaUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(MediaUtil.class);
private MediaUtil() {
}
public static String getMediaImageSelector(Resource imageResource) throws RepositoryException {
String selector = ".img";
if (MediaUtil.isOriginalImageRequested(imageResource)) {
selector = ".img.original";
} else if (MediaUtil.isImageScalingEnabled(imageResource)) {
String adaptiveSelector = ".img";
String adaptiveWidth = String.valueOf(MediaUtil.getImageWidthSelector(imageResource));
String adaptiveQuality = MediaUtil.getImageQualitySelector(imageResource);
selector = adaptiveSelector + "." + adaptiveWidth + "." + adaptiveQuality;
} else {
LOGGER.info("Media: Scaling disabled. Image will not be scaled, default rendering will be used");
}
return selector;
}
private static boolean isOriginalImageRequested(Resource imageResource) {
return MediaUtil.isBooleanPropertyTrue(imageResource, "useOriginalImage");
}
private static boolean isImageScalingEnabled(Resource imageResource) {
return MediaUtil.isBooleanPropertyTrue(imageResource, "scalingEnabled");
}
private static boolean isBooleanPropertyTrue(Resource imageResource, String propertyName) {
ValueMap properties = ResourceUtil.getValueMap((Resource)imageResource);
boolean isBooleanPropertyTrue = false;
if (properties.containsKey((Object)propertyName)) {
isBooleanPropertyTrue = (Boolean)properties.get(propertyName, Boolean.class);
}
return isBooleanPropertyTrue;
}
private static String getImageWidthSelector(Resource imageResource) {
ValueMap properties = ResourceUtil.getValueMap((Resource)imageResource);
String targetWidth = "full";
String setWidthOverride = (String)properties.get("scalewidth", String.class);
if (setWidthOverride != null) {
targetWidth = setWidthOverride;
}
return targetWidth;
}
private static String getImageQualitySelector(Resource imageResource) {
ValueMap properties = ResourceUtil.getValueMap((Resource)imageResource);
return (String)properties.get("quality", (Object)"full");
}
private static Page getParentPage(Resource resource) {
Resource parentResource = resource.getParent();
if (parentResource != null) {
if (ResourceUtil.isA((Resource)parentResource, (String)"cq:Page")) {
return (Page)parentResource.adaptTo(Page.class);
}
return MediaUtil.getParentPage(parentResource);
}
return null;
}
}