mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: use hijack for emoji uploads
This commit is contained in:
parent
b963307f30
commit
5e90abfaea
@ -10,7 +10,7 @@ class Admin::EmojisController < Admin::AdminController
|
|||||||
file = params[:file] || params[:files].first
|
file = params[:file] || params[:files].first
|
||||||
name = params[:name] || File.basename(file.original_filename, ".*")
|
name = params[:name] || File.basename(file.original_filename, ".*")
|
||||||
|
|
||||||
Scheduler::Defer.later("Upload Emoji") do
|
hijack do
|
||||||
# fix the name
|
# fix the name
|
||||||
name = name.gsub(/[^a-z0-9]+/i, '_')
|
name = name.gsub(/[^a-z0-9]+/i, '_')
|
||||||
.gsub(/_{2,}/, '_')
|
.gsub(/_{2,}/, '_')
|
||||||
@ -22,6 +22,8 @@ class Admin::EmojisController < Admin::AdminController
|
|||||||
type: 'custom_emoji'
|
type: 'custom_emoji'
|
||||||
).create_for(current_user.id)
|
).create_for(current_user.id)
|
||||||
|
|
||||||
|
good = true
|
||||||
|
|
||||||
data =
|
data =
|
||||||
if upload.persisted?
|
if upload.persisted?
|
||||||
custom_emoji = CustomEmoji.new(name: name, upload: upload)
|
custom_emoji = CustomEmoji.new(name: name, upload: upload)
|
||||||
@ -30,16 +32,16 @@ class Admin::EmojisController < Admin::AdminController
|
|||||||
Emoji.clear_cache
|
Emoji.clear_cache
|
||||||
{ name: custom_emoji.name, url: custom_emoji.upload.url }
|
{ name: custom_emoji.name, url: custom_emoji.upload.url }
|
||||||
else
|
else
|
||||||
|
good = false
|
||||||
failed_json.merge(errors: custom_emoji.errors.full_messages)
|
failed_json.merge(errors: custom_emoji.errors.full_messages)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
good = false
|
||||||
failed_json.merge(errors: upload.errors.full_messages)
|
failed_json.merge(errors: upload.errors.full_messages)
|
||||||
end
|
end
|
||||||
|
|
||||||
MessageBus.publish("/uploads/emoji", data.as_json, user_ids: [current_user.id])
|
render json: data.as_json, status: good ? 200 : 422
|
||||||
end
|
end
|
||||||
|
|
||||||
render json: success_json
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
@ -11,14 +11,15 @@ RSpec.describe Admin::EmojisController do
|
|||||||
describe "#create" do
|
describe "#create" do
|
||||||
describe 'when upload is invalid' do
|
describe 'when upload is invalid' do
|
||||||
it 'should publish the right error' do
|
it 'should publish the right error' do
|
||||||
message = MessageBus.track_publish("/uploads/emoji") do
|
|
||||||
post "/admin/customize/emojis.json", params: {
|
|
||||||
name: 'test',
|
|
||||||
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/fake.jpg")
|
|
||||||
}
|
|
||||||
end.first
|
|
||||||
|
|
||||||
expect(message.data["errors"]).to eq([I18n.t('upload.images.size_not_found')])
|
post "/admin/customize/emojis.json", params: {
|
||||||
|
name: 'test',
|
||||||
|
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/fake.jpg")
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(response.status).to eq(422)
|
||||||
|
parsed = JSON.parse(response.body)
|
||||||
|
expect(parsed["errors"]).to eq([I18n.t('upload.images.size_not_found')])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -26,14 +27,14 @@ RSpec.describe Admin::EmojisController do
|
|||||||
it 'should publish the right error' do
|
it 'should publish the right error' do
|
||||||
CustomEmoji.create!(name: 'test', upload: upload)
|
CustomEmoji.create!(name: 'test', upload: upload)
|
||||||
|
|
||||||
message = MessageBus.track_publish("/uploads/emoji") do
|
post "/admin/customize/emojis.json", params: {
|
||||||
post "/admin/customize/emojis.json", params: {
|
name: 'test',
|
||||||
name: 'test',
|
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png")
|
||||||
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png")
|
}
|
||||||
}
|
|
||||||
end.first
|
|
||||||
|
|
||||||
expect(message.data["errors"]).to eq([
|
expect(response.status).to eq(422)
|
||||||
|
parsed = JSON.parse(response.body)
|
||||||
|
expect(parsed["errors"]).to eq([
|
||||||
"Name #{I18n.t('activerecord.errors.models.custom_emoji.attributes.name.taken')}"
|
"Name #{I18n.t('activerecord.errors.models.custom_emoji.attributes.name.taken')}"
|
||||||
])
|
])
|
||||||
end
|
end
|
||||||
@ -42,20 +43,22 @@ RSpec.describe Admin::EmojisController do
|
|||||||
it 'should allow an admin to add a custom emoji' do
|
it 'should allow an admin to add a custom emoji' do
|
||||||
Emoji.expects(:clear_cache)
|
Emoji.expects(:clear_cache)
|
||||||
|
|
||||||
message = MessageBus.track_publish("/uploads/emoji") do
|
post "/admin/customize/emojis.json", params: {
|
||||||
post "/admin/customize/emojis.json", params: {
|
name: 'test',
|
||||||
name: 'test',
|
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png")
|
||||||
file: fixture_file_upload("#{Rails.root}/spec/fixtures/images/logo.png")
|
}
|
||||||
}
|
|
||||||
end.first
|
|
||||||
|
|
||||||
custom_emoji = CustomEmoji.last
|
custom_emoji = CustomEmoji.last
|
||||||
upload = custom_emoji.upload
|
upload = custom_emoji.upload
|
||||||
|
|
||||||
expect(upload.original_filename).to eq('logo.png')
|
expect(upload.original_filename).to eq('logo.png')
|
||||||
expect(message.data["errors"]).to eq(nil)
|
|
||||||
expect(message.data["name"]).to eq(custom_emoji.name)
|
data = JSON.parse(response.body)
|
||||||
expect(message.data["url"]).to eq(upload.url)
|
|
||||||
|
expect(response.status).to eq(200)
|
||||||
|
expect(data["errors"]).to eq(nil)
|
||||||
|
expect(data["name"]).to eq(custom_emoji.name)
|
||||||
|
expect(data["url"]).to eq(upload.url)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user