Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tootctl emoji export #13534

Merged
merged 8 commits into from
Apr 27, 2020
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 70 additions & 4 deletions lib/mastodon/emoji_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def self.exit_on_failure?
Existing emoji will be skipped unless the --overwrite option
is provided, in which case they will be overwritten.

You can specifiy a --category under which the emojis will be
You can specify a --category under which the emojis will be
grouped together.

With the --prefix option, a prefix can be added to all
Expand All @@ -33,17 +33,18 @@ def self.exit_on_failure?
With the --unlisted option, the processed emoji will not be
visible in the emoji picker (but still usable via other means)
LONG_DESC

def import(path)
imported = 0
skipped = 0
failed = 0
skipped = 0
lfuelling marked this conversation as resolved.
Show resolved Hide resolved
failed = 0
category = options[:category] ? CustomEmojiCategory.find_or_create_by(name: options[:category]) : nil

Gem::Package::TarReader.new(Zlib::GzipReader.open(path)) do |tar|
tar.each do |entry|
next unless entry.file? && entry.full_name.end_with?('.png')

shortcode = [options[:prefix], File.basename(entry.full_name, '.*'), options[:suffix]].compact.join
shortcode = [options[:prefix], File.basename(entry.full_name, '.*'), options[:suffix]].compact.join
custom_emoji = CustomEmoji.local.find_by(shortcode: shortcode)

if custom_emoji && !options[:overwrite]
Expand Down Expand Up @@ -72,13 +73,61 @@ def import(path)
say("Imported #{imported}, skipped #{skipped}, failed to import #{failed}", color(imported, skipped, failed))
end

option :category
option :shortcode, type: :boolean
option :overwrite, type: :boolean
desc 'export PATH', 'Export emoji to a TAR GZIP archive at PATH'
long_desc <<-LONG_DESC
Exports custom emoji to 'export.tar.gz' at PATH.

The --category option dumps only the specified category. If the given category doesn't exist, all emoji will be exported.

The --overwrite option will overwrite an existing archive.
LONG_DESC

def export(path)
exported = 0
skipped = 0
failed = 0
category = options[:category] ? CustomEmojiCategory.find_by(name: options[:category]) : nil
lfuelling marked this conversation as resolved.
Show resolved Hide resolved

export_file_name = File.join(path, 'export.tar.gz')
if options[:overwrite] || !File.file?(export_file_name)
lfuelling marked this conversation as resolved.
Show resolved Hide resolved
File.open(export_file_name, 'wb') do |file|
Zlib::GzipWriter.wrap(file) do |gzip|
Gem::Package::TarWriter.new(gzip) do |tar|
if !options[:category]
lfuelling marked this conversation as resolved.
Show resolved Hide resolved
say('Exporting all emoji...')
lfuelling marked this conversation as resolved.
Show resolved Hide resolved
CustomEmoji.local.all? do |emoji|
lfuelling marked this conversation as resolved.
Show resolved Hide resolved
add_emoji_to_file(emoji, exported, tar)
end
elsif !category.nil?
say("Exporting only '#{category.name}'...")
category.emojis&.each do |emoji|
lfuelling marked this conversation as resolved.
Show resolved Hide resolved
add_emoji_to_file(emoji, exported, tar)
end
else
say("Unable to find category '#{options[:category]}'!")
exit 1
end
end
end
end
puts
say("Exported #{exported}", color(exported, skipped, failed))
lfuelling marked this conversation as resolved.
Show resolved Hide resolved
else
say("Archive already exists! Use '--overwrite' to overwrite it!")
end
end

option :remote_only, type: :boolean
desc 'purge', 'Remove all custom emoji'
long_desc <<-LONG_DESC
Removes all custom emoji.

With the --remote-only option, only remote emoji will be deleted.
LONG_DESC

def purge
scope = options[:remote_only] ? CustomEmoji.remote : CustomEmoji
scope.in_batches.destroy_all
Expand All @@ -96,5 +145,22 @@ def color(green, _yellow, red)
:red
end
end

def export_file_name(emoji)
if options[:shortcode]
lfuelling marked this conversation as resolved.
Show resolved Hide resolved
emoji.shortcode + '.png'
lfuelling marked this conversation as resolved.
Show resolved Hide resolved
lfuelling marked this conversation as resolved.
Show resolved Hide resolved
else
emoji.image_file_name
end
end

def add_emoji_to_file(emoji, exported, tar)
emoji_file_name = export_file_name(emoji)
say("Adding '#{emoji.shortcode}' as '#{emoji_file_name}'...")
lfuelling marked this conversation as resolved.
Show resolved Hide resolved
tar.add_file_simple(emoji_file_name, 0o644, emoji.image_file_size) do |io|
io.write Paperclip.io_adapters.for(emoji.image).read
exported += 1
lfuelling marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end