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
.net
.MalformedURLException
;
14 import java
.nio
.file
.Files
;
15 import java
.nio
.file
.Paths
;
17 import javax
.jcr
.Binary
;
18 import javax
.jcr
.Node
;
19 import javax
.jcr
.Property
;
20 import javax
.jcr
.RepositoryException
;
22 import org
.apache
.commons
.io
.IOUtils
;
23 import org
.apache
.commons
.logging
.Log
;
24 import org
.apache
.commons
.logging
.LogFactory
;
25 import org
.argeo
.cms
.ui
.CmsImageManager
;
26 import org
.argeo
.jcr
.JcrUtils
;
27 import org
.eclipse
.rap
.rwt
.RWT
;
28 import org
.eclipse
.rap
.rwt
.service
.ResourceManager
;
29 import org
.eclipse
.rap
.rwt
.widgets
.FileUpload
;
30 import org
.eclipse
.swt
.graphics
.Image
;
31 import org
.eclipse
.swt
.graphics
.ImageData
;
32 import org
.eclipse
.swt
.graphics
.Point
;
33 import org
.eclipse
.swt
.widgets
.Control
;
34 import org
.eclipse
.swt
.widgets
.Display
;
35 import org
.eclipse
.swt
.widgets
.Label
;
37 /** Manages only public images so far. */
38 public class DefaultImageManager
implements CmsImageManager
{
39 private final static Log log
= LogFactory
.getLog(DefaultImageManager
.class);
40 // private MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
42 public Boolean
load(Node node
, Control control
, Point preferredSize
) throws RepositoryException
{
43 Point imageSize
= getImageSize(node
);
46 if (preferredSize
== null || imageSize
.x
== 0 || imageSize
.y
== 0
47 || (preferredSize
.x
== 0 && preferredSize
.y
== 0)) {
48 if (imageSize
.x
!= 0 && imageSize
.y
!= 0) {
49 // actual image size if completely known
52 // no image if not completely known
53 size
= resizeTo(NO_IMAGE_SIZE
, preferredSize
!= null ? preferredSize
: imageSize
);
54 imgTag
= CmsUiUtils
.noImg(size
);
57 } else if (preferredSize
.x
!= 0 && preferredSize
.y
!= 0) {
58 // given size if completely provided
62 // image is completely known
63 assert imageSize
.x
!= 0 && imageSize
.y
!= 0;
64 // one and only one of the dimension as been specified
65 assert preferredSize
.x
== 0 || preferredSize
.y
== 0;
66 size
= resizeTo(imageSize
, preferredSize
);
69 boolean loaded
= false;
73 if (control
instanceof Label
) {
75 // IMAGE RETRIEVED HERE
76 imgTag
= getImageTag(node
, size
);
79 imgTag
= CmsUiUtils
.noImg(size
);
84 Label lbl
= (Label
) control
;
87 } else if (control
instanceof FileUpload
) {
88 FileUpload lbl
= (FileUpload
) control
;
89 lbl
.setImage(CmsUiUtils
.noImage(size
));
98 private Point
resizeTo(Point orig
, Point constraints
) {
99 if (constraints
.x
!= 0 && constraints
.y
!= 0) {
101 } else if (constraints
.x
== 0 && constraints
.y
== 0) {
103 } else if (constraints
.y
== 0) {// force width
104 return new Point(constraints
.x
, scale(orig
.y
, orig
.x
, constraints
.x
));
105 } else if (constraints
.x
== 0) {// force height
106 return new Point(scale(orig
.x
, orig
.y
, constraints
.y
), constraints
.y
);
108 throw new IllegalArgumentException("Cannot resize " + orig
+ " to " + constraints
);
111 private int scale(int origDimension
, int otherDimension
, int otherConstraint
) {
112 return Math
.round(origDimension
* divide(otherConstraint
, otherDimension
));
115 private float divide(int a
, int b
) {
116 return ((float) a
) / ((float) b
);
119 public Point
getImageSize(Node node
) throws RepositoryException
{
121 Image image
= getSwtImage(node
);
122 return new Point(image
.getBounds().width
, image
.getBounds().height
);
125 /** @return null if not available */
127 public String
getImageTag(Node node
) throws RepositoryException
{
128 return getImageTag(node
, getImageSize(node
));
131 private String
getImageTag(Node node
, Point size
) throws RepositoryException
{
132 StringBuilder buf
= getImageTagBuilder(node
, size
);
135 return buf
.append("/>").toString();
138 /** @return null if not available */
140 public StringBuilder
getImageTagBuilder(Node node
, Point size
) throws RepositoryException
{
141 return getImageTagBuilder(node
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
144 /** @return null if not available */
145 private StringBuilder
getImageTagBuilder(Node node
, String width
, String height
) throws RepositoryException
{
146 String url
= getImageUrl(node
);
149 return CmsUiUtils
.imgBuilder(url
, width
, height
);
152 /** @return null if not available */
154 public String
getImageUrl(Node node
) throws RepositoryException
{
155 return CmsUiUtils
.getDataPathForUrl(node
);
158 protected String
getResourceName(Node node
) throws RepositoryException
{
159 String workspace
= node
.getSession().getWorkspace().getName();
160 if (node
.hasNode(JCR_CONTENT
))
161 return workspace
+ '_' + node
.getNode(JCR_CONTENT
).getIdentifier();
163 return workspace
+ '_' + node
.getIdentifier();
166 public Binary
getImageBinary(Node node
) throws RepositoryException
{
167 if (node
.isNodeType(NT_FILE
)) {
168 return node
.getNode(JCR_CONTENT
).getProperty(JCR_DATA
).getBinary();
174 public Image
getSwtImage(Node node
) throws RepositoryException
{
175 InputStream inputStream
= null;
176 Binary binary
= getImageBinary(node
);
180 inputStream
= binary
.getStream();
181 return new Image(Display
.getCurrent(), inputStream
);
183 IOUtils
.closeQuietly(inputStream
);
184 JcrUtils
.closeQuietly(binary
);
189 public String
uploadImage(Node context
, Node parentNode
, String fileName
, InputStream in
, String contentType
)
190 throws RepositoryException
{
191 InputStream inputStream
= null;
193 String previousResourceName
= null;
194 if (parentNode
.hasNode(fileName
)) {
195 Node node
= parentNode
.getNode(fileName
);
196 previousResourceName
= getResourceName(node
);
197 if (node
.hasNode(JCR_CONTENT
)) {
198 node
.getNode(JCR_CONTENT
).remove();
199 node
.addNode(JCR_CONTENT
, NT_RESOURCE
);
203 byte[] arr
= IOUtils
.toByteArray(in
);
204 Node fileNode
= JcrUtils
.copyBytesAsFile(parentNode
, fileName
, arr
);
205 inputStream
= new ByteArrayInputStream(arr
);
206 ImageData id
= new ImageData(inputStream
);
207 processNewImageFile(context
, fileNode
, id
);
209 String mime
= contentType
!= null ? contentType
: Files
.probeContentType(Paths
.get(fileName
));
211 fileNode
.getNode(JCR_CONTENT
).setProperty(Property
.JCR_MIMETYPE
, mime
);
213 fileNode
.getSession().save();
215 // reset resource manager
216 ResourceManager resourceManager
= RWT
.getResourceManager();
217 if (previousResourceName
!= null && resourceManager
.isRegistered(previousResourceName
)) {
218 resourceManager
.unregister(previousResourceName
);
219 if (log
.isDebugEnabled())
220 log
.debug("Unregistered image " + previousResourceName
);
222 return CmsUiUtils
.getDataPath(fileNode
);
223 } catch (IOException e
) {
224 throw new RuntimeException("Cannot upload image " + fileName
+ " in " + parentNode
, e
);
226 IOUtils
.closeQuietly(inputStream
);
230 /** Does nothing by default. */
231 protected void processNewImageFile(Node context
, Node fileNode
, ImageData id
)
232 throws RepositoryException
, IOException
{