7

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

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
CalebHC
  • 4,998
  • 3
  • 36
  • 42

2 Answers2

7

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.

John Topley
  • 113,588
  • 46
  • 195
  • 237
Mike Buckbee
  • 6,793
  • 2
  • 33
  • 36
6

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
  • 31,924
  • 13
  • 86
  • 94