discourse/app/models/optimized_image.rb
2013-06-17 02:49:34 +02:00

60 lines
1.6 KiB
Ruby

require "digest/sha1"
class OptimizedImage < ActiveRecord::Base
belongs_to :upload
def self.create_for(upload, width=nil, height=nil)
@image_sorcery_loaded ||= require "image_sorcery"
original_path = "#{Rails.root}/public#{upload.url}"
# create a temp file with the same extension as the original
temp_file = Tempfile.new(["discourse", File.extname(original_path)])
temp_path = temp_file.path
# do the resize when there is both dimensions
if width && height && ImageSorcery.new(original_path).convert(temp_path, resize: "#{width}x#{height}")
image_info = FastImage.new(temp_path)
thumbnail = OptimizedImage.new({
upload_id: upload.id,
sha: Digest::SHA1.file(temp_path).hexdigest,
ext: File.extname(temp_path),
width: image_info.size[0],
height: image_info.size[1]
})
# make sure the directory exists
FileUtils.mkdir_p Pathname.new(thumbnail.path).dirname
# move the temp file to the right location
File.open(thumbnail.path, "wb") do |f|
f.write temp_file.read
end
end
# close && remove temp file
temp_file.close
temp_file.unlink
thumbnail
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
def filename
"#{sha[6..16]}_#{width}x#{height}#{ext}"
end
end