discourse/app/models/optimized_image.rb

36 lines
755 B
Ruby
Raw Normal View History

2013-06-16 03:39:48 -05:00
class OptimizedImage < ActiveRecord::Base
belongs_to :upload
2013-06-16 18:00:25 -05:00
def self.create_for(upload_id, path)
image_info = FastImage.new(path)
OptimizedImage.new({
upload_id: upload_id,
sha: Digest::SHA1.file(path).hexdigest,
ext: File.extname(path),
width: image_info.size[0],
height: image_info.size[1]
})
end
def url
"#{Upload.base_url}/#{optimized_path}/#{filename}"
end
def path
"#{path_root}/#{optimized_path}/#{filename}"
end
def path_root
@path_root ||= "#{Rails.root}/public"
end
def optimized_path
"uploads/#{RailsMultisite::ConnectionManagement.current_db}/_optimized/#{sha[0..2]}/#{sha[3..5]}"
end
2013-06-16 03:39:48 -05:00
def filename
2013-06-16 18:00:25 -05:00
"#{sha[6..16]}_#{width}x#{height}#{ext}"
2013-06-16 03:39:48 -05:00
end
2013-06-16 18:00:25 -05:00
2013-06-16 03:39:48 -05:00
end