NIHVIVO-161 - If the user tries to upload a file that is too large, return a friendly error message.

This commit is contained in:
jeb228 2010-07-13 14:26:28 +00:00
parent 6cffc40c33
commit b0e72fb0e9
2 changed files with 27 additions and 15 deletions

View file

@ -17,7 +17,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -46,15 +45,20 @@ public class ImageUploadController extends FreeMarkerHttpServlet {
public static final String DUMMY_THUMBNAIL_URL = "/images/dummyImages/person.thumbnail.jpg";
/** Limit file size to 50 megabytes. */
public static final int MAXIMUM_FILE_SIZE = 50 * 1024 * 1024;
/** Limit file size to 6 megabytes. */
public static final int MAXIMUM_FILE_SIZE = 6 * 1024 * 1024;
/** Generated thumbnails will be this big. */
public static final int THUMBNAIL_HEIGHT = 115;
public static final int THUMBNAIL_WIDTH = 115;
/** The form field that tells what we are doing: uploading? deleting? */
public static final String PARAMETER_ACTION = "action";
/** The form field that identifies the Individual. */
public static final String PARAMETER_ENTITY_URI = "entityUri";
/** The form field of the uploaded file; use as a key to the FileItem map. */
public static final String PARAMETER_UPLOADED_FILE = "datafile";
public static final String ACTION_SAVE = "save";
@ -133,21 +137,18 @@ public class ImageUploadController extends FreeMarkerHttpServlet {
throws IOException, ServletException {
try {
FileUploadServletRequest parsedRequest = FileUploadServletRequest
.parseRequest(request, MAXIMUM_FILE_SIZE);
// Parse the multi-part request.
request = FileUploadServletRequest.parseRequest(request,
MAXIMUM_FILE_SIZE);
if (log.isTraceEnabled()) {
dumpRequestDetails(parsedRequest);
}
} catch (FileUploadException e) {
// Swallow the exception here. Test for FILE_ITEM_MAP later.
log.error("Failed to parse the multi-part HTTP request", e);
dumpRequestDetails(request);
}
try {
// do setup defined in VitroHttpServlet
// Do setup defined in VitroHttpServlet
setup(request);
VitroRequest vreq = new VitroRequest(request);
ResponseValues values = buildTheResponse(vreq);
// They can't do this if they aren't logged in.
@ -402,8 +403,8 @@ public class ImageUploadController extends FreeMarkerHttpServlet {
* if this is null, then all URLs lead to the welcome page.
*/
private TemplateResponseValues showAddImagePage(Individual entity) {
String formAction = (entity == null) ? "" : formAction(
entity.getURI(), ACTION_UPLOAD);
String formAction = (entity == null) ? "" : formAction(entity.getURI(),
ACTION_UPLOAD);
String cancelUrl = (entity == null) ? "" : displayPageUrl(entity
.getURI());

View file

@ -2,9 +2,12 @@
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import static edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadController.MAXIMUM_FILE_SIZE;
import static edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadController.PARAMETER_UPLOADED_FILE;
import static edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadController.THUMBNAIL_HEIGHT;
import static edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploadController.THUMBNAIL_WIDTH;
import static edu.cornell.mannlib.vitro.webapp.filestorage.uploadrequest.FileUploadServletRequest.FILE_ITEM_MAP;
import static edu.cornell.mannlib.vitro.webapp.filestorage.uploadrequest.FileUploadServletRequest.FILE_UPLOAD_EXCEPTION;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
@ -86,8 +89,16 @@ public class ImageUploadHelper {
@SuppressWarnings("unchecked")
FileItem validateImageFromRequest(HttpServletRequest request)
throws UserMistakeException {
Object exception = request.getAttribute(FILE_UPLOAD_EXCEPTION);
if (exception != null) {
int limit = MAXIMUM_FILE_SIZE / (1024 * 1024);
throw new UserMistakeException(
"Please upload an image smaller than " + limit
+ " megabytes");
}
Map<String, List<FileItem>> map = (Map<String, List<FileItem>>) request
.getAttribute(FileUploadServletRequest.FILE_ITEM_MAP);
.getAttribute(FILE_ITEM_MAP);
if (map == null) {
throw new IllegalStateException("Failed to parse the "
+ "multi-part request for uploading an image.");