emoji_cli.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # frozen_string_literal: true
  2. require 'rubygems/package'
  3. require_relative '../../config/boot'
  4. require_relative '../../config/environment'
  5. require_relative 'cli_helper'
  6. module Mastodon
  7. class EmojiCLI < Thor
  8. def self.exit_on_failure?
  9. true
  10. end
  11. option :prefix
  12. option :suffix
  13. option :overwrite, type: :boolean
  14. option :unlisted, type: :boolean
  15. option :category
  16. desc 'import PATH', 'Import emoji from a TAR GZIP archive at PATH'
  17. long_desc <<-LONG_DESC
  18. Imports custom emoji from a TAR GZIP archive specified by PATH.
  19. Existing emoji will be skipped unless the --overwrite option
  20. is provided, in which case they will be overwritten.
  21. You can specify a --category under which the emojis will be
  22. grouped together.
  23. With the --prefix option, a prefix can be added to all
  24. generated shortcodes. Likewise, the --suffix option controls
  25. the suffix of all shortcodes.
  26. With the --unlisted option, the processed emoji will not be
  27. visible in the emoji picker (but still usable via other means)
  28. LONG_DESC
  29. def import(path)
  30. imported = 0
  31. skipped = 0
  32. failed = 0
  33. category = options[:category] ? CustomEmojiCategory.find_or_create_by(name: options[:category]) : nil
  34. Gem::Package::TarReader.new(Zlib::GzipReader.open(path)) do |tar|
  35. tar.each do |entry|
  36. next unless entry.file? && entry.full_name.end_with?('.png')
  37. shortcode = [options[:prefix], File.basename(entry.full_name, '.*'), options[:suffix]].compact.join
  38. custom_emoji = CustomEmoji.local.find_by(shortcode: shortcode)
  39. if custom_emoji && !options[:overwrite]
  40. skipped += 1
  41. next
  42. end
  43. custom_emoji ||= CustomEmoji.new(shortcode: shortcode, domain: nil)
  44. custom_emoji.image = StringIO.new(entry.read)
  45. custom_emoji.image_file_name = File.basename(entry.full_name)
  46. custom_emoji.visible_in_picker = !options[:unlisted]
  47. custom_emoji.category = category
  48. if custom_emoji.save
  49. imported += 1
  50. else
  51. failed += 1
  52. say('Failure/Error: ', :red)
  53. say(entry.full_name)
  54. say(' ' + custom_emoji.errors[:image].join(', '), :red)
  55. end
  56. end
  57. end
  58. puts
  59. say("Imported #{imported}, skipped #{skipped}, failed to import #{failed}", color(imported, skipped, failed))
  60. end
  61. option :category
  62. option :overwrite, type: :boolean
  63. desc 'export PATH', 'Export emoji to a TAR GZIP archive at PATH'
  64. long_desc <<-LONG_DESC
  65. Exports custom emoji to 'export.tar.gz' at PATH.
  66. The --category option dumps only the specified category.
  67. If this option is not specified, all emoji will be exported.
  68. The --overwrite option will overwrite an existing archive.
  69. LONG_DESC
  70. def export(path)
  71. exported = 0
  72. category = CustomEmojiCategory.find_by(name: options[:category])
  73. export_file_name = File.join(path, 'export.tar.gz')
  74. if File.file?(export_file_name) && !options[:overwrite]
  75. say("Archive already exists! Use '--overwrite' to overwrite it!")
  76. exit 1
  77. end
  78. if category.nil? && options[:category]
  79. say("Unable to find category '#{options[:category]}'!")
  80. exit 1
  81. end
  82. File.open(export_file_name, 'wb') do |file|
  83. Zlib::GzipWriter.wrap(file) do |gzip|
  84. Gem::Package::TarWriter.new(gzip) do |tar|
  85. scope = !options[:category] || category.nil? ? CustomEmoji.local : category.emojis
  86. scope.find_each do |emoji|
  87. say("Adding '#{emoji.shortcode}'...")
  88. tar.add_file_simple(emoji.shortcode + File.extname(emoji.image_file_name), 0o644, emoji.image_file_size) do |io|
  89. io.write Paperclip.io_adapters.for(emoji.image).read
  90. exported += 1
  91. end
  92. end
  93. end
  94. end
  95. end
  96. say("Exported #{exported}")
  97. end
  98. option :remote_only, type: :boolean
  99. desc 'purge', 'Remove all custom emoji'
  100. long_desc <<-LONG_DESC
  101. Removes all custom emoji.
  102. With the --remote-only option, only remote emoji will be deleted.
  103. LONG_DESC
  104. def purge
  105. scope = options[:remote_only] ? CustomEmoji.remote : CustomEmoji
  106. scope.in_batches.destroy_all
  107. say('OK', :green)
  108. end
  109. private
  110. def color(green, _yellow, red)
  111. if !green.zero? && red.zero?
  112. :green
  113. elsif red.zero?
  114. :yellow
  115. else
  116. :red
  117. end
  118. end
  119. end
  120. end