AssetDetails.java
8.01 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.LabeledResource
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.AssetReferenceResolver
* com.day.cq.dam.api.Rendition
* com.day.cq.dam.commons.util.DamUtil
* com.day.cq.dam.commons.util.UIHelper
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.commons.ReferenceSearch
* com.day.cq.wcm.commons.ReferenceSearch$Info
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.RepositoryException
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONObject
*/
package com.day.cq.wcm.resource.details;
import com.day.cq.commons.LabeledResource;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetReferenceResolver;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.commons.util.DamUtil;
import com.day.cq.dam.commons.util.UIHelper;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.commons.ReferenceSearch;
import com.day.cq.wcm.resource.details.ResourceDetails;
import com.day.text.Text;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONObject;
public class AssetDetails
implements ResourceDetails {
final String ASSET_PROPERTY_METADATA = "jcr:content/metadata";
final String ASSET_PROPERTY_COMMENTS = "jcr:content/comments";
private Resource resource;
private Asset asset;
private Node assetNode;
public AssetDetails(Resource resource) {
if (resource == null) {
throw new IllegalArgumentException("Resource may not be null!");
}
this.resource = resource;
this.assetNode = (Node)resource.adaptTo(Node.class);
this.asset = (Asset)resource.adaptTo(Asset.class);
}
public Node getAssetNode() {
return this.assetNode;
}
public Asset getAsset() {
return this.asset;
}
@Override
public String getName() throws RepositoryException {
return this.asset.getName();
}
@Override
public long getLastModified() throws RepositoryException {
long assetLastModification = this.asset.getLastModified();
ValueMap vm = (ValueMap)this.resource.adaptTo(ValueMap.class);
if (assetLastModification == 0) {
Calendar created = (Calendar)vm.get("jcr:created", Calendar.class);
assetLastModification = null != created ? created.getTimeInMillis() : 0;
}
return assetLastModification;
}
public String getMimeType() {
return this.asset.getMimeType() != null ? this.asset.getMimeType() : null;
}
@Override
public int getReferencesSize(AssetReferenceResolver customResolver) throws RepositoryException {
int assetReferencesCount = 0;
Collection resultSet = new ReferenceSearch().search(this.resource.getResourceResolver(), this.assetNode.getPath()).values();
if (resultSet != null) {
Iterator it = resultSet.iterator();
while (it.hasNext()) {
ReferenceSearch.Info infoItem = (ReferenceSearch.Info)it.next();
Resource contentRes = infoItem.getPage().getContentResource();
if (contentRes == null || !contentRes.getResourceType().equals("mac/components/boardpage")) continue;
it.remove();
}
assetReferencesCount = resultSet.size();
}
if (customResolver != null) {
HashMap referencesInfo = new HashMap();
assetReferencesCount += customResolver.getReferences(this.assetNode.getPath(), this.resource.getResourceResolver()).size();
}
return assetReferencesCount;
}
@Override
public int getCommentsSize() throws RepositoryException {
int commentsCount = 0;
if (this.assetNode.hasNode("jcr:content/comments")) {
Node commentsNode = this.assetNode.getNode("jcr:content/comments");
NodeIterator it = commentsNode.getNodes();
while (it.hasNext()) {
++commentsCount;
it.next();
}
}
return commentsCount;
}
public String getThumbnailUrl() {
String thumbnailUrl = "";
Rendition thumbnailRendition = UIHelper.getBestfitRendition((Asset)this.asset, (int)319);
thumbnailUrl = this.getMimeType() != null && this.getMimeType().startsWith("Multipart/Related") ? this.asset.getPath() + ".folderthumbnail.jpg" : (thumbnailRendition != null ? thumbnailRendition.getPath() : this.asset.getPath() + ".thumb.319.319.png");
thumbnailUrl = Text.escapePath((String)thumbnailUrl);
return thumbnailUrl;
}
public long getWidth() throws RepositoryException {
long width = 0;
if (this.assetNode.hasNode("jcr:content/metadata")) {
Node metadataNode = this.assetNode.getNode("jcr:content/metadata");
try {
width = Long.valueOf(DamUtil.getValue((Node)metadataNode, (String)"tiff:ImageWidth", (String)DamUtil.getValue((Node)metadataNode, (String)"exif:PixelXDimension", (String)"")));
}
catch (Exception e) {
// empty catch block
}
}
return width;
}
public long getHeight() throws RepositoryException {
long height = 0;
if (this.assetNode.hasNode("jcr:content/metadata")) {
Node metadataNode = this.assetNode.getNode("jcr:content/metadata");
try {
height = Long.valueOf(DamUtil.getValue((Node)metadataNode, (String)"tiff:ImageLength", (String)DamUtil.getValue((Node)metadataNode, (String)"exif:PixelYDimension", (String)"")));
}
catch (Exception e) {
// empty catch block
}
}
return height;
}
public String getSize() {
return this.asset.getOriginal() != null ? UIHelper.getSizeLabel((double)this.asset.getOriginal().getSize()) : "0.0 B";
}
public String getResolution() throws RepositoryException {
long width = this.getWidth();
long height = this.getHeight();
return width != 0 && height != 0 ? "" + width + " x " + height : "";
}
public String getDescription() throws RepositoryException {
Asset asset = (Asset)this.resource.adaptTo(Asset.class);
if (asset != null) {
Node metadataNode = this.assetNode.getNode("jcr:content/metadata");
return DamUtil.getValue((Node)metadataNode, (String)"dc:description", (String)this.resource.getName());
}
LabeledResource lr = (LabeledResource)this.resource.adaptTo(LabeledResource.class);
if (lr != null) {
return lr.getDescription() != null ? lr.getDescription() : "";
}
return this.resource.getName();
}
public String getParamJSON() throws RepositoryException {
JSONObject param = new JSONObject();
if (this.assetNode.hasNode("jcr:content/metadata")) {
Node metadataNode = this.assetNode.getNode("jcr:content/metadata");
try {
String imageMap = DamUtil.getValue((Node)metadataNode, (String)"imageMap", (String)"");
if (StringUtils.isEmpty((String)imageMap)) {
param.put("./imageMap@Delete", (Object)"");
} else {
param.put("./imageMap", (Object)imageMap);
}
param.put("./imageCrop@Delete", (Object)"");
param.put("./imageRotate@Delete", (Object)"");
}
catch (Exception e) {
// empty catch block
}
}
return param.toString();
}
}