[richfaces-svn-commits] JBoss Rich Faces SVN: r6405 - in trunk/sandbox/ui/fileUpload/src/main: java/org/richfaces/org/jboss/seam/ui/renderkit and 2 other directories.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Thu Feb 28 08:07:51 EST 2008


Author: andrei_exadel
Date: 2008-02-28 08:07:51 -0500 (Thu, 28 Feb 2008)
New Revision: 6405

Modified:
   trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/UIFileUpload.java
   trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java
   trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java
   trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js
Log:
Smirnov's review notes implemented; server side checking for max files count

Modified: trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/UIFileUpload.java
===================================================================
--- trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/UIFileUpload.java	2008-02-28 13:05:51 UTC (rev 6404)
+++ trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/UIFileUpload.java	2008-02-28 13:07:51 UTC (rev 6405)
@@ -176,6 +176,14 @@
     public void removeFileUploadListener(FileUploadListener listener) {
         removeFacesListener(listener);
     }
+    
+    public void reset () {
+	this.localContentType = null;
+	this.localContentType = null;
+	this.localFileName = null;
+	this.localFileSize = null;
+	this.localInputStream = null;
+    }
 
     public void broadcast(FacesEvent e) {
 

Modified: trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java
===================================================================
--- trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java	2008-02-28 13:05:51 UTC (rev 6404)
+++ trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java	2008-02-28 13:07:51 UTC (rev 6405)
@@ -28,6 +28,7 @@
 import org.richfaces.component.UIProgressBar;
 import org.richfaces.event.UploadEvent;
 import org.richfaces.org.jboss.seam.ui.component.UIFileUpload;
+import org.richfaces.org.jboss.seam.web.FileUploadException;
 import org.richfaces.org.jboss.seam.web.MultipartRequest;
 import org.richfaces.renderkit.TemplateEncoderRendererBase;
 
@@ -71,6 +72,21 @@
 	}
 
     }
