Index: lams_build/lib/lams/lams.jar
===================================================================
diff -u -rab0ebee2c0b38fee4f7b362d6893f9d81bc3ba55 -rcac4b20509606b942a7cd762ebadd2674c3983ab
Binary files differ
Index: lams_central/conf/language/lams/ApplicationResources_en_AU.properties
===================================================================
diff -u -re63e2792149fe13694cbdf273d181cc8e5694565 -rcac4b20509606b942a7cd762ebadd2674c3983ab
--- lams_central/conf/language/lams/ApplicationResources_en_AU.properties (.../ApplicationResources_en_AU.properties) (revision e63e2792149fe13694cbdf273d181cc8e5694565)
+++ lams_central/conf/language/lams/ApplicationResources_en_AU.properties (.../ApplicationResources_en_AU.properties) (revision cac4b20509606b942a7cd762ebadd2674c3983ab)
@@ -738,5 +738,7 @@
tour.end.title =End Of Tour
tour.end.content =Thank you for taking the tour. To restart the tour, click the Tour button again.
+errors.maxfilesize =The uploaded file has exceeded the maximum file size limit of {0} bytes.
+
#======= End labels: Exported 731 labels for en AU =====
Index: lams_central/src/java/org/lamsfoundation/lams/web/LAMSConnectorServlet.java
===================================================================
diff -u -r5773f84ed608838de3521ecde87c52f3c72d478c -rcac4b20509606b942a7cd762ebadd2674c3983ab
--- lams_central/src/java/org/lamsfoundation/lams/web/LAMSConnectorServlet.java (.../LAMSConnectorServlet.java) (revision 5773f84ed608838de3521ecde87c52f3c72d478c)
+++ lams_central/src/java/org/lamsfoundation/lams/web/LAMSConnectorServlet.java (.../LAMSConnectorServlet.java) (revision cac4b20509606b942a7cd762ebadd2674c3983ab)
@@ -34,13 +34,18 @@
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
+import org.apache.struts.action.ActionMessage;
import org.lamsfoundation.lams.authoring.web.AuthoringConstants;
import org.lamsfoundation.lams.usermanagement.dto.UserDTO;
import org.lamsfoundation.lams.util.Configuration;
import org.lamsfoundation.lams.util.ConfigurationKeys;
import org.lamsfoundation.lams.util.FileUtil;
+import org.lamsfoundation.lams.util.FileValidatorUtil;
+import org.lamsfoundation.lams.util.MessageService;
import org.lamsfoundation.lams.web.session.SessionManager;
import org.lamsfoundation.lams.web.util.AttributeNames;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.WebApplicationContextUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -65,14 +70,7 @@
*
* @author Simone Chiaretta (simo@users.sourceforge.net)
* @author Mitchell Seaton
- *
- *
- *
- *
- *
- *
*/
-
public class LAMSConnectorServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(LAMSConnectorServlet.class);
@@ -82,15 +80,20 @@
private String realBaseDir;
private String lamsContextPath;
-
+
+ private static MessageService messageService;
+
/**
* Initialize the servlet.
* Retrieve from the servlet configuration the "baseDir" which is the root of the file repository:
* If not specified the value of "/UserFiles/" will be used.
- *
*/
@Override
public void init() throws ServletException {
+ WebApplicationContext ctx = WebApplicationContextUtils
+ .getRequiredWebApplicationContext(this.getServletContext());
+ messageService = (MessageService) ctx.getBean("centralMessageService");
+
LAMSConnectorServlet.baseDir = getInitParameter("baseDir");
debug = (new Boolean(getInitParameter("debug"))).booleanValue() && log.isDebugEnabled();
@@ -323,8 +326,25 @@
fileNameLong = fileNameLong.replace('\\', '/');
String[] pathParts = fileNameLong.split("/");
String fileName = pathParts[pathParts.length - 1];
+
+ // validate file size
+ ActionMessage maxFilesizeExceededMessage = FileValidatorUtil.validateFileSize(uplFile, true);
+ if (maxFilesizeExceededMessage != null) {
+ //assign fileName an error message to be shown on a client side
+ fileName = messageService.getMessage(maxFilesizeExceededMessage.getKey(),
+ maxFilesizeExceededMessage.getValues());
+ retVal.append("1");
- if (FileUtil.isExtensionAllowed(fileType, fileName)) {
+ // validate file extension
+ } else if (!FileUtil.isExtensionAllowed(fileType, fileName)) {
+ if (LAMSConnectorServlet.debug) {
+ log.debug("File extension is prohibited for upload " + fileName);
+ }
+
+ //will generate client-side alert message 'Invalid file type'
+ retVal.append("204");
+
+ } else {
File pathToSave = new File(validCurrentDirPath, fileName);
int counter = 1;
@@ -347,14 +367,6 @@
if (LAMSConnectorServlet.debug) {
log.debug("File save finished");
}
-
- } else {
- if (LAMSConnectorServlet.debug) {
- log.debug("File extension is prohibited for upload " + fileName);
- }
-
- //will generate client-side alert message 'Invalid file type'
- retVal.append("204");
}
return fileName;
Index: lams_central/src/java/org/lamsfoundation/lams/web/LAMSUploadServlet.java
===================================================================
diff -u -r5773f84ed608838de3521ecde87c52f3c72d478c -rcac4b20509606b942a7cd762ebadd2674c3983ab
--- lams_central/src/java/org/lamsfoundation/lams/web/LAMSUploadServlet.java (.../LAMSUploadServlet.java) (revision 5773f84ed608838de3521ecde87c52f3c72d478c)
+++ lams_central/src/java/org/lamsfoundation/lams/web/LAMSUploadServlet.java (.../LAMSUploadServlet.java) (revision cac4b20509606b942a7cd762ebadd2674c3983ab)
@@ -21,8 +21,13 @@
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;
+import org.apache.struts.action.ActionMessage;
import org.lamsfoundation.lams.util.FileUtil;
+import org.lamsfoundation.lams.util.FileValidatorUtil;
+import org.lamsfoundation.lams.util.MessageService;
import org.lamsfoundation.lams.util.UploadFileUtil;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* Servlet to upload files.
@@ -37,16 +42,19 @@
*
* @author Simone Chiaretta (simo@users.sourceforge.net)
* @author Mitchell Seaton
- *
- *
- *
- *
*/
-
public class LAMSUploadServlet extends HttpServlet {
private static final long serialVersionUID = 7839808388592495717L;
private static final Logger log = Logger.getLogger(LAMSUploadServlet.class);
+
+ private static MessageService messageService;
+
+ public void init() {
+ WebApplicationContext ctx = WebApplicationContextUtils
+ .getRequiredWebApplicationContext(this.getServletContext());
+ messageService = (MessageService) ctx.getBean("centralMessageService");
+ }
/**
* Manage the Upload requests.
@@ -102,7 +110,17 @@
String[] pathParts = fileNameLong.split("/");
String fileName = pathParts[pathParts.length - 1];
- if (FileUtil.isExtensionAllowed(fileType, fileName)) {
+ // validate file size
+ ActionMessage maxFilesizeExceededMessage = FileValidatorUtil.validateFileSize(uplFile, true);
+ if (maxFilesizeExceededMessage != null) {
+ returnMessage = messageService.getMessage(maxFilesizeExceededMessage.getKey(),
+ maxFilesizeExceededMessage.getValues());
+
+ // validate file extension
+ } else if (!FileUtil.isExtensionAllowed(fileType, fileName)) {
+ returnMessage = "Invalid file type";
+
+ } else {
File uploadDir = UploadFileUtil.getUploadDir(currentFolderStr, fileType);
fileName = UploadFileUtil.getUploadFileName(uploadDir, fileName);
newName = fileName;
@@ -115,12 +133,10 @@
if (LAMSUploadServlet.log.isDebugEnabled()) {
LAMSUploadServlet.log.debug("Uploaded file to " + destinationFile.getAbsolutePath());
}
- } else {
- returnMessage = "Invalid file type";
}
} catch (Exception e) {
LAMSUploadServlet.log.error(e);
- returnMessage = "Error while uploading file";
+ returnMessage = "Error while uploading file: " + e.getMessage();
}
}
Index: lams_common/src/java/org/lamsfoundation/lams/util/FileValidatorUtil.java
===================================================================
diff -u -r51fb2a37254f24bb2a805d4ffd54482c779f43fa -rcac4b20509606b942a7cd762ebadd2674c3983ab
--- lams_common/src/java/org/lamsfoundation/lams/util/FileValidatorUtil.java (.../FileValidatorUtil.java) (revision 51fb2a37254f24bb2a805d4ffd54482c779f43fa)
+++ lams_common/src/java/org/lamsfoundation/lams/util/FileValidatorUtil.java (.../FileValidatorUtil.java) (revision cac4b20509606b942a7cd762ebadd2674c3983ab)
@@ -27,6 +27,7 @@
import javax.servlet.http.HttpServletRequest;
+import org.apache.commons.fileupload.FileItem;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorAction;
@@ -125,8 +126,27 @@
}
return FileValidatorUtil.validateFileSize(fileSize, largeFile, errorKey, errors);
-
}
+
+ /**
+ *
+ * @param file
+ * @param largeFile
+ * @return return null if file size is below max filesize, otherwise, return error message
+ */
+ public static ActionMessage validateFileSize(FileItem file, boolean largeFile) {
+ int fileSize = (int) file.getSize();
+ ActionMessages errors = new ActionMessages();
+
+ ActionMessage errorMessage = null;
+ boolean isMaxFilesizeExceeded = !FileValidatorUtil.validateFileSize(fileSize, largeFile,
+ ActionMessages.GLOBAL_MESSAGE, errors);
+ if (isMaxFilesizeExceeded) {
+ errorMessage = (ActionMessage)errors.get().next();
+ }
+
+ return errorMessage;
+ }
private static boolean validateFileSize(int fileSize, boolean largeFile, String errorKey, ActionMessages errors) {
float maxFileSize = largeFile ? Configuration.getAsInt(ConfigurationKeys.UPLOAD_FILE_LARGE_MAX_SIZE)