DMImageProcess.java
10.9 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.dam.dm.process.api.PTiffManager
* com.adobe.cq.dam.dm.process.api.PTiffMetadata
* com.adobe.cq.dam.dm.process.api.PTiffRendition
* com.adobe.granite.workflow.WorkflowException
* com.adobe.granite.workflow.WorkflowSession
* com.adobe.granite.workflow.exec.WorkItem
* com.adobe.granite.workflow.exec.WorkflowData
* com.adobe.granite.workflow.exec.WorkflowProcess
* com.adobe.granite.workflow.metadata.MetaDataMap
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.Rendition
* com.day.cq.dam.commons.util.DamUtil
* com.scene7.is.util.RectInt
* com.scene7.is.util.SizeInt
* com.scene7.is.util.collections.CollectionUtil
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* 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.ModifiableValueMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceMetadata
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.jcr.resource.JcrPropertyMap
* org.jetbrains.annotations.NotNull
* org.jetbrains.annotations.Nullable
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dam.dm.process.workflow;
import com.adobe.cq.dam.dm.process.api.PTiffManager;
import com.adobe.cq.dam.dm.process.api.PTiffMetadata;
import com.adobe.cq.dam.dm.process.api.PTiffRendition;
import com.adobe.cq.dam.dm.process.workflow.InsetCropParser;
import com.adobe.cq.dam.dm.process.workflow.InvalidInsetCrop;
import com.adobe.cq.dam.s7imaging.impl.ProfilerUtil;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.commons.util.DamUtil;
import com.scene7.is.util.RectInt;
import com.scene7.is.util.SizeInt;
import com.scene7.is.util.collections.CollectionUtil;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.ModifiableValueMap;
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.api.resource.ValueMap;
import org.apache.sling.jcr.resource.JcrPropertyMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Property(name="process.label", value={"Processes assets for use with Dynamic Media Imaging"})
public class DMImageProcess
implements WorkflowProcess {
private static final Logger log = LoggerFactory.getLogger(DMImageProcess.class);
private static final String OPTION_CROP = "crop";
@Reference
private PTiffManager ptiffManager;
private static final Set<String> IGNORED_PROPERTIES = CollectionUtil.hashSetOf((Object[])new String[]{"jcr:primaryType"});
public void execute(WorkItem item, WorkflowSession wfSession, MetaDataMap args) throws WorkflowException {
if (!this.ptiffManager.isEnabled()) {
log.debug("Skipping because Dynamic Media is not enabled");
return;
}
Session session = (Session)wfSession.adaptTo(Session.class);
Asset asset = DMImageProcess.getAssetFromPayload(wfSession, item.getWorkflowData());
if (asset == null) {
log.debug("Skipping work item, since it's not an asset: {}", (Object)item);
return;
}
ValueMap options = DMImageProcess.getProcessingOptions(session, asset);
ProfilerUtil profilerUtil = new ProfilerUtil();
try {
asset.setBatchMode(true);
profilerUtil.start("createPTiffRendition");
PTiffRendition ptiff = this.createPTiffRendition(asset, options);
if (ptiff != null) {
profilerUtil.start("updateImageModifiers");
this.updateImageModifiers(asset, ptiff, options);
profilerUtil.start("updateLegacyProperties");
this.updateLegacyProperties(asset, ptiff);
}
profilerUtil.start("session.save");
session.save();
log.debug("Completed: {} {}", (Object)asset.getPath(), (Object)profilerUtil.print());
}
catch (RepositoryException e) {
throw new WorkflowException((Throwable)e);
}
catch (IOException e) {
throw new WorkflowException((Throwable)e);
}
}
@Nullable
private PTiffRendition createPTiffRendition(@NotNull Asset asset, @NotNull ValueMap options) throws IOException {
PTiffRendition newPTiff = null;
Rendition rendition = asset.getImagePreviewRendition();
if (rendition != null) {
PTiffRendition existingPTiff = this.ptiffManager.getPTiffRendition(asset);
if (!DMImageProcess.regeneratePTiff(rendition, existingPTiff)) {
log.debug("PTIFF rendition is up-to-date, skipping PTIFF generation: {}", (Object)asset.getPath());
return existingPTiff;
}
newPTiff = this.ptiffManager.createPTiffRendition(rendition, asset, options);
}
if (newPTiff == null) {
log.debug("Unable to generate PTIFF for asset: {}", (Object)asset.getPath());
this.ptiffManager.removePTiffRendition(asset);
}
return newPTiff;
}
private static boolean regeneratePTiff(@NotNull Rendition image, @Nullable PTiffRendition ptiff) {
if (ptiff == null) {
return true;
}
long originalLastMod = image.getResourceMetadata().getModificationTime();
if (originalLastMod < 0) {
return true;
}
long ptiffLastMod = ptiff.getResourceMetadata().getModificationTime();
return ptiffLastMod < originalLastMod;
}
private void updateImageModifiers(@NotNull Asset asset, @NotNull PTiffRendition ptiff, @NotNull ValueMap options) throws IOException {
ModifiableValueMap modifiers;
PTiffMetadata metadata = ptiff.getMetadata();
SizeInt tiffDim = SizeInt.sizeInt((int)metadata.getWidth(), (int)metadata.getHeight());
Resource jcrContent = DMImageProcess.getJcrContent(asset);
ResourceResolver resolver = jcrContent.getResourceResolver();
Resource modifierFolder = jcrContent.getChild("modifier");
if (modifierFolder == null) {
modifierFolder = resolver.create(jcrContent, "modifier", Collections.singletonMap("jcr:primaryType", "nt:unstructured"));
}
if ((modifiers = (ModifiableValueMap)modifierFolder.adaptTo(ModifiableValueMap.class)) == null) {
throw new IOException("Unable to modify properties of " + modifierFolder.getPath());
}
if (metadata.isOriginal()) {
DMImageProcess.addCropModifier(asset, tiffDim, options, modifiers);
} else {
modifiers.remove((Object)"crop");
}
if (!DMImageProcess.hasModifiers((ValueMap)modifiers)) {
resolver.delete(modifierFolder);
}
}
private static void addCropModifier(@NotNull Asset asset, @NotNull SizeInt tiffDim, @NotNull ValueMap options, @NotNull ModifiableValueMap modifiers) {
try {
String cropInset = (String)options.get("crop", (Object)"");
RectInt rect = RectInt.rectInt((SizeInt)tiffDim);
RectInt crop = InsetCropParser.parseInsetCrop(cropInset, rect);
if (crop.equals((Object)RectInt.rectInt())) {
log.warn("Crop ignored since it results in to an empty image " + asset.getPath());
modifiers.remove((Object)"crop");
} else if (crop.equals((Object)rect)) {
modifiers.remove((Object)"crop");
} else {
modifiers.put((Object)"crop", (Object)crop.toString());
}
}
catch (InvalidInsetCrop e) {
log.warn("Crop ignored while processing asset " + asset.getPath(), (Throwable)e);
}
}
private static boolean hasModifiers(@NotNull ValueMap map) {
for (String key : map.keySet()) {
if (IGNORED_PROPERTIES.contains(key)) continue;
return true;
}
return false;
}
private void updateLegacyProperties(@NotNull Asset asset, @NotNull PTiffRendition ptiff) throws IOException {
Resource jcrContent = DMImageProcess.getJcrContent(asset);
ModifiableValueMap map = (ModifiableValueMap)jcrContent.adaptTo(ModifiableValueMap.class);
if (map == null) {
throw new IOException("Unable to modify properties of " + jcrContent.getPath());
}
if (!map.containsKey((Object)"dam:s7damType")) {
map.put((Object)"dam:s7damType", (Object)"Image");
}
}
@NotNull
private static Resource getJcrContent(@NotNull Asset asset) throws IOException {
Resource jcrContent;
Resource resource = (Resource)asset.adaptTo(Resource.class);
if (resource != null && (jcrContent = resource.getChild("jcr:content")) != null) {
return jcrContent;
}
throw new IOException("Unable to access jcr:content node under " + asset.getPath());
}
@NotNull
private static ValueMap getProcessingOptions(@NotNull Session session, @NotNull Asset asset) {
Node imageProfileNode = DamUtil.getApplicableProfile((Asset)asset, (String)"imageProfile", (Session)session);
if (imageProfileNode != null) {
return new JcrPropertyMap(imageProfileNode);
}
return ValueMap.EMPTY;
}
@Nullable
private static Asset getAssetFromPayload(@NotNull WorkflowSession wfSession, @NotNull WorkflowData data) {
ResourceResolver resolver;
Resource resource;
if (data.getPayloadType().equals("JCR_PATH") && (resource = (resolver = (ResourceResolver)wfSession.adaptTo(ResourceResolver.class)).getResource(data.getPayload().toString())) != null) {
return DamUtil.resolveToAsset((Resource)resource);
}
return null;
}
protected void bindPtiffManager(PTiffManager pTiffManager) {
this.ptiffManager = pTiffManager;
}
protected void unbindPtiffManager(PTiffManager pTiffManager) {
if (this.ptiffManager == pTiffManager) {
this.ptiffManager = null;
}
}
}