-
-
Save arthurschreiber/1239780 to your computer and use it in GitHub Desktop.
if if if
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
### | |
Creates a new bucket for a user using the given parameters (if not already above the user's limit) | |
@param {Object} user the document of the authenticated user | |
@param {String} name to be used for the bucket | |
@param {Number} limit to be used for the bucket (in Byte) | |
@param {Function} callback gets two parameters - error object (if an error occured) and the new bucket document if successful. | |
@api public | |
### | |
Buckets::create = (user, name, limit, callback) -> | |
this.list user, (err, info) => | |
return callback(err) if err | |
# check the bucket name (for already taken) | |
for bucket in info.buckets | |
if bucket.name == name | |
return callback(message: "The name \"#{name}\" is already taken.") | |
# check the userlimit | |
if user.limit and (info.totalSize + limit) > user.limit | |
max = user.limit - info.totalSize | |
return callback(message: "The desired limit of #{limit} exceeds the total limit of #{user.limit} by #{limit-max}. The maximum limit is #{max}.") | |
bucket = | |
name: name | |
owner: user._id.toHexString() | |
limit: limit | |
size: 0 | |
this.collection.insert bucket, {safe:true}, (err, docs) -> | |
callback err, docs?[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment