开发者

How to give users a file storage limit?

开发者 https://www.devze.com 2022-12-11 12:56 出处:网络
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 sp开发者_高级运维ecific amount of r

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 sp开发者_高级运维ecific amount of room that they can use to upload files and we are using paperclip for storage on Amazon S3. Any thoughts? Thanks


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号