Search code examples
ruby-on-rails-3paperclipattachmentfilesize

Paperclip attachment file size


How do I fetch the file size of each style of a paperclip attachment?

@user.attachment_file_size doesn't seem to work

@user.attachment(:style).size

gives a number not related to the actual file size


Solution

  • I didn't find either how to get the file size for a given style, other than the original one.

    As seen in the paperclip source code, @user.attachment.size returns the size of the file as originally assigned. There is no way to get it for a specific style...

    A solution would be:

    open(@user.attachment(:style)).size
    

    But not efficient at all.

    To do it well, you should probably add some "custom size" fields in your attachment table that you would fill once the attachment is saved for each style...