Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 419
CalebHC Avatar asked Nov 10 '09 04:11

CalebHC


2 Answers

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.

like image 187
Mike Buckbee Avatar answered Oct 07 '22 01:10

Mike Buckbee


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.

like image 24
Damien MATHIEU Avatar answered Oct 06 '22 23:10

Damien MATHIEU