| 1 |
/* |
| 2 |
* jQuery File Upload Validation Plugin |
| 3 |
* https://github.com/blueimp/jQuery-File-Upload |
| 4 |
* |
| 5 |
* Copyright 2013, Sebastian Tschan |
| 6 |
* https://blueimp.net |
| 7 |
* |
| 8 |
* Licensed under the MIT license: |
| 9 |
* http://www.opensource.org/licenses/MIT |
| 10 |
*/ |
| 11 |
|
| 12 |
/* global define, require, window */ |
| 13 |
|
| 14 |
(function (factory) { |
| 15 |
'use strict'; |
| 16 |
if (typeof define === 'function' && define.amd) { |
| 17 |
// Register as an anonymous AMD module: |
| 18 |
define([ |
| 19 |
'jquery', |
| 20 |
'./jquery.fileupload-process' |
| 21 |
], factory); |
| 22 |
} else if (typeof exports === 'object') { |
| 23 |
// Node/CommonJS: |
| 24 |
factory(require('jquery')); |
| 25 |
} else { |
| 26 |
// Browser globals: |
| 27 |
factory( |
| 28 |
window.jQuery |
| 29 |
); |
| 30 |
} |
| 31 |
}(function ($) { |
| 32 |
'use strict'; |
| 33 |
|
| 34 |
// Append to the default processQueue: |
| 35 |
$.blueimp.fileupload.prototype.options.processQueue.push( |
| 36 |
{ |
| 37 |
action: 'validate', |
| 38 |
// Always trigger this action, |
| 39 |
// even if the previous action was rejected: |
| 40 |
always: true, |
| 41 |
// Options taken from the global options map: |
| 42 |
acceptFileTypes: '@', |
| 43 |
maxFileSize: '@', |
| 44 |
minFileSize: '@', |
| 45 |
maxNumberOfFiles: '@', |
| 46 |
disabled: '@disableValidation' |
| 47 |
} |
| 48 |
); |
| 49 |
|
| 50 |
// The File Upload Validation plugin extends the fileupload widget |
| 51 |
// with file validation functionality: |
| 52 |
$.widget('blueimp.fileupload', $.blueimp.fileupload, { |
| 53 |
|
| 54 |
options: { |
| 55 |
/* |
| 56 |
// The regular expression for allowed file types, matches |
| 57 |
// against either file type or file name: |
| 58 |
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i, |
| 59 |
// The maximum allowed file size in bytes: |
| 60 |
maxFileSize: 10000000, // 10 MB |
| 61 |
// The minimum allowed file size in bytes: |
| 62 |
minFileSize: undefined, // No minimal file size |
| 63 |
// The limit of files to be uploaded: |
| 64 |
maxNumberOfFiles: 10, |
| 65 |
*/ |
| 66 |
|
| 67 |
// Function returning the current number of files, |
| 68 |
// has to be overriden for maxNumberOfFiles validation: |
| 69 |
getNumberOfFiles: $.noop, |
| 70 |
|
| 71 |
// Error and info messages: |
| 72 |
messages: { |
| 73 |
maxNumberOfFiles: 'Maximum number of files exceeded', |
| 74 |
acceptFileTypes: 'File type not allowed', |
| 75 |
maxFileSize: 'File is too large', |
| 76 |
minFileSize: 'File is too small' |
| 77 |
} |
| 78 |
}, |
| 79 |
|
| 80 |
processActions: { |
| 81 |
|
| 82 |
validate: function (data, options) { |
| 83 |
if (options.disabled) { |
| 84 |
return data; |
| 85 |
} |
| 86 |
var dfd = $.Deferred(), |
| 87 |
settings = this.options, |
| 88 |
file = data.files[data.index], |
| 89 |
fileSize; |
| 90 |
if (options.minFileSize || options.maxFileSize) { |
| 91 |
fileSize = file.size; |
| 92 |
} |
| 93 |
if ($.type(options.maxNumberOfFiles) === 'number' && |
| 94 |
(settings.getNumberOfFiles() || 0) + data.files.length > |
| 95 |
options.maxNumberOfFiles) { |
| 96 |
file.error = settings.i18n('maxNumberOfFiles'); |
| 97 |
} else if (options.acceptFileTypes && |
| 98 |
!(options.acceptFileTypes.test(file.type) || |
| 99 |
options.acceptFileTypes.test(file.name))) { |
| 100 |
file.error = settings.i18n('acceptFileTypes'); |
| 101 |
} else if (fileSize > options.maxFileSize) { |
| 102 |
file.error = settings.i18n('maxFileSize'); |
| 103 |
} else if ($.type(fileSize) === 'number' && |
| 104 |
fileSize < options.minFileSize) { |
| 105 |
file.error = settings.i18n('minFileSize'); |
| 106 |
} else { |
| 107 |
delete file.error; |
| 108 |
} |
| 109 |
if (file.error || data.files.error) { |
| 110 |
data.files.error = true; |
| 111 |
dfd.rejectWith(this, [data]); |
| 112 |
} else { |
| 113 |
dfd.resolveWith(this, [data]); |
| 114 |
} |
| 115 |
return dfd.promise(); |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
}); |
| 121 |
|
| 122 |
})); |
| 123 |
|