FileThumbnailProvider.java
3.16 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.thumbnail.ThumbnailProvider
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
*/
package com.day.cq.dam.commons.thumbnail.provider;
import com.day.cq.commons.thumbnail.ThumbnailProvider;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
@Component(metatype=0)
@Service
@Property(name="thumbnail.provider.name", value={"nt:file"})
public class FileThumbnailProvider
implements ThumbnailProvider {
private static final String LIGHBOX_ASSETREF_SEPARATOR = "_:_";
private static final String PROP_NAME_ASSETREFS = "assetRefs";
private static final Pattern lightboxPattern = Pattern.compile("/home/users/(.*)/(.*)/profile/lightbox/(.*)");
public String getThumbnailPath(Resource resource, int width, int height, Map<String, Object> additionalConf) {
if (this.isLightBoxAsset(resource)) {
return this.getThumbnailPathForLightboxEntry(resource, width, height);
}
return null;
}
private String getThumbnailPathForLightboxEntry(Resource entry, int width, int height) {
String[] assetRefs;
String path = null;
String lightboxPath = entry.getParent().getPath();
ResourceResolver resolver = entry.getResourceResolver();
ValueMap lightboxProps = ResourceUtil.getValueMap((Resource)resolver.getResource(lightboxPath));
for (String ref : assetRefs = (String[])lightboxProps.get("assetRefs", (Object)new String[0])) {
Asset asset;
String assetPath;
Rendition rendition;
Resource assetResource;
String renditionName;
String[] fragments = StringUtils.splitByWholeSeparator((String)ref, (String)"_:_");
if (2 != fragments.length || !ResourceUtil.getName((Resource)entry).equals(fragments[0]) || null == (asset = (Asset)(assetResource = resolver.getResource(assetPath = fragments[1])).adaptTo(Asset.class)) || null == (rendition = asset.getRendition(renditionName = "cq5dam.thumbnail." + width + "." + height + ".png"))) continue;
path = rendition.getPath();
}
return path;
}
private boolean isLightBoxAsset(Resource resource) {
Matcher matcher = lightboxPattern.matcher(resource.getPath());
return matcher.matches();
}
}