Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 718
Nick Ginanto Avatar asked Feb 10 '13 22:02

Nick Ginanto


1 Answers

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

like image 164
pierrea Avatar answered Oct 15 '22 07:10

pierrea