Verify that uploaded files are readable when selected
authorRob Crittenden <rcritten@redhat.com>
Tue, 3 Nov 2015 15:26:36 +0000 (10:26 -0500)
committerPatrick Uiterwijk <puiterwijk@redhat.com>
Sat, 14 Nov 2015 00:16:06 +0000 (01:16 +0100)
When selecting a file using file input this doesn't guarantee
that the file contents are readable. No real error is provided
in this case, things just don't work as expected.

When adding a SAML SP and the metadata file or image is provided via
file upload try to read the file in the browser first and pop up an
alert if it is unreadable.

https://fedorahosted.org/ipsilon/ticket/22

Signed-off-by: Rob Crittenden <rcritten@redhat.com>
Reviewed-by: Simo Sorce <simo@redhat.com>
Reviewed-by: John Dennis <jdennis@redhat.com>
templates/admin/option_config.html
templates/admin/providers/saml2_sp_new.html

index 02babe6..14326fd 100644 (file)
                     var reader = new FileReader(); // instance of the FileReader
                     reader.readAsDataURL(files[0]); // read the local file
  
-                    reader.onloadend = function(){ // set image data as background of div
+                    reader.onloadend = function(e){ // set image data as background of div
+                        var contents = e.target.result;
+                        if (!contents) {
+                            window.alert('Image file is unreadable')
+                            document.getElementById('uploadFile').value = null;
+                        }
+
                         $("#imagePreview").css("background-image", "url("+this.result+")");
                     }
                 }
index 1c95355..13f1a9e 100644 (file)
         <a href="{{ back }}" class="btn btn-default" title="Back">Back</a>
         </form>
     </div>
+
+<script>
+    function verifyFile(filename, objid, failtext) {
+        var reader = new FileReader();
+        reader.readAsDataURL(filename); // read the local file
+
+        reader.onloadend = function(e){
+            var contents = e.target.result;
+            if (!contents) {
+                window.alert(failtext)
+                document.getElementById(objid).value = null;
+            }
+        }
+    }
+
+    $(function() {
+        $("#file").on("change", function()
+        {
+            var files = !!this.files ? this.files : [];
+            if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support
+
+            verifyFile(files[0], 'file', 'Metadata file is unreadable');
+        });
+    });
+
+    $(function() {
+        $("#image").on("change", function()
+        {
+            var files = !!this.files ? this.files : [];
+            if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support
+
+            verifyFile(files[0], 'image', 'Image file is unreadable');
+        });
+    });
+</script>
 {% endblock %}