Commits

Anonymous committed 6050de5

WW-1438
- Rich text editor image upload does not handle invalid url path (windows)
- Rich text editor image upload does not handle same image filename after it is uploaded for the 2nd time or more

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@2883573baa09-0c28-0410-bef9-dab3c582ae83

Comments (0)

Files changed (2)

src/java/com/opensymphony/webwork/components/DefaultRichtexteditorConnector.java

     }
 
     protected String calculateActualServerPath(String actualServerPath, String type, String folderPath) throws Exception {
-        String path = "file://"+servletContext.getRealPath("/WEB-INF/classes"+actualServerPath);
+        String path = "file:////"+servletContext.getRealPath("/WEB-INF/classes"+actualServerPath);
+        path = path.trim();
+        path = path.replace('\\', '/'); 
         makeDirIfNotExists(path);
         path = path.endsWith("/") ? path : path+"/";
         return path+type+folderPath;
                     if (a > 100) {
                         return FileUploadResult.invalidFile();
                     }
+                    tmpFile = calculateActualServerPath(getActualServerPath(), type, virtualFolderPath)+filename+ext;
                 }
                 copyFile(newFile, new java.io.File(new URI(tmpFile)));
                 return FileUploadResult.uploadCompleteWithFilenamChanged(filename+ext);
     protected boolean makeDirIfNotExists(String path) throws URISyntaxException {
         java.io.File dir = new java.io.File(new URI(path));
         if (! dir.exists()) {
-            _log.debug("make directory "+dir);
+        	if (_log.isDebugEnabled()) {
+        		_log.debug("make directory "+dir);
+        	}
             boolean ok = dir.mkdirs();
             if (! ok) {
                 throw new WebWorkException("cannot make directory "+dir);
     protected boolean makeFileIfNotExists(String filePath) throws IOException, URISyntaxException {
         java.io.File f = new java.io.File(new URI(filePath));
         if (! f.exists()) {
-            _log.debug("creating file "+filePath);
+        	if (_log.isDebugEnabled()) {
+        		_log.debug("creating file "+filePath);
+        	}
             boolean ok = f.createNewFile();
             if (! ok) {
                 throw new WebWorkException("cannot create file "+filePath);
         FileInputStream fis = null;
         FileOutputStream fos = null;
         try {
-            _log.debug("copy file from "+from+" to "+to);
+        	if (_log.isDebugEnabled()) {
+        		_log.debug("copy file from "+from+" to "+to);
+        	}
             fis = new FileInputStream(from);
             fos = new FileOutputStream(to);
             int tmpByte = fis.read();
         if (size > 0) {
             size = (size / 100);
         }
-        _log.debug("size of file "+file+" is "+size+" kb");
+        if (_log.isDebugEnabled()) {
+        	_log.debug("size of file "+file+" is "+size+" kb");
+        }
         return size;
     }
 }

src/test/com/opensymphony/webwork/components/DefaultRichtexteditorConnectorTest.java

+/*
+ * Copyright (c) 2002-2007 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.webwork.components;
+
+import java.net.URISyntaxException;
+
+import org.springframework.mock.web.MockServletContext;
+
+import junit.framework.TestCase;
+
+/**
+ * @author tmjee
+ * @version $Date$ $Id$
+ */
+public class DefaultRichtexteditorConnectorTest extends TestCase {
+
+	public void testCalculateActualServerPath() throws Exception {
+		MockServletContext servletContext = new MockServletContext() {
+			public String getRealPath(String path) {
+				return "C:\\eclipse\\workspace\\myProject\\web"+path;
+			}
+		};
+		DefaultRichtexteditorConnector connector = new DefaultRichtexteditorConnector() {
+			private static final long serialVersionUID = -3436164274268111578L;
+
+			protected boolean makeDirIfNotExists(String path) throws URISyntaxException {
+				return true;
+			}
+		};
+		connector.setServletContext(servletContext);
+		String result = connector.calculateActualServerPath("\\com\\opensymphony\\webwork\\static\\richtexteditor\\data", "image", "/testing");
+		assertEquals("file:////C:/eclipse/workspace/myProject/web/WEB-INF/classes/com/opensymphony/webwork/static/richtexteditor/data/image/testing", result);
+	}
+}