1 package org
.argeo
.cms
.ui
.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
.ui
.CmsImageManager
;
24 import org
.argeo
.jcr
.JcrUtils
;
25 import org
.eclipse
.rap
.rwt
.RWT
;
26 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
27 import org
.eclipse
.rap
.rwt
.widgets
.FileUpload
;
28 import org
.eclipse
.swt
.graphics
.Image
;
29 import org
.eclipse
.swt
.graphics
.ImageData
;
30 import org
.eclipse
.swt
.graphics
.Point
;
31 import org
.eclipse
.swt
.widgets
.Control
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.eclipse
.swt
.widgets
.Label
;
35 /** Manages only public images so far. */
36 public class DefaultImageManager
implements CmsImageManager
{
37 private final static Log log
= LogFactory
.getLog(DefaultImageManager
.class);
38 // private MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
40 public Boolean
load(Node node
, Control control
, Point preferredSize
) throws RepositoryException
{
41 Point imageSize
= getImageSize(node
);
44 if (preferredSize
== null || imageSize
.x
== 0 || imageSize
.y
== 0
45 || (preferredSize
.x
== 0 && preferredSize
.y
== 0)) {
46 if (imageSize
.x
!= 0 && imageSize
.y
!= 0) {
47 // actual image size if completely known
50 // no image if not completely known
51 size
= resizeTo(NO_IMAGE_SIZE
, preferredSize
!= null ? preferredSize
: imageSize
);
52 imgTag
= CmsUiUtils
.noImg(size
);
55 } else if (preferredSize
.x
!= 0 && preferredSize
.y
!= 0) {
56 // given size if completely provided
60 // image is completely known
61 assert imageSize
.x
!= 0 && imageSize
.y
!= 0;
62 // one and only one of the dimension as been specified
63 assert preferredSize
.x
== 0 || preferredSize
.y
== 0;
64 size
= resizeTo(imageSize
, preferredSize
);
67 boolean loaded
= false;
71 if (control
instanceof Label
) {
73 // IMAGE RETRIEVED HERE
74 imgTag
= getImageTag(node
, size
);
77 imgTag
= CmsUiUtils
.noImg(size
);
82 Label lbl
= (Label
) control
;
85 } else if (control
instanceof FileUpload
) {
86 FileUpload lbl
= (FileUpload
) control
;
87 lbl
.setImage(CmsUiUtils
.noImage(size
));
96 private Point
resizeTo(Point orig
, Point constraints
) {
97 if (constraints
.x
!= 0 && constraints
.y
!= 0) {
99 } else if (constraints
.x
== 0 && constraints
.y
== 0) {
101 } else if (constraints
.y
== 0) {// force width
102 return new Point(constraints
.x
, scale(orig
.y
, orig
.x
, constraints
.x
));
103 } else if (constraints
.x
== 0) {// force height
104 return new Point(scale(orig
.x
, orig
.y
, constraints
.y
), constraints
.y
);
106 throw new IllegalArgumentException("Cannot resize " + orig
+ " to " + constraints
);
109 private int scale(int origDimension
, int otherDimension
, int otherConstraint
) {
110 return Math
.round(origDimension
* divide(otherConstraint
, otherDimension
));
113 private float divide(int a
, int b
) {
114 return ((float) a
) / ((float) b
);
117 public Point
getImageSize(Node node
) throws RepositoryException
{
119 Image image
= getSwtImage(node
);
120 return new Point(image
.getBounds().width
, image
.getBounds().height
);
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 CmsUiUtils
.imgBuilder(url
, width
, height
);
150 /** @return null if not available */
152 public String
getImageUrl(Node node
) throws RepositoryException
{
153 return CmsUiUtils
.getDataPath(node
);
156 protected String
getResourceName(Node node
) throws RepositoryException
{
157 String workspace
= node
.getSession().getWorkspace().getName();
158 if (node
.hasNode(JCR_CONTENT
))
159 return workspace
+ '_' + node
.getNode(JCR_CONTENT
).getIdentifier();
161 return workspace
+ '_' + node
.getIdentifier();
164 public Binary
getImageBinary(Node node
) throws RepositoryException
{
165 if (node
.isNodeType(NT_FILE
)) {
166 return node
.getNode(JCR_CONTENT
).getProperty(JCR_DATA
).getBinary();
172 public Image
getSwtImage(Node node
) throws RepositoryException
{
173 InputStream inputStream
= null;
174 Binary binary
= getImageBinary(node
);
178 inputStream
= binary
.getStream();
179 return new Image(Display
.getCurrent(), inputStream
);
181 IOUtils
.closeQuietly(inputStream
);
182 JcrUtils
.closeQuietly(binary
);
187 public String
uploadImage(Node context
, Node parentNode
, String fileName
, InputStream in
, String contentType
)
188 throws RepositoryException
{
189 InputStream inputStream
= null;
191 String previousResourceName
= null;
192 if (parentNode
.hasNode(fileName
)) {
193 Node node
= parentNode
.getNode(fileName
);
194 previousResourceName
= getResourceName(node
);
195 if (node
.hasNode(JCR_CONTENT
)) {
196 node
.getNode(JCR_CONTENT
).remove();
197 node
.addNode(JCR_CONTENT
, NT_RESOURCE
);
201 byte[] arr
= IOUtils
.toByteArray(in
);
202 Node fileNode
= JcrUtils
.copyBytesAsFile(parentNode
, fileName
, arr
);
203 inputStream
= new ByteArrayInputStream(arr
);
204 ImageData id
= new ImageData(inputStream
);
205 processNewImageFile(context
, fileNode
, id
);
207 String mime
= contentType
!= null ? contentType
: Files
.probeContentType(Paths
.get(fileName
));
209 fileNode
.getNode(JCR_CONTENT
).setProperty(Property
.JCR_MIMETYPE
, mime
);
211 fileNode
.getSession().save();
213 // reset resource manager
214 ResourceManager resourceManager
= RWT
.getResourceManager();
215 if (previousResourceName
!= null && resourceManager
.isRegistered(previousResourceName
)) {
216 resourceManager
.unregister(previousResourceName
);
217 if (log
.isDebugEnabled())
218 log
.debug("Unregistered image " + previousResourceName
);
220 return CmsUiUtils
.getDataPath(fileNode
);
221 } catch (IOException e
) {
222 throw new RuntimeException("Cannot upload image " + fileName
+ " in " + parentNode
, e
);
224 IOUtils
.closeQuietly(inputStream
);
228 /** Does nothing by default. */
229 protected void processNewImageFile(Node context
, Node fileNode
, ImageData id
)
230 throws RepositoryException
, IOException
{