RenditionMakerImpl.java
14.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.gfx.Gfx
* com.adobe.cq.gfx.Instructions
* com.adobe.cq.gfx.Layer
* com.adobe.cq.gfx.Plan
* com.adobe.xmp.XMPMeta
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* com.day.cq.dam.api.handler.xmp.XMPHandler
* com.day.cq.dam.api.handler.xmp.XMPWriteBackOptions
* com.day.cq.dam.api.renditions.RenditionMaker
* com.day.cq.dam.api.renditions.RenditionTemplate
* com.day.cq.dam.commons.metadata.SimpleXmpToJcrMetadataBuilder
* com.day.cq.dam.commons.util.DamUtil
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.io.IOUtils
* org.apache.commons.lang.ArrayUtils
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceMetadata
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.mime.MimeTypeService
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.core.impl;
import com.adobe.cq.gfx.Gfx;
import com.adobe.cq.gfx.Instructions;
import com.adobe.cq.gfx.Layer;
import com.adobe.cq.gfx.Plan;
import com.adobe.xmp.XMPMeta;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.handler.xmp.XMPHandler;
import com.day.cq.dam.api.handler.xmp.XMPWriteBackOptions;
import com.day.cq.dam.api.renditions.RenditionMaker;
import com.day.cq.dam.api.renditions.RenditionTemplate;
import com.day.cq.dam.commons.metadata.SimpleXmpToJcrMetadataBuilder;
import com.day.cq.dam.commons.util.DamUtil;
import com.day.cq.dam.core.impl.handler.xmp.XMPWriteBackOptionsImpl;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Dictionary;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.mime.MimeTypeService;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="Adobe CQ DAM Rendition Maker", description="Adobe CQ DAM Rendition Maker")
@Service
public class RenditionMakerImpl
implements RenditionMaker {
private static final Logger log = LoggerFactory.getLogger(RenditionMakerImpl.class);
public static final String PREFIX_WEB_RENDITION = "cq5dam.web.";
private static final boolean DEFAULT_PROPAGATE_XMP = false;
@Property(boolValue={0}, label="Propagate XMP", description="Propagate XMP from asset metadata to renditions")
private static final String PROPAGATE_XMP = "xmp.propagate";
private static final String XMP_PROP_DELIM = " ";
private static final String PS_AUX_ISO = "http://ns.adobe.com/exif/1.0/aux/ ISO";
private static final String[] DEFAULT_XMP_EXCLUDES = new String[]{"http://ns.adobe.com/exif/1.0/aux/ ISO"};
@Property(value={"http://ns.adobe.com/exif/1.0/aux/ ISO"}, cardinality=1, label="XMP Excludes", description="XMP properties to skip on propagation. Format: <namespace> <property>")
private static final String XMP_EXCLUDES = "xmp.excludes";
@Reference
private Gfx gfx;
@Reference
private MimeTypeService mimeTypeService;
@Reference
protected XMPHandler xmpHandler;
private boolean propagateXMP;
private String[] xmpExcludes;
@Activate
private void activate(ComponentContext ctx) {
Dictionary cfg = ctx.getProperties();
this.propagateXMP = PropertiesUtil.toBoolean(cfg.get("xmp.propagate"), (boolean)false);
this.xmpExcludes = PropertiesUtil.toStringArray(cfg.get("xmp.excludes"), (String[])DEFAULT_XMP_EXCLUDES);
}
public RenditionTemplate createThumbnailTemplate(Asset asset, int width, int height, boolean center) {
return this.createThumbnailTemplate(asset, null, width, height, center);
}
public RenditionTemplate createThumbnailTemplate(Rendition rendition, int width, int height, boolean doCenter) {
return this.createThumbnailTemplate(rendition.getAsset(), rendition, width, height, doCenter);
}
public RenditionTemplate createWebRenditionTemplate(Asset asset, int width, int height, int quality, String mimeType, String[] mimeTypesToKeep) {
return this.createWebRenditionTemplate(asset, null, width, height, quality, mimeType, mimeTypesToKeep);
}
public RenditionTemplate createWebRenditionTemplate(Rendition rendition, int width, int height, int quality, String mimeType, String[] mimeTypesToKeep) {
return this.createWebRenditionTemplate(rendition.getAsset(), rendition, width, height, quality, mimeType, mimeTypesToKeep);
}
public /* varargs */ List<Rendition> generateRenditions(Asset asset, RenditionTemplate ... templates) {
boolean oldBatchMode = asset.isBatchMode();
asset.setBatchMode(true);
ArrayList<Rendition> renditions = new ArrayList<Rendition>();
for (RenditionTemplate cfg : templates) {
Rendition rendition = cfg.apply(asset);
if (rendition == null) continue;
log.info("generated rendition: {}", (Object)rendition.getPath());
renditions.add(rendition);
}
if (renditions.isEmpty()) {
asset.setBatchMode(oldBatchMode);
return renditions;
}
if (this.propagateXMP) {
this.propagateXMP(asset, renditions);
}
asset.setBatchMode(oldBatchMode);
try {
((Node)asset.adaptTo(Node.class)).getSession().save();
}
catch (RepositoryException e) {
log.error("Error while generating renditions for asset " + asset.getPath(), (Throwable)e);
}
return renditions;
}
private RenditionTemplate createThumbnailTemplate(Asset asset, Rendition rendition, int width, int height, boolean doCenter) {
String[] arrstring;
PlanBasedTemplate template = new PlanBasedTemplate();
if (doCenter) {
String[] arrstring2 = new String[1];
arrstring = arrstring2;
arrstring2[0] = "margin";
} else {
arrstring = null;
}
template.renditionName = DamUtil.getThumbnailName((int)width, (int)height, (String[])arrstring);
template.mimeType = "image/png";
template.plan = this.gfx.createPlan();
template.plan.layer(0).set("src", (Object)(rendition != null ? rendition.getPath() : asset.getPath()));
RenditionMakerImpl.applyThumbnail(width, height, doCenter, template.mimeType, template.plan);
return template;
}
private RenditionTemplate createWebRenditionTemplate(Asset asset, Rendition rendition, int width, int height, int quality, String mimeType, String[] mimeTypesToKeep) {
PlanBasedTemplate template = new PlanBasedTemplate();
String assetMimeType = asset.getMimeType();
template.mimeType = ArrayUtils.contains((Object[])mimeTypesToKeep, (Object)assetMimeType) ? assetMimeType : mimeType;
template.renditionName = "cq5dam.web." + width + "." + height + "." + this.mimeTypeService.getExtension(template.mimeType);
template.plan = this.gfx.createPlan();
template.plan.layer(0).set("src", (Object)(rendition != null ? rendition.getPath() : asset.getPath()));
this.applyWebRendition(width, height, quality, template.mimeType, template.plan);
return template;
}
private static void applyThumbnail(int width, int height, boolean doCenter, String mimeType, Plan plan) {
Instructions global = plan.view();
global.set("wid", (Object)width);
global.set("hei", (Object)height);
global.set("fit", (Object)(doCenter ? "fit,0" : "constrain,0"));
String fmt = StringUtils.substringAfter((String)mimeType, (String)"/");
if ("png".equals(fmt) || "gif".equals(fmt) || "tif".equals(fmt)) {
fmt = fmt + "-alpha";
}
global.set("fmt", (Object)fmt);
}
private void applyWebRendition(int width, int height, int quality, String mimeType, Plan plan) {
Instructions global = plan.view();
global.set("wid", (Object)width);
global.set("hei", (Object)height);
global.set("fit", (Object)"constrain,0");
if ("image/jpg".equals(mimeType) || "image/jpeg".equals(mimeType)) {
global.set("qlt", (Object)quality);
} else if ("image/gif".equals(mimeType)) {
global.set("quantize", (Object)("adaptive,diffuse," + quality));
}
String fmt = StringUtils.substringAfter((String)mimeType, (String)"/");
if ("png".equals(fmt) || "gif".equals(fmt) || "tif".equals(fmt)) {
fmt = fmt + "-alpha";
}
global.set("fmt", (Object)fmt);
}
private static boolean needsUpdate(Asset asset, String renditionName) {
Rendition rendition = asset.getRendition(renditionName);
if (rendition == null) {
return true;
}
long originalLastMod = asset.getOriginal().getResourceMetadata().getModificationTime();
if (originalLastMod < 0) {
return true;
}
long renditionLastMod = rendition.getResourceMetadata().getModificationTime();
return renditionLastMod < originalLastMod;
}
private void propagateXMP(Asset asset, List<Rendition> renditions) {
if (this.xmpHandler != null && this.xmpHandler.isSupported(asset.getMimeType())) {
XMPWriteBackOptionsImpl writeBackOptions = new XMPWriteBackOptionsImpl();
writeBackOptions.createVersion(false);
writeBackOptions.setRenditions(new HashSet<Rendition>(renditions));
this.writeXmp(asset, writeBackOptions, this.xmpHandler);
}
}
private synchronized void writeXmp(Asset asset, XMPWriteBackOptions writeBackOptions, XMPHandler xmpHandler) {
try {
Node assetNode = (Node)asset.adaptTo(Node.class);
Node metadataNode = assetNode.getNode("jcr:content/metadata");
SimpleXmpToJcrMetadataBuilder metadataAdapter = new SimpleXmpToJcrMetadataBuilder();
XMPMeta xmpMeta = metadataAdapter.getXmpFromJcr(metadataNode);
for (String exclude : this.xmpExcludes) {
String[] split = exclude.split(" ", 2);
if (split.length == 2) {
String ns = split[0];
String prop = split[1];
if (!xmpMeta.doesPropertyExist(ns, prop)) continue;
log.debug("Excluding XMP property: {} {}", (Object)ns, (Object)prop);
xmpMeta.deleteProperty(ns, prop);
continue;
}
log.warn("Configuration xmp.excludes has entry that does not have the format '<namespace> <property>': {}", (Object)exclude);
}
xmpHandler.writeXmp(asset, xmpMeta, writeBackOptions);
}
catch (Throwable e) {
if (log.isDebugEnabled()) {
log.debug("Could not propagate xmp to renditions of asset: '" + asset.getPath() + "'", e);
}
log.warn("Could not propagate xmp to renditions of asset '{}': {}", (Object)asset.getPath(), (Object)e.getMessage());
}
}
protected void bindGfx(Gfx gfx) {
this.gfx = gfx;
}
protected void unbindGfx(Gfx gfx) {
if (this.gfx == gfx) {
this.gfx = null;
}
}
protected void bindMimeTypeService(MimeTypeService mimeTypeService) {
this.mimeTypeService = mimeTypeService;
}
protected void unbindMimeTypeService(MimeTypeService mimeTypeService) {
if (this.mimeTypeService == mimeTypeService) {
this.mimeTypeService = null;
}
}
protected void bindXmpHandler(XMPHandler xMPHandler) {
this.xmpHandler = xMPHandler;
}
protected void unbindXmpHandler(XMPHandler xMPHandler) {
if (this.xmpHandler == xMPHandler) {
this.xmpHandler = null;
}
}
private class PlanBasedTemplate
implements RenditionTemplate {
public Plan plan;
public String renditionName;
public String mimeType;
private PlanBasedTemplate() {
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public Rendition apply(Asset asset) {
if (RenditionMakerImpl.needsUpdate(asset, this.renditionName)) {
InputStream stream = null;
try {
stream = RenditionMakerImpl.this.gfx.render(this.plan, ((Resource)asset.adaptTo(Resource.class)).getResourceResolver());
if (stream == null) {
log.info("cannot generate rendition. stream not found [{}]", (Object)this.renditionName);
Rendition rendition = null;
return rendition;
}
Rendition rendition = asset.addRendition(this.renditionName, stream, this.mimeType);
return rendition;
}
finally {
IOUtils.closeQuietly((InputStream)stream);
}
}
log.info("skipping regeneration of rendition '{}' because original is older: {}", (Object)this.renditionName, (Object)asset.getPath());
return null;
}
}
}