mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
create thumbnails when needed
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user