2014-12-23 08:12:26 +08:00
|
|
|
class Emoji
|
|
|
|
include ActiveModel::SerializerSupport
|
|
|
|
|
|
|
|
attr_reader :path
|
|
|
|
attr_accessor :name, :url
|
|
|
|
|
|
|
|
# whitelist emojis so that new user can post emojis
|
|
|
|
Post::white_listed_image_classes << "emoji"
|
|
|
|
|
|
|
|
def initialize(path = nil)
|
|
|
|
@path = path
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove
|
|
|
|
return if path.blank?
|
2015-02-14 02:26:26 +08:00
|
|
|
if File.exists?(path)
|
|
|
|
File.delete(path) rescue nil
|
|
|
|
Emoji.clear_cache
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.all
|
2015-02-19 13:58:05 +08:00
|
|
|
Discourse.cache.fetch("all_emojis") { standard | custom }
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.standard
|
2015-02-19 13:58:05 +08:00
|
|
|
Discourse.cache.fetch("standard_emojis") { load_standard }
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.custom
|
2015-02-19 13:58:05 +08:00
|
|
|
Discourse.cache.fetch("custom_emojis") { load_custom }
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
2015-02-10 01:54:57 +08:00
|
|
|
def self.exists?(name)
|
|
|
|
Emoji[name].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.[](name)
|
|
|
|
Emoji.custom.detect { |e| e.name == name }
|
|
|
|
end
|
|
|
|
|
2014-12-23 08:12:26 +08:00
|
|
|
def self.create_from_path(path)
|
|
|
|
extension = File.extname(path)
|
|
|
|
Emoji.new(path).tap do |e|
|
|
|
|
e.name = File.basename(path, ".*")
|
|
|
|
e.url = "/#{base_url}/#{e.name}#{extension}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_from_db_item(emoji)
|
|
|
|
name = emoji["aliases"].first
|
|
|
|
filename = "#{name}.png"
|
|
|
|
Emoji.new.tap do |e|
|
|
|
|
e.name = name
|
|
|
|
e.url = "/images/emoji/#{SiteSetting.emoji_set}/#{filename}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_for(file, name)
|
|
|
|
extension = File.extname(file.original_filename)
|
|
|
|
path = "#{Emoji.base_directory}/#{name}#{extension}"
|
2015-02-10 01:54:57 +08:00
|
|
|
|
2015-02-14 02:26:26 +08:00
|
|
|
# store the emoji
|
|
|
|
FileUtils.mkdir_p(Pathname.new(path).dirname)
|
|
|
|
File.open(path, "wb") { |f| f << file.tempfile.read }
|
|
|
|
# clear the cache
|
|
|
|
Emoji.clear_cache
|
2015-01-30 01:36:35 +08:00
|
|
|
# launch resize job
|
|
|
|
Jobs.enqueue(:resize_emoji, path: path)
|
2014-12-23 08:12:26 +08:00
|
|
|
# return created emoji
|
2015-02-10 01:54:57 +08:00
|
|
|
Emoji[name]
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_cache
|
2015-02-19 13:58:05 +08:00
|
|
|
Discourse.cache.delete("custom_emojis")
|
|
|
|
Discourse.cache.delete("standard_emojis")
|
|
|
|
Discourse.cache.delete("all_emojis")
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.db_file
|
2015-01-24 07:07:39 +08:00
|
|
|
"#{Rails.root}/lib/emoji/db.json"
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_standard
|
|
|
|
File.open(db_file, "r:UTF-8") { |f| JSON.parse(f.read) }
|
|
|
|
.map { |emoji| Emoji.create_from_db_item(emoji) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_custom
|
2015-02-14 02:34:24 +08:00
|
|
|
Dir.glob(File.join(Emoji.base_directory, "*.{png,gif}"))
|
|
|
|
.sort
|
|
|
|
.map { |emoji| Emoji.create_from_path(emoji) }
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.base_directory
|
|
|
|
"public/#{base_url}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.base_url
|
|
|
|
db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
"uploads/#{db}/_emoji"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|