diff --git a/.jshintrc b/.jshintrc index a2fb006..765e3d1 100644 --- a/.jshintrc +++ b/.jshintrc @@ -126,7 +126,6 @@ //Internals "S3Policy": false, "Slingshot": true, - "matchAllowedFileTypes": false, - "mixins": false + "matchAllowedFileTypes": false } } diff --git a/lib/directive.js b/lib/directive.js index cbef75a..a7f68e8 100644 --- a/lib/directive.js +++ b/lib/directive.js @@ -133,7 +133,7 @@ Slingshot.Directive = function (service, directive) { this._directive = directive; }; -_.extend(Slingshot.Directive.prototype, mixins, { +_.extend(Slingshot.Directive.prototype, { /** * @param {{userId: String}} method @@ -160,17 +160,29 @@ _.extend(Slingshot.Directive.prototype, mixins, { return instructions; }, - /** - * @param {String} restriction - Name of the restriction to retrieve - * @returns {mixed} The restriction configuration + /** + * + * @method requestAuthorization + * + * @throws Meteor.Error + * + * @param {Object} context + * @param {FileInfo} file + * @param {Object} [meta] + * @param {Function} [authorize] + * + * @returns {Boolean} */ - - getRestriction: function (restriction) { - var restrictions = _.pick(this._directive, - ['authorize', 'maxSize', 'allowedFileTypes'] - ); - return restrictions[restriction]; + + requestAuthorization: function (context, file, meta) { + var validators = Slingshot.Validators, + restrictions = _.pick(this._directive, + ['authorize', 'maxSize', 'allowedFileTypes'] + ); + + return validators.authorize(context, file, meta, restrictions); } + }); Meteor.methods({ diff --git a/lib/restrictions.js b/lib/restrictions.js index d773247..082512f 100644 --- a/lib/restrictions.js +++ b/lib/restrictions.js @@ -52,35 +52,39 @@ Slingshot.getRestrictions = function (name) { return this._restrictions[name] || {}; }; -/* global mixins: true */ -mixins = { - /** - * - * @method requestAuthorization - * - * @throws Meteor.Error - * - * @param {FileInfo} file - * @param {Object} [meta] - * - * @returns {Boolean} - */ - - requestAuthorization: function (method, file, meta) { - var authorize = this.getRestriction("authorize"); - return this.checkFileSize(file.size) && this.checkFileType(file.type) && - (typeof authorize !== 'function' || authorize.call(method, file, meta)); +Slingshot.Validators = { + + /** + * + * @method authorize + * + * @throws Meteor.Error + * + * @param {Object} context + * @param {FileInfo} file + * @param {Object} [meta] + * @param {Object} [restrictions] + * + * @returns {Boolean} + */ + + authorize: function (context, file, meta, restrictions) { + return this.checkFileSize(file.size, restrictions.maxSize) && + this.checkFileType(file.type, restrictions.allowedFileTypes) && + (typeof restrictions.authorize !== 'function' || + restrictions.authorize.call(context, file, meta)); }, /** * @throws Meteor.Error * * @param {Number} size - Size of file in bytes. + * @param {Number} maxSize - Max size of file in bytes. * @returns {boolean} */ - checkFileSize: function (size) { - var maxSize = Math.min(this.getRestriction("maxSize"), Infinity); + checkFileSize: function (size, maxSize) { + maxSize = Math.min(maxSize, Infinity); if (maxSize && size > maxSize) throw new Meteor.Error("Upload denied", "File exceeds allowed size of " + @@ -94,12 +98,11 @@ mixins = { * @throws Meteor.Error * * @param {String} type - Mime type + * @param {RegExp,Array,String} allowed - Allowed file type(s) * @returns {boolean} */ - checkFileType: function (type) { - var allowed = this.getRestriction("allowedFileTypes"); - + checkFileType: function (type, allowed) { if (allowed instanceof RegExp) { if (!allowed.test(type)) diff --git a/lib/upload.js b/lib/upload.js index d657aa3..4f6957b 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -34,7 +34,7 @@ Slingshot.Upload = function (directive, metaData) { return formData; } - _.extend(self, mixins, { + _.extend(self, { /** * @returns {string} @@ -65,7 +65,10 @@ Slingshot.Upload = function (directive, metaData) { userId: Meteor.userId() }; try { - return self.requestAuthorization(context, file, metaData); + var validators = Slingshot.Validators, + restrictions = Slingshot.getRestrictions(directive); + + return validators.authorize(context, file, metaData, restrictions); } catch(error) { return error; } @@ -262,16 +265,8 @@ Slingshot.Upload = function (directive, metaData) { field = data && _.findWhere(data, {name: name}); return field && field.value; - }, - - /** - * @param {String} restriction - Name of the restriction to retrieve - * @returns {mixed} The restriction configuration - */ - - getRestriction: function (restriction) { - return Slingshot.getRestrictions(directive)[restriction]; } + }); };