2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-02 17:41:57 +08:00
|
|
|
module Jobs
|
2019-10-02 12:01:53 +08:00
|
|
|
class MigrateCustomEmojis < ::Jobs::Onceoff
|
2017-02-02 17:41:57 +08:00
|
|
|
def execute_onceoff(args)
|
|
|
|
return if Rails.env.test?
|
|
|
|
|
2017-03-17 08:28:24 +08:00
|
|
|
Dir["#{Rails.root}/#{Emoji.base_directory}/*.{png,gif}"].each do |path|
|
|
|
|
name = File.basename(path, File.extname(path))
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2017-03-17 08:28:24 +08:00
|
|
|
File.open(path) do |file|
|
2017-05-11 06:16:57 +08:00
|
|
|
upload =
|
2017-03-17 08:28:24 +08:00
|
|
|
UploadCreator.new(file, File.basename(path), type: "custom_emoji").create_for(
|
2017-05-11 06:16:57 +08:00
|
|
|
Discourse.system_user.id,
|
|
|
|
)
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2017-03-17 08:28:24 +08:00
|
|
|
if upload.persisted?
|
|
|
|
custom_emoji = CustomEmoji.new(name: name, upload: upload)
|
|
|
|
|
|
|
|
if !custom_emoji.save
|
|
|
|
warn("Failed to create custom emoji '#{name}': #{custom_emoji.errors.full_messages}")
|
2017-02-02 17:41:57 +08:00
|
|
|
end
|
2017-03-17 08:28:24 +08:00
|
|
|
else
|
|
|
|
warn(
|
|
|
|
"Failed to create upload for '#{name}' custom emoji: #{upload.errors.full_messages}",
|
|
|
|
)
|
2017-02-02 17:41:57 +08:00
|
|
|
end
|
|
|
|
end
|
2017-03-17 08:28:24 +08:00
|
|
|
end
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2017-03-17 08:28:24 +08:00
|
|
|
Emoji.clear_cache
|
2017-02-02 17:41:57 +08:00
|
|
|
|
2022-11-02 03:05:13 +08:00
|
|
|
Post.where("cooked LIKE ?", "%#{Emoji.base_url}%").find_each { |post| post.rebake! }
|
2017-02-02 17:41:57 +08:00
|
|
|
end
|
2017-03-17 08:28:24 +08:00
|
|
|
|
|
|
|
def warn(message)
|
|
|
|
Rails.logger.warn(message)
|
|
|
|
end
|
2017-02-02 17:41:57 +08:00
|
|
|
end
|
|
|
|
end
|