emoji.rb 4.5 KB

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