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