ThumbnailHelper.java
7.97 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.image.Layer
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.ValueFactory
* org.apache.commons.io.FileUtils
* org.apache.commons.io.IOUtils
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.resource.collection.ResourceCollection
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.core.impl;
import com.day.cq.dam.core.impl.ui.preview.PreviewOptions;
import com.day.image.Layer;
import java.awt.Color;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Collections;
import java.util.Map;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.resource.collection.ResourceCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ThumbnailHelper {
private static final Logger log = LoggerFactory.getLogger(ThumbnailHelper.class);
private static final String MIMETYPE = "image/jpeg";
private static final String MANUAL_THUMBNAIL = "manualThumbnail.jpg";
private static final String PARAMETER_MANUAL_THUMBNAIL = "coverImage";
private static final String PARAMETER_REMOVE_MANUAL_THUMBNAIL = "removemanualthumbnail";
public static void updateManualThumbnail(SlingHttpServletRequest request, Resource res, PreviewOptions options) throws IOException {
boolean removeManualThumbnail = Boolean.valueOf(request.getParameter("removemanualthumbnail"));
if (removeManualThumbnail) {
ThumbnailHelper.removeManualThumbnail(res);
} else {
RequestParameter thumbnailParameter = request.getRequestParameter("coverImage");
if (thumbnailParameter != null && thumbnailParameter.getSize() > 0) {
InputStream thumbnailStream = thumbnailParameter.getInputStream();
ThumbnailHelper.setManualThumbnail(res, thumbnailStream, options);
}
}
}
private static void setManualThumbnail(Resource res, InputStream thumbnailStream, PreviewOptions options) throws IOException {
if (res != null) {
Resource targetResource = ThumbnailHelper.getTargetResource(res);
ThumbnailHelper.saveThumbnail(targetResource, thumbnailStream, options);
}
}
private static Resource getTargetResource(Resource res) {
Resource targetResource = null;
ResourceCollection coll = (ResourceCollection)res.adaptTo(ResourceCollection.class);
targetResource = coll != null ? res : res.getChild("jcr:content");
return targetResource;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private static void saveThumbnail(Resource targetResource, InputStream thumbnailStream, PreviewOptions options) throws IOException {
FileOutputStream itout = null;
File imageTmpFile = null;
FileInputStream is = null;
try {
imageTmpFile = File.createTempFile("image", ".tmp");
Layer thumbnailLayer = new Layer(thumbnailStream);
itout = new FileOutputStream(imageTmpFile);
thumbnailLayer = ThumbnailHelper.adjustLayer(thumbnailLayer, options);
thumbnailLayer.write("image/jpeg", 1.0, (OutputStream)itout);
is = new FileInputStream(imageTmpFile);
Resource assetContent = ThumbnailHelper.createFolderThumbnailContent(targetResource);
Node node = (Node)assetContent.adaptTo(Node.class);
if (!node.hasNode("jcr:content")) {
log.debug("Unable to write thumbnail " + node.getPath());
return;
}
Node thumbnailContent = node.getNode("jcr:content");
thumbnailContent.setProperty("jcr:data", node.getSession().getValueFactory().createBinary((InputStream)is));
thumbnailContent.setProperty("width", (long)options.getWidth());
thumbnailContent.setProperty("height", (long)options.getHeight());
thumbnailContent.setProperty("bgcolor", (long)options.getBgcolor().getRGB());
thumbnailContent.setProperty("jcr:lastModified", Calendar.getInstance());
}
catch (RepositoryException re) {
log.warn("could not save thumbnail" + re.getMessage());
}
finally {
IOUtils.closeQuietly((InputStream)is);
IOUtils.closeQuietly((OutputStream)itout);
FileUtils.deleteQuietly((File)imageTmpFile);
}
}
private static Resource createResource(Resource resource, String name, String type) throws PersistenceException {
ResourceResolver resourceResolver = resource.getResourceResolver();
Resource thumbnailResource = resourceResolver.getResource(resource.getPath() + "/" + name);
if (null != thumbnailResource) {
return thumbnailResource;
}
return resourceResolver.create(resource, name, Collections.singletonMap("jcr:primaryType", type));
}
private static Resource createFolderThumbnailContent(Resource resource) throws PersistenceException {
Resource folderThumbnail = ThumbnailHelper.createResource(resource, "manualThumbnail.jpg", "nt:file");
ThumbnailHelper.createResource(folderThumbnail, "jcr:content", "nt:unstructured");
return folderThumbnail;
}
private static Layer adjustLayer(Layer layer, PreviewOptions options) {
Rectangle rect;
double aspectRatioThumb;
double aspectRatioImage = (double)layer.getWidth() / (double)(layer.getHeight() + 1);
if (aspectRatioImage < (aspectRatioThumb = (double)options.getWidth() / (double)(options.getHeight() + 1))) {
int width = Math.min(options.getWidth(), layer.getWidth());
layer.resize(width, layer.getHeight() * width / (layer.getWidth() + 1));
rect = new Rectangle(0, Math.max((layer.getHeight() - options.getHeight()) / 2, 0), options.getWidth(), options.getHeight());
} else {
int height = Math.min(options.getHeight(), layer.getHeight());
layer.resize(layer.getWidth() * height / (layer.getHeight() + 1), height);
rect = new Rectangle(Math.max((layer.getWidth() - options.getWidth()) / 2, 0), 0, options.getWidth(), options.getHeight());
}
layer.crop((Rectangle2D)rect);
int x = (options.getWidth() - layer.getWidth()) / 2;
int y = (options.getHeight() - layer.getHeight()) / 2;
Layer bgLayer = new Layer(options.getWidth(), options.getHeight(), (Paint)options.getBgcolor());
layer.setX(x);
layer.setY(y);
bgLayer.merge(layer);
return bgLayer;
}
private static void removeManualThumbnail(Resource res) throws PersistenceException {
Resource targetResource = ThumbnailHelper.getTargetResource(res);
ResourceResolver resourceResolver = targetResource.getResourceResolver();
Resource thumbnailResource = resourceResolver.getResource(targetResource.getPath() + "/" + "manualThumbnail.jpg");
if (null != thumbnailResource) {
resourceResolver.delete(thumbnailResource);
}
}
}