Fix clean data path when it contains spaces.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / util / DefaultImageManager.java
index 1c6f7198413942206a04c5694a06b5073e0036b1..c9a3e2291eb1f828a63d14bb30cb9a3d20ef232e 100644 (file)
@@ -9,8 +9,13 @@ import static org.argeo.cms.ui.CmsConstants.NO_IMAGE_SIZE;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.util.StringTokenizer;
 
 import javax.jcr.Binary;
 import javax.jcr.Node;
@@ -20,7 +25,6 @@ import javax.jcr.RepositoryException;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.argeo.cms.CmsException;
 import org.argeo.cms.ui.CmsImageManager;
 import org.argeo.jcr.JcrUtils;
 import org.eclipse.rap.rwt.RWT;
@@ -104,7 +108,7 @@ public class DefaultImageManager implements CmsImageManager {
                } else if (constraints.x == 0) {// force height
                        return new Point(scale(orig.x, orig.y, constraints.y), constraints.y);
                }
-               throw new CmsException("Cannot resize " + orig + " to " + constraints);
+               throw new IllegalArgumentException("Cannot resize " + orig + " to " + constraints);
        }
 
        private int scale(int origDimension, int otherDimension, int otherConstraint) {
@@ -118,7 +122,7 @@ public class DefaultImageManager implements CmsImageManager {
        public Point getImageSize(Node node) throws RepositoryException {
                // TODO optimise
                Image image = getSwtImage(node);
-               return new Point(image.getBounds().width,image.getBounds().height);
+               return new Point(image.getBounds().width, image.getBounds().height);
        }
 
        /** @return null if not available */
@@ -151,25 +155,22 @@ public class DefaultImageManager implements CmsImageManager {
        /** @return null if not available */
        @Override
        public String getImageUrl(Node node) throws RepositoryException {
-               return CmsUiUtils.getDataPath(node);
-               // String name = getResourceName(node);
-               // ResourceManager resourceManager = RWT.getResourceManager();
-               // if (!resourceManager.isRegistered(name)) {
-               // InputStream inputStream = null;
-               // Binary binary = getImageBinary(node);
-               // if (binary == null)
-               // return null;
-               // try {
-               // inputStream = binary.getStream();
-               // resourceManager.register(name, inputStream);
-               // } finally {
-               // IOUtils.closeQuietly(inputStream);
-               // JcrUtils.closeQuietly(binary);
-               // }
-               // if (log.isTraceEnabled())
-               // log.trace("Registered image " + name);
-               // }
-               // return resourceManager.getLocation(name);
+               return getCleanDataPath(node);
+       }
+
+       /** Clean special character from the URL. */
+       protected String getCleanDataPath(Node node) throws RepositoryException {
+               String path = CmsUiUtils.getDataPath(node);
+               StringTokenizer st = new StringTokenizer(path, "/");
+               StringBuilder sb = new StringBuilder();
+               while (st.hasMoreElements()) {
+                       sb.append('/');
+                       String encoded = URLEncoder.encode(st.nextToken(), StandardCharsets.UTF_8);
+                       encoded = encoded.replace("+", "%20");
+                       sb.append(encoded);
+
+               }
+               return sb.toString();
        }
 
        protected String getResourceName(Node node) throws RepositoryException {
@@ -203,7 +204,8 @@ public class DefaultImageManager implements CmsImageManager {
        }
 
        @Override
-       public String uploadImage(Node parentNode, String fileName, InputStream in) throws RepositoryException {
+       public String uploadImage(Node context, Node parentNode, String fileName, InputStream in, String contentType)
+                       throws RepositoryException {
                InputStream inputStream = null;
                try {
                        String previousResourceName = null;
@@ -220,10 +222,12 @@ public class DefaultImageManager implements CmsImageManager {
                        Node fileNode = JcrUtils.copyBytesAsFile(parentNode, fileName, arr);
                        inputStream = new ByteArrayInputStream(arr);
                        ImageData id = new ImageData(inputStream);
-                       processNewImageFile(fileNode, id);
+                       processNewImageFile(context, fileNode, id);
 
-                       String mime = Files.probeContentType(Paths.get(fileName));
-                       fileNode.setProperty(Property.JCR_MIMETYPE, mime);
+                       String mime = contentType != null ? contentType : Files.probeContentType(Paths.get(fileName));
+                       if (mime != null) {
+                               fileNode.getNode(JCR_CONTENT).setProperty(Property.JCR_MIMETYPE, mime);
+                       }
                        fileNode.getSession().save();
 
                        // reset resource manager
@@ -233,15 +237,16 @@ public class DefaultImageManager implements CmsImageManager {
                                if (log.isDebugEnabled())
                                        log.debug("Unregistered image " + previousResourceName);
                        }
-                       return getImageUrl(fileNode);
+                       return CmsUiUtils.getDataPath(fileNode);
                } catch (IOException e) {
-                       throw new CmsException("Cannot upload image " + fileName + " in " + parentNode, e);
+                       throw new RuntimeException("Cannot upload image " + fileName + " in " + parentNode, e);
                } finally {
                        IOUtils.closeQuietly(inputStream);
                }
        }
 
-       /** Does nothign by default. */
-       protected void processNewImageFile(Node fileNode, ImageData id) throws RepositoryException, IOException {
+       /** Does nothing by default. */
+       protected void processNewImageFile(Node context, Node fileNode, ImageData id)
+                       throws RepositoryException, IOException {
        }
 }