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 477d65be7894f8bc092f1dffd68fd72fe87f6ad1..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) {
@@ -151,7 +155,22 @@ public class DefaultImageManager implements CmsImageManager {
        /** @return null if not available */
        @Override
        public String getImageUrl(Node node) throws RepositoryException {
-               return CmsUiUtils.getDataPath(node);
+               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 {
@@ -218,9 +237,9 @@ 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);
                }