1 package org
.argeo
.cms
.util
;
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
.ui
.CmsConstants
.NO_IMAGE_SIZE
;
9 import java
.io
.ByteArrayInputStream
;
10 import java
.io
.IOException
;
11 import java
.io
.InputStream
;
12 import java
.nio
.file
.Files
;
13 import java
.nio
.file
.Paths
;
15 import javax
.jcr
.Binary
;
16 import javax
.jcr
.Node
;
17 import javax
.jcr
.Property
;
18 import javax
.jcr
.RepositoryException
;
20 import org
.apache
.commons
.io
.IOUtils
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.cms
.CmsException
;
24 import org
.argeo
.cms
.ui
.CmsImageManager
;
25 import org
.argeo
.jcr
.JcrUtils
;
26 import org
.eclipse
.rap
.rwt
.RWT
;
27 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
28 import org
.eclipse
.rap
.rwt
.widgets
.FileUpload
;
29 import org
.eclipse
.swt
.graphics
.Image
;
30 import org
.eclipse
.swt
.graphics
.ImageData
;
31 import org
.eclipse
.swt
.graphics
.Point
;
32 import org
.eclipse
.swt
.widgets
.Control
;
33 import org
.eclipse
.swt
.widgets
.Display
;
34 import org
.eclipse
.swt
.widgets
.Label
;
36 /** Manages only public images so far. */
37 public class DefaultImageManager
implements CmsImageManager
{
38 private final static Log log
= LogFactory
.getLog(DefaultImageManager
.class);
39 // private MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
41 public Boolean
load(Node node
, Control control
, Point preferredSize
) throws RepositoryException
{
42 Point imageSize
= getImageSize(node
);
45 if (preferredSize
== null || imageSize
.x
== 0 || imageSize
.y
== 0
46 || (preferredSize
.x
== 0 && preferredSize
.y
== 0)) {
47 if (imageSize
.x
!= 0 && imageSize
.y
!= 0) {
48 // actual image size if completely known
51 // no image if not completely known
52 size
= resizeTo(NO_IMAGE_SIZE
, preferredSize
!= null ? preferredSize
: imageSize
);
53 imgTag
= CmsUtils
.noImg(size
);
56 } else if (preferredSize
.x
!= 0 && preferredSize
.y
!= 0) {
57 // given size if completely provided
61 // image is completely known
62 assert imageSize
.x
!= 0 && imageSize
.y
!= 0;
63 // one and only one of the dimension as been specified
64 assert preferredSize
.x
== 0 || preferredSize
.y
== 0;
65 size
= resizeTo(imageSize
, preferredSize
);
68 boolean loaded
= false;
72 if (control
instanceof Label
) {
74 // IMAGE RETRIEVED HERE
75 imgTag
= getImageTag(node
, size
);
78 imgTag
= CmsUtils
.noImg(size
);
83 Label lbl
= (Label
) control
;
86 } else if (control
instanceof FileUpload
) {
87 FileUpload lbl
= (FileUpload
) control
;
88 lbl
.setImage(CmsUtils
.noImage(size
));
97 private Point
resizeTo(Point orig
, Point constraints
) {
98 if (constraints
.x
!= 0 && constraints
.y
!= 0) {
100 } else if (constraints
.x
== 0 && constraints
.y
== 0) {
102 } else if (constraints
.y
== 0) {// force width
103 return new Point(constraints
.x
, scale(orig
.y
, orig
.x
, constraints
.x
));
104 } else if (constraints
.x
== 0) {// force height
105 return new Point(scale(orig
.x
, orig
.y
, constraints
.y
), constraints
.y
);
107 throw new CmsException("Cannot resize " + orig
+ " to " + constraints
);
110 private int scale(int origDimension
, int otherDimension
, int otherConstraint
) {
111 return Math
.round(origDimension
* divide(otherConstraint
, otherDimension
));
114 private float divide(int a
, int b
) {
115 return ((float) a
) / ((float) b
);
118 public Point
getImageSize(Node node
) throws RepositoryException
{
119 // TODO load the SWT image ?
120 return new Point(0, 0);
123 /** @return null if not available */
125 public String
getImageTag(Node node
) throws RepositoryException
{
126 return getImageTag(node
, getImageSize(node
));
129 private String
getImageTag(Node node
, Point size
) throws RepositoryException
{
130 StringBuilder buf
= getImageTagBuilder(node
, size
);
133 return buf
.append("/>").toString();
136 /** @return null if not available */
138 public StringBuilder
getImageTagBuilder(Node node
, Point size
) throws RepositoryException
{
139 return getImageTagBuilder(node
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
142 /** @return null if not available */
143 private StringBuilder
getImageTagBuilder(Node node
, String width
, String height
) throws RepositoryException
{
144 String url
= getImageUrl(node
);
147 return CmsUtils
.imgBuilder(url
, width
, height
);
150 /** @return null if not available */
152 public String
getImageUrl(Node node
) throws RepositoryException
{
153 return CmsUtils
.getDataPath(node
);
154 // String name = getResourceName(node);
155 // ResourceManager resourceManager = RWT.getResourceManager();
156 // if (!resourceManager.isRegistered(name)) {
157 // InputStream inputStream = null;
158 // Binary binary = getImageBinary(node);
159 // if (binary == null)
162 // inputStream = binary.getStream();
163 // resourceManager.register(name, inputStream);
165 // IOUtils.closeQuietly(inputStream);
166 // JcrUtils.closeQuietly(binary);
168 // if (log.isTraceEnabled())
169 // log.trace("Registered image " + name);
171 // return resourceManager.getLocation(name);
174 protected String
getResourceName(Node node
) throws RepositoryException
{
175 String workspace
= node
.getSession().getWorkspace().getName();
176 if (node
.hasNode(JCR_CONTENT
))
177 return workspace
+ '_' + node
.getNode(JCR_CONTENT
).getIdentifier();
179 return workspace
+ '_' + node
.getIdentifier();
182 public Binary
getImageBinary(Node node
) throws RepositoryException
{
183 if (node
.isNodeType(NT_FILE
)) {
184 return node
.getNode(JCR_CONTENT
).getProperty(JCR_DATA
).getBinary();
190 public Image
getSwtImage(Node node
) throws RepositoryException
{
191 InputStream inputStream
= null;
192 Binary binary
= getImageBinary(node
);
196 inputStream
= binary
.getStream();
197 return new Image(Display
.getCurrent(), inputStream
);
199 IOUtils
.closeQuietly(inputStream
);
200 JcrUtils
.closeQuietly(binary
);
205 public String
uploadImage(Node parentNode
, String fileName
, InputStream in
) throws RepositoryException
{
206 InputStream inputStream
= null;
208 String previousResourceName
= null;
209 if (parentNode
.hasNode(fileName
)) {
210 Node node
= parentNode
.getNode(fileName
);
211 previousResourceName
= getResourceName(node
);
212 if (node
.hasNode(JCR_CONTENT
)) {
213 node
.getNode(JCR_CONTENT
).remove();
214 node
.addNode(JCR_CONTENT
, NT_RESOURCE
);
218 byte[] arr
= IOUtils
.toByteArray(in
);
219 Node fileNode
= JcrUtils
.copyBytesAsFile(parentNode
, fileName
, arr
);
220 inputStream
= new ByteArrayInputStream(arr
);
221 ImageData id
= new ImageData(inputStream
);
222 processNewImageFile(fileNode
, id
);
224 String mime
= Files
.probeContentType(Paths
.get(fileName
));
225 fileNode
.setProperty(Property
.JCR_MIMETYPE
, mime
);
226 fileNode
.getSession().save();
228 // reset resource manager
229 ResourceManager resourceManager
= RWT
.getResourceManager();
230 if (previousResourceName
!= null && resourceManager
.isRegistered(previousResourceName
)) {
231 resourceManager
.unregister(previousResourceName
);
232 if (log
.isDebugEnabled())
233 log
.debug("Unregistered image " + previousResourceName
);
235 return getImageUrl(fileNode
);
236 } catch (IOException e
) {
237 throw new CmsException("Cannot upload image " + fileName
+ " in " + parentNode
, e
);
239 IOUtils
.closeQuietly(inputStream
);
243 /** Does nothign by default. */
244 protected void processNewImageFile(Node fileNode
, ImageData id
) throws RepositoryException
, IOException
{