-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#32: File constraints now available on both client and server #35
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f784f3f
#32: File constraints now available on both client and server, added …
Pagebakers 500dfed
#32: Removed logging
Pagebakers a4200a5
#32: Reverted the api and changed how restrictions are injected
Pagebakers 1885155
#32: Revert authorizing status
Pagebakers 5d67eb5
#32: Restructured and improved the code
Pagebakers 873e6e1
#32: Added documentation, updated jsdoc
Pagebakers d509e43
#32: Improved validation
Pagebakers 5a4f5e9
#32: fixed upload authorizing status
Pagebakers 698bd65
#32: Updated readme
Pagebakers ce817e6
#32: No file-restrictions on client is now permitted
gsuess 050dd59
#32: Simplified Readme.
gsuess File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/** | ||
* @module meteor-slingshot | ||
*/ | ||
|
||
Slingshot = {}; | ||
|
||
/* global matchAllowedFileTypes: true */ | ||
matchAllowedFileTypes = Match.OneOf(String, [String], RegExp, null); | ||
|
||
/** | ||
* List of configured restrictions by name. | ||
* | ||
* @type {Object.<String, Function>} | ||
* @private | ||
*/ | ||
|
||
Slingshot._restrictions = {}; | ||
|
||
/** | ||
* Creates file upload restrictions for a specific directive. | ||
* | ||
* @param {string} name - A unique identifier of the directive. | ||
* @param {Object} restrictions - The file upload restrictions. | ||
* @returns {Object} | ||
*/ | ||
|
||
Slingshot.fileRestrictions = function (name, restrictions) { | ||
check(restrictions, { | ||
authorize: Match.Optional(Function), | ||
maxSize: Match.Optional(Match.OneOf(Number, null)), | ||
allowedFileTypes: Match.Optional(matchAllowedFileTypes), | ||
}); | ||
|
||
return (Slingshot._restrictions[name] = | ||
_.extend(Slingshot._restrictions[name] || {}, restrictions)); | ||
}; | ||
|
||
/** | ||
* @param {string} name - The unique identifier of the directive to | ||
* retrieve the restrictions for. | ||
* @returns {Object} | ||
*/ | ||
|
||
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)); | ||
}, | ||
|
||
/** | ||
* @throws Meteor.Error | ||
* | ||
* @param {Number} size - Size of file in bytes. | ||
* @returns {boolean} | ||
*/ | ||
|
||
checkFileSize: function (size) { | ||
var maxSize = Math.min(this.getRestriction("maxSize"), Infinity); | ||
|
||
if (maxSize && size > maxSize) | ||
throw new Meteor.Error("Upload denied", "File exceeds allowed size of " + | ||
formatBytes(maxSize)); | ||
|
||
return true; | ||
}, | ||
|
||
/** | ||
* | ||
* @throws Meteor.Error | ||
* | ||
* @param {String} type - Mime type | ||
* @returns {boolean} | ||
*/ | ||
|
||
checkFileType: function (type) { | ||
var allowed = this.getRestriction("allowedFileTypes"); | ||
|
||
if (allowed instanceof RegExp) { | ||
|
||
if (!allowed.test(type)) | ||
throw new Meteor.Error("Upload denied", | ||
type + " is not an allowed file type"); | ||
|
||
return true; | ||
} | ||
|
||
if (_.isArray(allowed)) { | ||
if (allowed.indexOf(type) < 0) { | ||
throw new Meteor.Error("Upload denied", | ||
type + " is not one of the followed allowed file types: " + | ||
allowed.join(", ")); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
if (allowed !== type) { | ||
throw new Meteor.Error("Upload denied", "Only file of type " + allowed + | ||
" can be uploaded"); | ||
} | ||
|
||
return true; | ||
} | ||
}; | ||
|
||
/** Human readable data-size in bytes. | ||
* | ||
* @param size {Number} | ||
* @returns {string} | ||
*/ | ||
|
||
function formatBytes(size) { | ||
var units = ['Bytes', 'KB', 'MB', 'GB', 'TB'], | ||
unit = units.shift(); | ||
|
||
while (size >= 0x400 && units.length) { | ||
size /= 0x400; | ||
unit = units.shift(); | ||
} | ||
|
||
return (Math.round(size * 100) / 100) + " " + unit; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this API change can be avoided, if the we mix in the file restrictions into options before constructing the directive.