Guest User

Untitled

a guest
Jan 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. ###
  2. Creates a new bucket for a user using the given parameters (if not already above the user's limit)
  3.  
  4. @param {Object} user the document of the authenticated user
  5. @param {String} name to be used for the bucket
  6. @param {Number} limit to be used for the bucket (in Byte)
  7. @param {Function} callback gets two parameters - error object (if an error occured) and the new bucket document if successful.
  8.  
  9. @api public
  10. ###
  11. Buckets::create = (user, name, limit, callback) ->
  12. this.list user, (err, info) =>
  13. return callback(err) if err
  14.  
  15. # check the bucket name (for already taken)
  16. for bucket in info.buckets
  17. if bucket.name == name
  18. return callback(message: "The name \"#{name}\" is already taken.")
  19.  
  20. # check the userlimit
  21. if user.limit and (info.totalSize + limit) > user.limit
  22. max = user.limit - info.totalSize
  23. return callback(message: "The desired limit of #{limit} exceeds the total limit of #{user.limit} by #{limit-max}. The maximum limit is #{max}.")
  24.  
  25. bucket =
  26. name: name
  27. owner: user._id.toHexString()
  28. limit: limit
  29. size: 0
  30.  
  31. this.collection.insert bucket, {safe:true}, (err, docs) ->
  32. return callback err if err
  33. return callback null, docs[0]
Add Comment
Please, Sign In to add comment