discourse/app/controllers/admin/emojis_controller.rb
Martin Brennan fac9185421
FEATURE: Move emoji uploader to use uppy (#14814)
This commit changes the emoji uploader to use the UppyUploadMixin,
and makes some minor changes to the emoji uploader (tightening the
copy for drag and drop and adding a percentage for the upload).

Since no other uppy upload mixin components have needed to upload
multiple files so far, this necessitated adding a tracker for the
in progress uploads so we know when to reset the uploader once all
uploads are complete.

At the moment, the emoji uploader cannot be used for direct S3 uploads
because the admin emoji controller creates other records and does other
magic with the emojis. At some point we need to factor this kind of thing
into the ExternalUploadManager.transform! action to complete external
uploads.
2021-11-04 11:31:58 +10:00

65 lines
1.7 KiB
Ruby

# frozen_string_literal: true
class Admin::EmojisController < Admin::AdminController
def index
render_serialized(Emoji.custom, EmojiSerializer, root: false)
end
# TODO (martin) Figure out a way that this kind of custom logic can
# be run in the ExternalUploadManager when a direct S3 upload is completed,
# related to preventDirectS3Uploads in the UppyUploadMixin.
def create
file = params[:file] || params[:files].first
name = params[:name] || File.basename(file.original_filename, ".*")
group = params[:group] ? params[:group].downcase : nil
hijack do
# fix the name
name = name.gsub(/[^a-z0-9]+/i, '_')
.gsub(/_{2,}/, '_')
.downcase
upload = UploadCreator.new(
file.tempfile,
file.original_filename,
type: 'custom_emoji'
).create_for(current_user.id)
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
good = false
failed_json.merge(errors: custom_emoji.errors.full_messages)
end
else
good = false
failed_json.merge(errors: upload.errors.full_messages)
end
render json: data.as_json, status: good ? 200 : 422
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
end
end