]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/internal/ImageManagerImpl.java
Adapt to changes with third parties.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / internal / ImageManagerImpl.java
1 package org.argeo.cms.ui.internal;
2
3 import static javax.jcr.Node.JCR_CONTENT;
4 import static javax.jcr.Property.JCR_DATA;
5 import static javax.jcr.nodetype.NodeType.NT_FILE;
6 import static javax.jcr.nodetype.NodeType.NT_RESOURCE;
7 import static org.argeo.cms.CmsTypes.CMS_STYLED;
8 import static org.argeo.cms.ui.CmsConstants.NO_IMAGE_SIZE;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.nio.file.Files;
14 import java.nio.file.Paths;
15
16 import javax.jcr.Binary;
17 import javax.jcr.Node;
18 import javax.jcr.Property;
19 import javax.jcr.RepositoryException;
20
21 import org.apache.commons.io.IOUtils;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.cms.CmsException;
25 import org.argeo.cms.CmsNames;
26 import org.argeo.cms.CmsTypes;
27 import org.argeo.cms.ui.CmsImageManager;
28 import org.argeo.cms.util.CmsUtils;
29 import org.argeo.jcr.JcrUtils;
30 import org.eclipse.rap.rwt.RWT;
31 import org.eclipse.rap.rwt.service.ResourceManager;
32 import org.eclipse.rap.rwt.widgets.FileUpload;
33 import org.eclipse.swt.graphics.Image;
34 import org.eclipse.swt.graphics.ImageData;
35 import org.eclipse.swt.graphics.Point;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.Label;
39
40 /** Manages only public images so far. */
41 public class ImageManagerImpl implements CmsImageManager, CmsNames {
42 private final static Log log = LogFactory.getLog(ImageManagerImpl.class);
43 // private MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
44
45 public Boolean load(Node node, Control control, Point preferredSize) throws RepositoryException {
46 Point imageSize = getImageSize(node);
47 Point size;
48 String imgTag = null;
49 if (preferredSize == null || imageSize.x == 0 || imageSize.y == 0
50 || (preferredSize.x == 0 && preferredSize.y == 0)) {
51 if (imageSize.x != 0 && imageSize.y != 0) {
52 // actual image size if completely known
53 size = imageSize;
54 } else {
55 // no image if not completely known
56 size = resizeTo(NO_IMAGE_SIZE, preferredSize != null ? preferredSize : imageSize);
57 imgTag = CmsUtils.noImg(size);
58 }
59
60 } else if (preferredSize.x != 0 && preferredSize.y != 0) {
61 // given size if completely provided
62 size = preferredSize;
63 } else {
64 // at this stage :
65 // image is completely known
66 assert imageSize.x != 0 && imageSize.y != 0;
67 // one and only one of the dimension as been specified
68 assert preferredSize.x == 0 || preferredSize.y == 0;
69 size = resizeTo(imageSize, preferredSize);
70 }
71
72 boolean loaded = false;
73 if (control == null)
74 return loaded;
75
76 if (control instanceof Label) {
77 if (imgTag == null) {
78 // IMAGE RETRIEVED HERE
79 imgTag = getImageTag(node, size);
80 //
81 if (imgTag == null)
82 imgTag = CmsUtils.noImg(size);
83 else
84 loaded = true;
85 }
86
87 Label lbl = (Label) control;
88 lbl.setText(imgTag);
89 // lbl.setSize(size);
90 } else if (control instanceof FileUpload) {
91 FileUpload lbl = (FileUpload) control;
92 lbl.setImage(CmsUtils.noImage(size));
93 lbl.setSize(size);
94 return loaded;
95 } else
96 loaded = false;
97
98 return loaded;
99 }
100
101 private Point resizeTo(Point orig, Point constraints) {
102 if (constraints.x != 0 && constraints.y != 0) {
103 return constraints;
104 } else if (constraints.x == 0 && constraints.y == 0) {
105 return orig;
106 } else if (constraints.y == 0) {// force width
107 return new Point(constraints.x, scale(orig.y, orig.x, constraints.x));
108 } else if (constraints.x == 0) {// force height
109 return new Point(scale(orig.x, orig.y, constraints.y), constraints.y);
110 }
111 throw new CmsException("Cannot resize " + orig + " to " + constraints);
112 }
113
114 private int scale(int origDimension, int otherDimension, int otherConstraint) {
115 return Math.round(origDimension * divide(otherConstraint, otherDimension));
116 }
117
118 private float divide(int a, int b) {
119 return ((float) a) / ((float) b);
120 }
121
122 public Point getImageSize(Node node) throws RepositoryException {
123 return new Point(node.hasProperty(CMS_IMAGE_WIDTH) ? (int) node.getProperty(CMS_IMAGE_WIDTH).getLong() : 0,
124 node.hasProperty(CMS_IMAGE_WIDTH) ? (int) node.getProperty(CMS_IMAGE_HEIGHT).getLong() : 0);
125 }
126
127 /** @return null if not available */
128 @Override
129 public String getImageTag(Node node) throws RepositoryException {
130 return getImageTag(node, getImageSize(node));
131 }
132
133 private String getImageTag(Node node, Point size) throws RepositoryException {
134 StringBuilder buf = getImageTagBuilder(node, size);
135 if (buf == null)
136 return null;
137 return buf.append("/>").toString();
138 }
139
140 /** @return null if not available */
141 @Override
142 public StringBuilder getImageTagBuilder(Node node, Point size) throws RepositoryException {
143 return getImageTagBuilder(node, Integer.toString(size.x), Integer.toString(size.y));
144 }
145
146 /** @return null if not available */
147 private StringBuilder getImageTagBuilder(Node node, String width, String height) throws RepositoryException {
148 String url = getImageUrl(node);
149 if (url == null)
150 return null;
151 return CmsUtils.imgBuilder(url, width, height);
152 }
153
154 /** @return null if not available */
155 @Override
156 public String getImageUrl(Node node) throws RepositoryException {
157 return CmsUtils.getDataPath(node);
158 // String name = getResourceName(node);
159 // ResourceManager resourceManager = RWT.getResourceManager();
160 // if (!resourceManager.isRegistered(name)) {
161 // InputStream inputStream = null;
162 // Binary binary = getImageBinary(node);
163 // if (binary == null)
164 // return null;
165 // try {
166 // inputStream = binary.getStream();
167 // resourceManager.register(name, inputStream);
168 // } finally {
169 // IOUtils.closeQuietly(inputStream);
170 // JcrUtils.closeQuietly(binary);
171 // }
172 // if (log.isTraceEnabled())
173 // log.trace("Registered image " + name);
174 // }
175 // return resourceManager.getLocation(name);
176 }
177
178 protected String getResourceName(Node node) throws RepositoryException {
179 String workspace = node.getSession().getWorkspace().getName();
180 if (node.hasNode(JCR_CONTENT))
181 return workspace + '_' + node.getNode(JCR_CONTENT).getIdentifier();
182 else
183 return workspace + '_' + node.getIdentifier();
184 }
185
186 public Binary getImageBinary(Node node) throws RepositoryException {
187 if (node.isNodeType(NT_FILE))
188 return node.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary();
189 else if (node.isNodeType(CMS_STYLED) && node.hasProperty(CMS_DATA)) {
190 return node.getProperty(CMS_DATA).getBinary();
191 } else {
192 return null;
193 }
194 }
195
196 public Image getSwtImage(Node node) throws RepositoryException {
197 InputStream inputStream = null;
198 Binary binary = getImageBinary(node);
199 if (binary == null)
200 return null;
201 try {
202 inputStream = binary.getStream();
203 return new Image(Display.getCurrent(), inputStream);
204 } finally {
205 IOUtils.closeQuietly(inputStream);
206 JcrUtils.closeQuietly(binary);
207 }
208 }
209
210 @Override
211 public String uploadImage(Node parentNode, String fileName, InputStream in) throws RepositoryException {
212 InputStream inputStream = null;
213 try {
214 String previousResourceName = null;
215 if (parentNode.hasNode(fileName)) {
216 Node node = parentNode.getNode(fileName);
217 previousResourceName = getResourceName(node);
218 if (node.hasNode(JCR_CONTENT)) {
219 node.getNode(JCR_CONTENT).remove();
220 node.addNode(JCR_CONTENT, NT_RESOURCE);
221 }
222 }
223
224 byte[] arr = IOUtils.toByteArray(in);
225 Node fileNode = JcrUtils.copyBytesAsFile(parentNode, fileName, arr);
226 fileNode.addMixin(CmsTypes.CMS_IMAGE);
227
228 inputStream = new ByteArrayInputStream(arr);
229 ImageData id = new ImageData(inputStream);
230 fileNode.setProperty(CMS_IMAGE_WIDTH, id.width);
231 fileNode.setProperty(CMS_IMAGE_HEIGHT, id.height);
232 String mime = Files.probeContentType(Paths.get(fileName));
233 fileNode.setProperty(Property.JCR_MIMETYPE, mime);
234 fileNode.getSession().save();
235
236 // reset resource manager
237 ResourceManager resourceManager = RWT.getResourceManager();
238 if (previousResourceName != null && resourceManager.isRegistered(previousResourceName)) {
239 resourceManager.unregister(previousResourceName);
240 if (log.isDebugEnabled())
241 log.debug("Unregistered image " + previousResourceName);
242 }
243 return getImageUrl(fileNode);
244 } catch (IOException e) {
245 throw new CmsException("Cannot upload image " + fileName + " in " + parentNode, e);
246 } finally {
247 IOUtils.closeQuietly(inputStream);
248 }
249 }
250 }