mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
FIX: use distributed mutex to prevent errors when uploading emojis in batches
This commit is contained in:
parent
f82e8ce5f1
commit
1e6f886886
2 changed files with 33 additions and 15 deletions
|
@ -13,8 +13,7 @@ class Admin::EmojisController < Admin::AdminController
|
|||
.gsub(/_{2,}/, '_')
|
||||
.downcase
|
||||
|
||||
# check the name doesn't already exist
|
||||
if Emoji.custom.detect { |e| e.name == name }
|
||||
if Emoji.exists?(name)
|
||||
render json: failed_json.merge(message: I18n.t("emoji.errors.name_already_exists", name: name)), status: 422
|
||||
else
|
||||
if emoji = Emoji.create_for(file, name)
|
||||
|
@ -28,7 +27,7 @@ class Admin::EmojisController < Admin::AdminController
|
|||
|
||||
def destroy
|
||||
name = params.require(:id)
|
||||
Emoji.custom.detect { |e| e.name == name }.try(:remove)
|
||||
Emoji[name].try(:remove)
|
||||
render nothing: true
|
||||
end
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
class Emoji
|
||||
include ActiveModel::SerializerSupport
|
||||
|
||||
EMOJIS_CUSTOM_LOCK ||= "_emojis_custom_lock_".freeze
|
||||
|
||||
attr_reader :path
|
||||
attr_accessor :name, :url
|
||||
|
||||
|
@ -13,9 +15,12 @@ class Emoji
|
|||
|
||||
def remove
|
||||
return if path.blank?
|
||||
if File.exists?(path)
|
||||
File.delete(path) rescue nil
|
||||
Emoji.clear_cache
|
||||
|
||||
DistributedMutex.new(EMOJIS_CUSTOM_LOCK).synchronize do
|
||||
if File.exists?(path)
|
||||
File.delete(path) rescue nil
|
||||
Emoji.clear_cache
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -31,6 +36,14 @@ class Emoji
|
|||
Discourse.cache.fetch("custom", family: "emoji") { load_custom }
|
||||
end
|
||||
|
||||
def self.exists?(name)
|
||||
Emoji[name].present?
|
||||
end
|
||||
|
||||
def self.[](name)
|
||||
Emoji.custom.detect { |e| e.name == name }
|
||||
end
|
||||
|
||||
def self.create_from_path(path)
|
||||
extension = File.extname(path)
|
||||
Emoji.new(path).tap do |e|
|
||||
|
@ -51,15 +64,19 @@ class Emoji
|
|||
def self.create_for(file, name)
|
||||
extension = File.extname(file.original_filename)
|
||||
path = "#{Emoji.base_directory}/#{name}#{extension}"
|
||||
# store the emoji
|
||||
FileUtils.mkdir_p(Pathname.new(path).dirname)
|
||||
File.open(path, "wb") { |f| f << file.tempfile.read }
|
||||
# clear the cache
|
||||
Emoji.clear_cache
|
||||
|
||||
DistributedMutex.new(EMOJIS_CUSTOM_LOCK).synchronize do
|
||||
# store the emoji
|
||||
FileUtils.mkdir_p(Pathname.new(path).dirname)
|
||||
File.open(path, "wb") { |f| f << file.tempfile.read }
|
||||
# clear the cache
|
||||
Emoji.clear_cache
|
||||
end
|
||||
|
||||
# launch resize job
|
||||
Jobs.enqueue(:resize_emoji, path: path)
|
||||
# return created emoji
|
||||
Emoji.custom.detect { |e| e.name == name }
|
||||
Emoji[name]
|
||||
end
|
||||
|
||||
def self.clear_cache
|
||||
|
@ -76,9 +93,11 @@ class Emoji
|
|||
end
|
||||
|
||||
def self.load_custom
|
||||
Dir.glob(File.join(Emoji.base_directory, "*.{png,gif}"))
|
||||
.sort
|
||||
.map { |emoji| Emoji.create_from_path(emoji) }
|
||||
DistributedMutex.new(EMOJIS_CUSTOM_LOCK).synchronize do
|
||||
Dir.glob(File.join(Emoji.base_directory, "*.{png,gif}"))
|
||||
.sort
|
||||
.map { |emoji| Emoji.create_from_path(emoji) }
|
||||
end
|
||||
end
|
||||
|
||||
def self.base_directory
|
||||
|
|
Loading…
Reference in a new issue