create thumbnails when needed

This commit is contained in:
Régis Hanol
2013-06-17 01:00:25 +02:00
parent d4c3fe4e6a
commit cc9e0ec80a
4 changed files with 87 additions and 9 deletions

View File

@@ -1,7 +1,35 @@
class OptimizedImage < ActiveRecord::Base
belongs_to :upload
def filename
"#{sha[0..2]}/#{sha[3..5]}/#{sha[6..16]}_#{width}x#{height}#{ext}"
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
def filename
"#{sha[6..16]}_#{width}x#{height}#{ext}"
end
end