Paperclip attachment file size

前端 未结 4 825
难免孤独
难免孤独 2021-01-18 07:52

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(         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-18 08:21

    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...

提交回复
热议问题