From e68c0656b517e52ae97500d0f0d32cc6c9feb4df Mon Sep 17 00:00:00 2001 From: Rob Johnston Date: Mon, 2 Oct 2017 20:16:48 -0400 Subject: [PATCH 1/5] Additional: Validate max number and size of file(s). --- src/additional/maxfiles.js | 16 ++++++++++++++++ src/additional/maxsize.js | 18 ++++++++++++++++++ src/additional/maxsizetotal.js | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/additional/maxfiles.js create mode 100644 src/additional/maxsize.js create mode 100644 src/additional/maxsizetotal.js diff --git a/src/additional/maxfiles.js b/src/additional/maxfiles.js new file mode 100644 index 000000000..a3897cf37 --- /dev/null +++ b/src/additional/maxfiles.js @@ -0,0 +1,16 @@ +//Limit the number of files in a FileList. +$.validator.addMethod( "maxfiles", function( value, element, param ) { + if ( this.optional( element ) ) { + return true; + } + + if ( $( element ).attr( "type" ) === "file" ) { + if ( element.files && element.files.length ) { + if ( element.files.length > param ) { + return false; + } + } + } + + return true; +}, $.validator.format( "Please select no more than {0} files." ) ); diff --git a/src/additional/maxsize.js b/src/additional/maxsize.js new file mode 100644 index 000000000..12c07d66d --- /dev/null +++ b/src/additional/maxsize.js @@ -0,0 +1,18 @@ +//Limit the size of each individual file in a FileList. +$.validator.addMethod( "maxsize", function( value, element, param ) { + if ( this.optional( element ) ) { + return true; + } + + if ( $( element ).attr( "type" ) === "file" ) { + if ( element.files && element.files.length ) { + for ( var i = 0; i < element.files.length; i++ ) { + if ( element.files[ i ].size > param ) { + return false; + } + } + } + } + + return true; +}, $.validator.format( "File size must not exceed {0} bytes each." ) ); diff --git a/src/additional/maxsizetotal.js b/src/additional/maxsizetotal.js new file mode 100644 index 000000000..68ac441c4 --- /dev/null +++ b/src/additional/maxsizetotal.js @@ -0,0 +1,22 @@ +//Limit the size of all files in a FileList. +$.validator.addMethod( "maxsizetotal", function( value, element, param ) { + if ( this.optional( element ) ) { + return true; + } + + if ( $( element ).attr( "type" ) === "file" ) { + if ( element.files && element.files.length ) { + var totalSize = 0; + + for ( var i = 0; i < element.files.length; i++ ) { + totalSize += element.files[ i ].size; + if ( totalSize > param ) { + return false; + } + } + } + } + + return true; +}, $.validator.format( "Total size of all files must not exceed {0} bytes." ) ); + From 360699d6a087c92fa503f775f46aa1be719b8f8b Mon Sep 17 00:00:00 2001 From: Rob Johnston Date: Tue, 3 Oct 2017 17:15:36 -0400 Subject: [PATCH 2/5] Tests: Add tests for max number and size of file(s). --- test/methods.js | 111 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/test/methods.js b/test/methods.js index 173a2281d..ce36e896f 100644 --- a/test/methods.js +++ b/test/methods.js @@ -42,6 +42,39 @@ function acceptFileDummyInput( filename, mimeType ) { }; } +function fileDummyInput( selectedFiles ) { + var aFiles = [], + oFiles; + + for ( var i = 0; i < selectedFiles.length; i++ ) { + aFiles.push( { + name: selectedFiles[ i ].name, + size: selectedFiles[ i ].size, + type: "image/jpeg" + } ); + } + + //Convert the array of objects to an object. + oFiles = aFiles.reduce( function( acc, cur, i ) { + acc[ i ] = cur; + return acc; + }, {} ); + + //Add the "length" property to the object. + oFiles.length = selectedFiles.length; + + //Add the "item()" method to the object. + oFiles.item = function( i ) { return aFiles[ i ]; }; + + return { + type: "file", + files: oFiles, + nodeName: "INPUT", + value: "/tmp/fake_value", + hasAttribute: function() { return false; } + }; +} + QUnit.module( "methods" ); QUnit.test( "default messages", function( assert ) { @@ -1704,3 +1737,81 @@ QUnit.test( "file accept - invalid mime type", function( assert ) { proxy = $.proxy( $.validator.methods.accept, new $.validator( {}, $form[ 0 ] ), null, input, "application/vnd.google-earth.kml+xml" ); assert.equal( proxy(), false, "the selected file for upload has invalid mime type" ); } ); + +QUnit.test( "file size - below max", function( assert ) { + var input = acceptFileDummyInput( "test.png", "image/png" ), + $form = $( "
" ), + proxy = $.proxy( $.validator.methods.maxsize, new $.validator( {}, $form[ 0 ] ), null, input, "500001" ); + assert.ok( proxy(), "the selected file for upload is smaller than max" ); +} ); + +QUnit.test( "file size - over max", function( assert ) { + var input = acceptFileDummyInput( "test.png", "image/png" ), + $form = $( "" ), + proxy = $.proxy( $.validator.methods.maxsize, new $.validator( {}, $form[ 0 ] ), null, input, "500000" ); + assert.equal( proxy(), false, "the selected file for upload is greater than max" ); +} ); + +QUnit.test( "file maxsize - valid size", function( assert ) { + var selectedFiles = [ { name: "test.jpg", size: 500000 } ], + input = fileDummyInput( selectedFiles ), + $form = $( "" ), + proxy = $.proxy( $.validator.methods.maxsize, new $.validator( {}, $form[ 0 ] ), null, input, 500000 ); + assert.ok( proxy(), "the size of the file does not exceed the maximum" ); +} ); + +QUnit.test( "file maxsize - valid size for each file", function( assert ) { + var selectedFiles = [ { name: "test1.jpg", size: 500000 }, { name: "test2.jpg", size: 500000 } ], + input = fileDummyInput( selectedFiles ), + $form = $( "" ), + proxy = $.proxy( $.validator.methods.maxsize, new $.validator( {}, $form[ 0 ] ), null, input, 500000 ); + assert.ok( proxy(), "the size of the each file does not exceed the maximum" ); +} ); + +QUnit.test( "file maxsize - too big", function( assert ) { + var selectedFiles = [ { name: "test.jpg", size: 500001 } ], + input = fileDummyInput( selectedFiles ), + $form = $( "" ), + proxy = $.proxy( $.validator.methods.maxsize, new $.validator( {}, $form[ 0 ] ), null, input, 500000 ); + assert.equal( proxy(), false, "the size of the file exceeds the maximum" ); +} ); + +QUnit.test( "file maxsize - second file too big", function( assert ) { + var selectedFiles = [ { name: "test1.jpg", size: 500000 }, { name: "test2.jpg", size: 500001 } ], + input = fileDummyInput( selectedFiles ), + $form = $( "" ), + proxy = $.proxy( $.validator.methods.maxsize, new $.validator( {}, $form[ 0 ] ), null, input, 500000 ); + assert.equal( proxy(), false, "the size of the second file exceeds the maximum" ); +} ); + +QUnit.test( "file maxsizetotal - valid size", function( assert ) { + var selectedFiles = [ { name: "test1.jpg", size: 250000 }, { name: "test2.jpg", size: 250000 } ], + input = fileDummyInput( selectedFiles ), + $form = $( "" ), + proxy = $.proxy( $.validator.methods.maxsizetotal, new $.validator( {}, $form[ 0 ] ), null, input, 500000 ); + assert.ok( proxy(), "the size of the files together does not exceed the maximum" ); +} ); + +QUnit.test( "file maxsizetotal - too big", function( assert ) { + var selectedFiles = [ { name: "test1.jpg", size: 250000 }, { name: "test2.jpg", size: 250001 } ], + input = fileDummyInput( selectedFiles ), + $form = $( "" ), + proxy = $.proxy( $.validator.methods.maxsizetotal, new $.validator( {}, $form[ 0 ] ), null, input, 500000 ); + assert.equal( proxy(), false, "the size of the files together exceeds the maximum" ); +} ); + +QUnit.test( "file maxfiles - valid number", function( assert ) { + var selectedFiles = [ { name: "test1.jpg", size: 500000 }, { name: "test2.jpg", size: 500000 } ], + input = fileDummyInput( selectedFiles ), + $form = $( "" ), + proxy = $.proxy( $.validator.methods.maxfiles, new $.validator( {}, $form[ 0 ] ), null, input, 2 ); + assert.ok( proxy(), "the number of files does not exceed the maximum" ); +} ); + +QUnit.test( "file maxfiles - too many", function( assert ) { + var selectedFiles = [ { name: "test1.jpg", size: 500000 }, { name: "test2.jpg", size: 500000 }, { name: "test3.jpg", size: 500000 } ], + input = fileDummyInput( selectedFiles ), + $form = $( "" ), + proxy = $.proxy( $.validator.methods.maxfiles, new $.validator( {}, $form[ 0 ] ), null, input, 2 ); + assert.equal( proxy(), false, "the number of files exceeds the maximum" ); +} ); From 00d2b203b412d22cb99394a77d74315a8f5545b3 Mon Sep 17 00:00:00 2001 From: Rob Johnston Date: Sun, 8 Oct 2017 14:11:14 -0400 Subject: [PATCH 3/5] Additional: Add space before comment. --- src/additional/maxfiles.js | 2 +- src/additional/maxsize.js | 2 +- src/additional/maxsizetotal.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/additional/maxfiles.js b/src/additional/maxfiles.js index a3897cf37..23490a4f2 100644 --- a/src/additional/maxfiles.js +++ b/src/additional/maxfiles.js @@ -1,4 +1,4 @@ -//Limit the number of files in a FileList. +// Limit the number of files in a FileList. $.validator.addMethod( "maxfiles", function( value, element, param ) { if ( this.optional( element ) ) { return true; diff --git a/src/additional/maxsize.js b/src/additional/maxsize.js index 12c07d66d..0b9db7cd3 100644 --- a/src/additional/maxsize.js +++ b/src/additional/maxsize.js @@ -1,4 +1,4 @@ -//Limit the size of each individual file in a FileList. +// Limit the size of each individual file in a FileList. $.validator.addMethod( "maxsize", function( value, element, param ) { if ( this.optional( element ) ) { return true; diff --git a/src/additional/maxsizetotal.js b/src/additional/maxsizetotal.js index 68ac441c4..c2f990bca 100644 --- a/src/additional/maxsizetotal.js +++ b/src/additional/maxsizetotal.js @@ -1,4 +1,4 @@ -//Limit the size of all files in a FileList. +// Limit the size of all files in a FileList. $.validator.addMethod( "maxsizetotal", function( value, element, param ) { if ( this.optional( element ) ) { return true; From f7f7a760aea2c082b9339fe037d2c4002c864acb Mon Sep 17 00:00:00 2001 From: Rob Johnston Date: Sun, 8 Oct 2017 14:14:54 -0400 Subject: [PATCH 4/5] Additional: Merge two 'if' blocks into one. --- src/additional/maxfiles.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/additional/maxfiles.js b/src/additional/maxfiles.js index 23490a4f2..ed7c4d7dc 100644 --- a/src/additional/maxfiles.js +++ b/src/additional/maxfiles.js @@ -5,10 +5,8 @@ $.validator.addMethod( "maxfiles", function( value, element, param ) { } if ( $( element ).attr( "type" ) === "file" ) { - if ( element.files && element.files.length ) { - if ( element.files.length > param ) { - return false; - } + if ( element.files && element.files.length && element.files.length > param ) { + return false; } } From d70eb7a8f95c6aac40a3aa3dcc96b15511a80825 Mon Sep 17 00:00:00 2001 From: Brahim Arkni Date: Sun, 8 Oct 2017 21:22:30 +0100 Subject: [PATCH 5/5] Additional: remove duplicate check --- src/additional/maxfiles.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/additional/maxfiles.js b/src/additional/maxfiles.js index ed7c4d7dc..0cf53e735 100644 --- a/src/additional/maxfiles.js +++ b/src/additional/maxfiles.js @@ -5,7 +5,7 @@ $.validator.addMethod( "maxfiles", function( value, element, param ) { } if ( $( element ).attr( "type" ) === "file" ) { - if ( element.files && element.files.length && element.files.length > param ) { + if ( element.files && element.files.length > param ) { return false; } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy