2017-05-11 06:16:57 +08:00
|
|
|
require_dependency 'upload_creator'
|
|
|
|
|
2014-12-23 08:12:26 +08: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, ".*")
|
|
|
|
|
2015-05-20 22:45:31 +08:00
|
|
|
Scheduler::Defer.later("Upload Emoji") do
|
|
|
|
# fix the name
|
|
|
|
name = name.gsub(/[^a-z0-9]+/i, '_')
|
|
|
|
.gsub(/_{2,}/, '_')
|
|
|
|
.downcase
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
upload = UploadCreator.new(
|
2017-02-02 17:41:57 +08:00
|
|
|
file.tempfile,
|
|
|
|
file.original_filename,
|
2017-05-11 06:16:57 +08:00
|
|
|
type: 'custom_emoji'
|
|
|
|
).create_for(current_user.id)
|
2017-02-02 17:41:57 +08:00
|
|
|
|
|
|
|
data =
|
|
|
|
if upload.persisted?
|
|
|
|
custom_emoji = CustomEmoji.new(name: name, upload: upload)
|
|
|
|
|
|
|
|
if custom_emoji.save
|
|
|
|
Emoji.clear_cache
|
|
|
|
{ name: custom_emoji.name, url: custom_emoji.upload.url }
|
|
|
|
else
|
|
|
|
failed_json.merge(errors: custom_emoji.errors.full_messages)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
failed_json.merge(errors: upload.errors.full_messages)
|
|
|
|
end
|
2015-05-20 22:45:31 +08:00
|
|
|
|
|
|
|
MessageBus.publish("/uploads/emoji", data.as_json, user_ids: [current_user.id])
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
2015-05-20 22:45:31 +08:00
|
|
|
render json: success_json
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
name = params.require(:id)
|
2017-02-02 17:41:57 +08:00
|
|
|
|
|
|
|
custom_emoji = CustomEmoji.find_by(name: name)
|
|
|
|
raise Discourse::InvalidParameters unless custom_emoji
|
|
|
|
|
|
|
|
CustomEmoji.transaction do
|
|
|
|
custom_emoji.upload.destroy!
|
|
|
|
custom_emoji.destroy!
|
|
|
|
end
|
|
|
|
|
|
|
|
Emoji.clear_cache
|
|
|
|
|
|
|
|
Jobs.enqueue(:rebake_custom_emoji_posts, name: name)
|
|
|
|
|
|
|
|
render json: success_json
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|