discourse/app/controllers/admin/emojis_controller.rb

62 lines
1.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2014-12-22 18:12:26 -06:00
class Admin::EmojisController < Admin::AdminController
def index
render_serialized(Emoji.custom, EmojiSerializer, root: false)
end
def create
file = params[:file] || params[:files].first
name = params[:name] || File.basename(file.original_filename, ".*")
group = params[:group] ? params[:group].downcase : nil
2014-12-22 18:12:26 -06:00
2017-12-17 17:31:19 -06:00
hijack do
# fix the name
name = name.gsub(/[^a-z0-9]+/i, '_')
2017-07-27 20:20:09 -05:00
.gsub(/_{2,}/, '_')
.downcase
upload = UploadCreator.new(
file.tempfile,
file.original_filename,
type: 'custom_emoji'
).create_for(current_user.id)
2017-12-17 17:31:19 -06:00
good = true
data =
if upload.persisted?
custom_emoji = CustomEmoji.new(name: name, upload: upload, group: group)
if custom_emoji.save
Emoji.clear_cache
{ name: custom_emoji.name, url: custom_emoji.upload.url, group: group }
else
2017-12-17 17:31:19 -06:00
good = false
failed_json.merge(errors: custom_emoji.errors.full_messages)
end
else
2017-12-17 17:31:19 -06:00
good = false
failed_json.merge(errors: upload.errors.full_messages)
end
2017-12-17 17:31:19 -06:00
render json: data.as_json, status: good ? 200 : 422
2014-12-22 18:12:26 -06:00
end
end
def destroy
name = params.require(:id)
# NOTE: the upload will automatically be removed by the 'clean_up_uploads' job
CustomEmoji.find_by(name: name)&.destroy!
Emoji.clear_cache
Jobs.enqueue(:rebake_custom_emoji_posts, name: name)
render json: success_json
2014-12-22 18:12:26 -06:00
end
end