From ce817e6766b19f89ad8b1ebbec077a9177b7af92 Mon Sep 17 00:00:00 2001 From: gsuess Date: Mon, 5 Jan 2015 00:20:01 +0100 Subject: [PATCH] #32: No file-restrictions on client is now permitted --- lib/directive.js | 14 +++++++------- lib/upload.js | 14 +++++++------- lib/validators.js | 14 +++++++------- package.js | 6 ++++-- versions.json | 2 +- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/directive.js b/lib/directive.js index 4197e31..22a9803 100644 --- a/lib/directive.js +++ b/lib/directive.js @@ -73,7 +73,7 @@ Slingshot._directives = {}; Slingshot.createDirective = function (name, service, options) { if (_.has(Slingshot._directives, name)) throw new Error("Directive '" + name + "' already exists"); - + var restrictions = Slingshot.getRestrictions(name); _.defaults(options, restrictions); @@ -99,7 +99,7 @@ Slingshot.getDirective = function (name) { Slingshot.Directive = function (service, directive) { check(this, Slingshot.Directive); - + //service does not have to be a plain-object, so checking fields individually check(service.directiveMatch, Object); check(service.upload, Function); @@ -115,7 +115,7 @@ Slingshot.Directive = function (service, directive) { cacheControl: Match.Optional(String), contentDisposition: Match.Optional(Match.OneOf(String, null)) }, service.directiveMatch)); - + /** * @method storageService * @returns {Object} @@ -159,7 +159,7 @@ _.extend(Slingshot.Directive.prototype, { return instructions; }, - + /** * * @method requestAuthorization @@ -178,10 +178,10 @@ _.extend(Slingshot.Directive.prototype, { restrictions = _.pick(this._directive, ['authorize', 'maxSize', 'allowedFileTypes'] ); - - return validators.authorize(context, file, meta, restrictions); + + return validators.checkAll(context, file, meta, restrictions); } - + }); Meteor.methods({ diff --git a/lib/upload.js b/lib/upload.js index a638845..b5c0ccd 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -59,12 +59,12 @@ Slingshot.Upload = function (directive, metaData) { uploaded: function () { return loaded.get(); }, - + /** * @param {File} file * @returns {null|Error} Returns null on success, Error on failure. */ - + validate: function(file) { var context = { userId: Meteor.userId() @@ -72,8 +72,8 @@ Slingshot.Upload = function (directive, metaData) { try { var validators = Slingshot.Validators, restrictions = Slingshot.getRestrictions(directive); - - validators.authorize(context, file, metaData, restrictions) && null; + + validators.checkAll(context, file, metaData, restrictions) && null; } catch(error) { return error; } @@ -119,11 +119,11 @@ Slingshot.Upload = function (directive, metaData) { if (!self.file) { callback(new Error("No file to request upload for")); } - + var file = _.pick(self.file, "name", "size", "type"); status.set("authorizing"); - + var error = this.validate(file); if (error) { status.set("failed"); @@ -270,7 +270,7 @@ Slingshot.Upload = function (directive, metaData) { return field && field.value; } - + }); }; diff --git a/lib/validators.js b/lib/validators.js index e4cbf6f..ffebc2f 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -1,8 +1,8 @@ Slingshot.Validators = { - + /** * - * @method authorize + * @method checkAll * * @throws Meteor.Error * @@ -14,7 +14,7 @@ Slingshot.Validators = { * @returns {Boolean} */ - authorize: function (context, file, meta, restrictions) { + checkAll: function (context, file, meta, restrictions) { return this.checkFileSize(file.size, restrictions.maxSize) && this.checkFileType(file.type, restrictions.allowedFileTypes) && (typeof restrictions.authorize !== 'function' || @@ -44,7 +44,7 @@ Slingshot.Validators = { * @throws Meteor.Error * * @param {String} type - Mime type - * @param {RegExp|Array|String} allowed - Allowed file type(s) + * @param {(RegExp|Array|String)} [allowed] - Allowed file type(s) * @returns {boolean} */ @@ -68,8 +68,8 @@ Slingshot.Validators = { return true; } - if (allowed !== type) { - throw new Meteor.Error("Upload denied", "Only file of type " + allowed + + if (allowed && allowed !== type) { + throw new Meteor.Error("Upload denied", "Only files of type " + allowed + " can be uploaded"); } @@ -93,4 +93,4 @@ function formatBytes(size) { } return (Math.round(size * 100) / 100) + " " + unit; -} \ No newline at end of file +} diff --git a/package.js b/package.js index 5ad1118..8fc9191 100644 --- a/package.js +++ b/package.js @@ -10,12 +10,14 @@ Package.on_use(function (api) { api.use(["underscore", "check"]); api.use(["tracker", "reactive-var"], "client"); - + api.add_files([ "lib/restrictions.js", "lib/validators.js" - ], ["client", "server"]); + ]); + api.add_files("lib/upload.js", "client"); + api.add_files([ "lib/directive.js", "lib/storage-policy.js", diff --git a/versions.json b/versions.json index c2a4a24..ade065e 100644 --- a/versions.json +++ b/versions.json @@ -34,6 +34,6 @@ ] ], "pluginDependencies": [], - "toolVersion": "meteor-tool@1.0.36", + "toolVersion": "meteor-tool@1.0.35", "format": "1.0" } \ No newline at end of file