+    
+    /**
+     * Method checks if uploaded files count overflowed
+     * @param fileUpload
+     * @param map
+     * @return
+     */
+    private boolean checkFileCount (UIFileUpload fileUpload, Map<String, File> map) {
+	Integer max = fileUpload.getMaxFilesQuantity();
+	if (map != null && map.size() >= max) {
+	    fileUpload.reset();
+	    return false;
+	}
+	return true;
+    }
 
     /**
      * Put uploaded file into data attribute defined 
@@ -85,7 +101,11 @@
 	    Class clazz = data.getType(context.getELContext());
 	    if (clazz.isAssignableFrom(Map.class)) {
 		Map<String, File> map = (Map<String, File>) data.getValue(context.getELContext());
-		map.put(fileUpload.getLocalFileName(), file);
+		if (checkFileCount(fileUpload, map)) {
+		    map.put(fileUpload.getLocalFileName(), file);
+		}else {
+		    throw new FileUploadException("Files count overflow");
+		}
 	    }
 	}
     }

Modified: trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java
===================================================================
--- trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java	2008-02-28 13:05:51 UTC (rev 6404)
+++ trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java	2008-02-28 13:07:51 UTC (rev 6405)
@@ -147,7 +147,7 @@
 	    return fileSize;
 	}
 
-	public void createTempFile() {
+	public File createTempFile() {
 	    try {
 
 		tempFile = File.createTempFile(new UID().toString().replace(
@@ -157,6 +157,7 @@
 	    } catch (IOException ex) {
 		throw new FileUploadException("Could not create temporary file");
 	    }
+	    return tempFile;
 	}
 
 	@Override
@@ -288,6 +289,7 @@
     	encoding = request.getCharacterEncoding();
 
     	parameters = new HashMap<String, Param>();
+    	File file = null; 
 	this.percentMap = getProgressData();
 
     	try {
@@ -334,7 +336,7 @@
     											if (headers.containsKey(PARAM_FILENAME)) {
     												FileParam fp = new FileParam(paramName);
     												if (createTempFiles)
-    													fp.createTempFile();
+    												file = fp.createTempFile();
     												fp.setContentType(headers
     														.get(PARAM_CONTENT_TYPE));
     												fp.setFilename(decodeFileName(headers
@@ -413,12 +415,18 @@
     					pos = 0;
     					fillProgressInfo();
     				} else {
+    				    	if (file != null) {
+    				    	    file.delete();
+    				    	}
     					return false;
     				}
     			}
 
     			return true;
     		} else {
+    		    	if (file != null) {
+		    	    file.delete();
+		    	}
     			return false;
     		}
     	} catch (IOException ex) {

Modified: trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js
===================================================================
--- trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js	2008-02-28 13:05:51 UTC (rev 6404)
+++ trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js	2008-02-28 13:07:51 UTC (rev 6405)
@@ -54,7 +54,7 @@
 							   [
 							    new E('td',{'className':function (context) { return 'upload_font upload_name upload_table_td ' + Richfaces.evalMacro("className", context);}},
 							    		[
-							    		 new E('div',{'className':'upload_name_padding'},
+							    		 new E('div',{'className':'upload_name_padding','style':'overflow-x : hidden;'},
 							    				 [
 							    				  new ET(function (context) { return Richfaces.evalMacro("fileName", context)})
 							    				  ]),
@@ -73,7 +73,7 @@
 					    				  ]),
 			    				  new E('td',{'className':'upload_table_td'},
 			    						  [
-			    						   new E('div',{'className':'upload_font upload_scroll'})
+			    						   new E('div',{'className':'upload_font upload_scroll'},[ new T ('\u00A0') ])
 			    						   ])
 							    ])
 					   ])
@@ -95,7 +95,7 @@
 FileUploadEntry.LABELS[FileUploadEntry.INITIALIZED] = '';
 FileUploadEntry.LABELS[FileUploadEntry.READY] = '';
 FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_IN_PROGRESS] = 'uploading';
-FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_CANCELED] = 'canceled';
+FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_CANCELED] = '';
 FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_SUCCESS] = 'done';
 FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_TRANSFER_ERROR] = 'transfer_error';
 FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_SERVER_ERROR] = 'server_error';
@@ -197,14 +197,11 @@
 		Element.insert(this.statusLabel, FileUploadEntry.LABELS[newState]);
 
 		if (newState == FileUploadEntry.UPLOAD_IN_PROGRESS) {
-			//Element.insert(this.controlArea, FileUploadEntry.stopControlTemplate.getContent());
 			Element.update(this.controlArea, FileUploadEntry.stopControlTemplate.invoke('getContent',{'className': this.uploadObject.classes.FILE_ENTRY_CONTROL.ENABLED}).join(''));
-		} else if (newState == FileUploadEntry.READY) {
-			//Element.insert(this.controlArea, FileUploadEntry.cancelControlTemplate.getContent());
+		} else if (newState == FileUploadEntry.UPLOAD_SUCCESS) {
+			Element.update(this.controlArea, FileUploadEntry.clearControlTemplate.invoke('getContent',{'className': this.uploadObject.classes.FILE_ENTRY_CONTROL.ENABLED}).join(''));
+		} else {
 			Element.update(this.controlArea, FileUploadEntry.cancelControlTemplate.invoke('getContent',{'className': this.uploadObject.classes.FILE_ENTRY_CONTROL.ENABLED}).join(''));
-		} else {
-			//Element.insert(this.controlArea, FileUploadEntry.clearControlTemplate.getContent());
-			Element.update(this.controlArea, FileUploadEntry.clearControlTemplate.invoke('getContent',{'className': this.uploadObject.classes.FILE_ENTRY_CONTROL.ENABLED}).join(''));
 		}
 		
 		if (newState == FileUploadEntry.UPLOAD_SUCCESS) {
@@ -316,7 +313,6 @@
 	},
 	
 	onload: function() {
-		//if (this.iframe && this.iframe.src == 'about:blank') return;
 		var iframeDocument = this.iframe.contentWindow.document;
 		var elt = iframeDocument.getElementById('_richfaces_file_upload_stopped');
 		var restr = iframeDocument.getElementById('_richfaces_file_upload_size_restricted');
@@ -375,6 +371,8 @@
 	events: null,
 	
 	maxFileBatchSize: null,
+	
+	uploadedCount: 0,
 
 	initialize: function(id, stopScript, getFileSizeScript, progressBarId, classes, label, maxFiles, events, disabled, acceptedTypes, options) {
 		this.id = id;
@@ -515,7 +513,7 @@
 		var l = this.entries.length;
 		for (var i = 0; i < l; i++) {
 			var entry = this.entries[i];
-			if (entry.state == FileUploadEntry.READY || entry.state == FileUploadEntry.INITIALIZED) {
+			if (entry.state == FileUploadEntry.READY || entry.state == FileUploadEntry.INITIALIZED || entry.state == FileUploadEntry.UPLOAD_CANCELED) {
 
 				return entry;
 			}
@@ -588,12 +586,7 @@
 
 	cleanAllDisabled: function () {
 	if (this.options['autoclear']) return true;
-		var c = 0;
-		 for (var i = 0; i < this.entries.length; i++) {
-		 	if (this.entries[i].state == FileUploadEntry.UPLOAD_SUCCESS) {
-		 		c++;
-		 	}
-		 }
+		var c = this.getFileEntriesSumByState(FileUploadEntry.UPLOAD_SUCCESS);
 		return (c == 0);
 	},
 	
@@ -601,21 +594,29 @@
 		if (this.runUpload) {
 			return false;
 		} else {
-			var c = 0;
-		 	for (var i = 0; i < this.entries.length; i++) {
-		 		var e = this.entries[i];
-		 		if (e.state == FileUploadEntry.READY || e.state == FileUploadEntry.INITIALIZED) {
-		 			c++;
-		 		}
-		 	}
+			var c = this.getFileEntriesSumByState(FileUploadEntry.READY, FileUploadEntry.INITIALIZED, FileUploadEntry.UPLOAD_CANCELED);
 			return (c == 0);
 		}
 	},
 	
+	getFileEntriesSumByState: function () {
+		var statuses = {}
+		var s = 0;
+		for (var i = 0; i < arguments.length; i++) {
+			statuses[arguments[i]] = true;
+		}
+		for (var i = 0; i < this.entries.length; i++) {
+			if (statuses[this.entries[i].state]) {
+				s++;
+			}
+		}
+		return s;
+	},
+	
 	switchUploadButton: function () {
 		if (this.runUpload) {
 			var d = $(this.id + ":upload2");
-			d.innerHTML = "<b>Cancel</b>";
+			d.innerHTML = "<b>Stop</b>";
 			d.onclick = function () {
 				this.stop();
 			}.bind(this);
@@ -651,7 +652,7 @@
     },
 
     disableAddButton: function() {
-    	var disabled = (this.maxFileBatchSize && this.entries.length >= this.maxFileBatchSize);
+    	var disabled = ((this.getFileEntriesSumByState(FileUploadEntry.READY, FileUploadEntry.INITIALIZED, FileUploadEntry.UPLOAD_CANCELED) + this.uploadedCount) >= this.maxFileBatchSize);
     	this.currentInput.disabled = disabled || this.disabled;
     	var d1 = $(this.id+":add1");
     	var d2 = $(this.id+":add2");
@@ -751,9 +752,9 @@
 		if (newState == FileUploadEntry.UPLOAD_SUCCESS || newState == FileUploadEntry.UPLOAD_SIZE_ERROR) {
 			//todo clear completed
 		
-			//if (this.activeEntry) {
-			//	this.activeEntry.clear();
-			//}
+			if (newState == FileUploadEntry.UPLOAD_SUCCESS) {
+				this.uploadedCount++;
+			}
 			
 			this._endUpload();
 			




More information about the richfaces-svn-commits mailing list