views:

159

answers:

2

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

+5  A: 

Paperclip stores the file size.
So you could just, for one user, do something like :

def used_file_size
    self.files.sum(:document_file_size)
end

You'll then have the total size of every user's documents.
You can then add a max size limit and not validate the document's upload if the user has reached that size.

Damien MATHIEU
+4  A: 

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.

Mike Buckbee