You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.6 KiB

  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. filename = File.basename(entry.full_name, '.*')
  38. # Skip macOS shadow files
  39. next if filename.start_with?('._')
  40. shortcode = [options[:prefix], filename, options[:suffix]].compact.join
  41. custom_emoji = CustomEmoji.local.find_by("LOWER(shortcode) = ?", shortcode.downcase)
  42. if custom_emoji && !options[:overwrite]
  43. skipped += 1
  44. next
  45. end
  46. custom_emoji ||= CustomEmoji.new(shortcode: shortcode, domain: nil)
  47. custom_emoji.image = StringIO.new(entry.read)
  48. custom_emoji.image_file_name = File.basename(entry.full_name)
  49. custom_emoji.visible_in_picker = !options[:unlisted]
  50. custom_emoji.category = category
  51. if custom_emoji.save
  52. imported += 1
  53. else
  54. failed += 1
  55. say('Failure/Error: ', :red)
  56. say(entry.full_name)
  57. say(' ' + custom_emoji.errors[:image].join(', '), :red)
  58. end
  59. end
  60. end
  61. puts
  62. say("Imported #{imported}, skipped #{skipped}, failed to import #{failed}", color(imported, skipped, failed))
  63. end
  64. option :category
  65. option :overwrite, type: :boolean
  66. desc 'export PATH', 'Export emoji to a TAR GZIP archive at PATH'
  67. long_desc <<-LONG_DESC
  68. Exports custom emoji to 'export.tar.gz' at PATH.
  69. The --category option dumps only the specified category.
  70. If this option is not specified, all emoji will be exported.
  71. The --overwrite option will overwrite an existing archive.
  72. LONG_DESC
  73. def export(path)
  74. exported = 0
  75. category = CustomEmojiCategory.find_by(name: options[:category])
  76. export_file_name = File.join(path, 'export.tar.gz')
  77. if File.file?(export_file_name) && !options[:overwrite]
  78. say("Archive already exists! Use '--overwrite' to overwrite it!")
  79. exit 1
  80. end
  81. if category.nil? && options[:category]
  82. say("Unable to find category '#{options[:category]}'!")
  83. exit 1
  84. end
  85. File.open(export_file_name, 'wb') do |file|
  86. Zlib::GzipWriter.wrap(file) do |gzip|
  87. Gem::Package::TarWriter.new(gzip) do |tar|
  88. scope = !options[:category] || category.nil? ? CustomEmoji.local : category.emojis
  89. scope.find_each do |emoji|
  90. say("Adding '#{emoji.shortcode}'...")
  91. tar.add_file_simple(emoji.shortcode + File.extname(emoji.image_file_name), 0o644, emoji.image_file_size) do |io|
  92. io.write Paperclip.io_adapters.for(emoji.image).read
  93. exported += 1
  94. end
  95. end
  96. end
  97. end
  98. end
  99. say("Exported #{exported}")
  100. end
  101. option :remote_only, type: :boolean
  102. desc 'purge', 'Remove all custom emoji'
  103. long_desc <<-LONG_DESC
  104. Removes all custom emoji.
  105. With the --remote-only option, only remote emoji will be deleted.
  106. LONG_DESC
  107. def purge
  108. scope = options[:remote_only] ? CustomEmoji.remote : CustomEmoji
  109. scope.in_batches.destroy_all
  110. say('OK', :green)
  111. end
  112. private
  113. def color(green, _yellow, red)
  114. if !green.zero? && red.zero?
  115. :green
  116. elsif red.zero?
  117. :yellow
  118. else
  119. :red
  120. end
  121. end
  122. end
  123. end