Sha256: 7f5032da1c002a39454546f3033d0c267d48acb4d27d99f680613b88bbeba901

Contents?: true

Size: 808 Bytes

Versions: 2

Compression:

Stored size: 808 Bytes

Contents

module Plotline
  class Image < ActiveRecord::Base
    before_save :set_metadata
    after_destroy :remove_image_file

    def filename
      remote_file? ? image : File.join('./public', image)
    end

    def remove_image_file
      File.delete(filename) unless remote_file?
    end

    private

    def set_metadata
      img = FastImage.new(filename)

      self.width, self.height = img.size
      self.ratio = self.width.to_f / self.height.to_f
      self.content_type = img.type
      self.file_size = img.content_length

      return if remote_file?

      File.open(filename) do |file|
        self.file_size = file.size # content_length doesn't always work
      end

      self.exif = Exiftool.new(filename).to_hash
    end

    def remote_file?
      image.start_with?('http')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
plotline-0.1.1 app/models/plotline/image.rb
plotline-0.1.0 app/models/plotline/image.rb