Search code examples
ruby-on-railsrubylimitsfile-storage

How to give users a file storage limit?


I'm working on a web application right now using Rails and wanted to see if anyone knew of a good way to keep track of file storage limits? We want to give users a specific amount of room that they can use to upload files and we are using paperclip for storage on Amazon S3. Any thoughts? Thanks


Solution

  • One of the optional columns you can use with paperclip is the OBJECT_file_size which you can sum up like

    # Assuming a user has many files relationship
    @user.uploads.sum(:image_file_size)
    

    As to the actually imposing the cap, I'd recommend creating a custom validation on whatever the file model is so that you can pass errors and issues back to the client.