Calculating Attachment Fu Disk Usage In Rails

Posted about 1 year ago

So I’ll keep this one short and to the point. I wanted to see in the admin of my site how much disk space each photo album was taking up, and also a grand total of all photo albums. As noted in the title I am using Attachment Fu in Rails. The latter of the two is very easy, I could just use a system call to du -chs and be done with it, or I could do some fun programming! Here is what I did…

To find the disk usage of a particular photo album is not completely simple. I need to consider each of three thumbnail sizes for each image as well as the original image itself. I might as well take advantage of the attachment_fu helpers in some sort of nested looping.

Ok, I’m beginning to rant so here’s the damn code:

Calculating size of each photo album.



  def photo_album_size(album)

    album_size = 0

    album.pictures.each do |p|

      album_size += File.size(DOCUMENT_ROOT + p.public_filename)

      p.thumbnails.each do |t|

        album_size += File.size(DOCUMENT_ROOT + t.public_filename)

      end

    end

    return album_size

  end

 

Calculating grand total of all photo albums.



  def total_photo_albums_size()

    albums_size = 0

    albums = PhotoAlbum.find(:all)

    albums.each do |a|

      a.pictures.each do |p|

        albums_size += File.size(DOCUMENT_ROOT + p.public_filename)

        p.thumbnails.each do |t|

          albums_size += File.size(DOCUMENT_ROOT + t.public_filename)

        end

      end

    end

    return albums_size

  end

 

So thats it. I threw these in my photo albums helper and they work pretty well. By the way DOCUMENT_ROOT will have to be defined or you will have to substitute for it in some other way.

Sean

Posted in , ,  | no comments

Comments

(leave url/email »)

   Comment Markup Help Preview comment