Skip to content

Commit

Permalink
#32: Restructured and improved the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Pagebakers committed Dec 30, 2014
1 parent 1885155 commit 5d67eb5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 46 deletions.
3 changes: 1 addition & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
//Internals
"S3Policy": false,
"Slingshot": true,
"matchAllowedFileTypes": false,
"mixins": false
"matchAllowedFileTypes": false
}
}
32 changes: 22 additions & 10 deletions lib/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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({
Expand Down
49 changes: 26 additions & 23 deletions lib/restrictions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 " +
Expand All @@ -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))
Expand Down
17 changes: 6 additions & 11 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Slingshot.Upload = function (directive, metaData) {
return formData;
}

_.extend(self, mixins, {
_.extend(self, {

/**
* @returns {string}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
}

});
};

Expand Down

0 comments on commit 5d67eb5

Please sign in to comment.