1 package org
.argeo
.cms
.ui
.internal
;
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
;
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
;
16 import javax
.jcr
.Binary
;
17 import javax
.jcr
.Node
;
18 import javax
.jcr
.Property
;
19 import javax
.jcr
.RepositoryException
;
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
;
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();
45 public Boolean
load(Node node
, Control control
, Point preferredSize
) throws RepositoryException
{
46 Point imageSize
= getImageSize(node
);
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
55 // no image if not completely known
56 size
= resizeTo(NO_IMAGE_SIZE
, preferredSize
!= null ? preferredSize
: imageSize
);
57 imgTag
= CmsUtils
.noImg(size
);
60 } else if (preferredSize
.x
!= 0 && preferredSize
.y
!= 0) {
61 // given size if completely provided
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
);
72 boolean loaded
= false;
76 if (control
instanceof Label
) {
78 // IMAGE RETRIEVED HERE
79 imgTag
= getImageTag(node
, size
);
82 imgTag
= CmsUtils
.noImg(size
);
87 Label lbl
= (Label
) control
;
90 } else if (control
instanceof FileUpload
) {
91 FileUpload lbl
= (FileUpload
) control
;
92 lbl
.setImage(CmsUtils
.noImage(size
));
101 private Point
resizeTo(Point orig
, Point constraints
) {
102 if (constraints
.x
!= 0 && constraints
.y
!= 0) {
104 } else if (constraints
.x
== 0 && constraints
.y
== 0) {
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
);
111 throw new CmsException("Cannot resize " + orig
+ " to " + constraints
);
114 private int scale(int origDimension
, int otherDimension
, int otherConstraint
) {
115 return Math
.round(origDimension
* divide(otherConstraint
, otherDimension
));
118 private float divide(int a
, int b
) {
119 return ((float) a
) / ((float) b
);
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);
127 /** @return null if not available */
129 public String
getImageTag(Node node
) throws RepositoryException
{
130 return getImageTag(node
, getImageSize(node
));
133 private String
getImageTag(Node node
, Point size
) throws RepositoryException
{
134 StringBuilder buf
= getImageTagBuilder(node
, size
);
137 return buf
.append("/>").toString();
140 /** @return null if not available */
142 public StringBuilder
getImageTagBuilder(Node node
, Point size
) throws RepositoryException
{
143 return getImageTagBuilder(node
, Integer
.toString(size
.x
), Integer
.toString(size
.y
));
146 /** @return null if not available */
147 private StringBuilder
getImageTagBuilder(Node node
, String width
, String height
) throws RepositoryException
{
148 String url
= getImageUrl(node
);
151 return CmsUtils
.imgBuilder(url
, width
, height
);
154 /** @return null if not available */
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)
166 // inputStream = binary.getStream();
167 // resourceManager.register(name, inputStream);
169 // IOUtils.closeQuietly(inputStream);
170 // JcrUtils.closeQuietly(binary);
172 // if (log.isTraceEnabled())
173 // log.trace("Registered image " + name);
175 // return resourceManager.getLocation(name);
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();
183 return workspace
+ '_' + node
.getIdentifier();
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();
196 public Image
getSwtImage(Node node
) throws RepositoryException
{
197 InputStream inputStream
= null;
198 Binary binary
= getImageBinary(node
);
202 inputStream
= binary
.getStream();
203 return new Image(Display
.getCurrent(), inputStream
);
205 IOUtils
.closeQuietly(inputStream
);
206 JcrUtils
.closeQuietly(binary
);
211 public String
uploadImage(Node parentNode
, String fileName
, InputStream in
) throws RepositoryException
{
212 InputStream inputStream
= null;
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
);
224 byte[] arr
= IOUtils
.toByteArray(in
);
225 Node fileNode
= JcrUtils
.copyBytesAsFile(parentNode
, fileName
, arr
);
226 fileNode
.addMixin(CmsTypes
.CMS_IMAGE
);
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();
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
);
243 return getImageUrl(fileNode
);
244 } catch (IOException e
) {
245 throw new CmsException("Cannot upload image " + fileName
+ " in " + parentNode
, e
);
247 IOUtils
.closeQuietly(inputStream
);