The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

82 lines
2.3 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. desc 'import PATH', 'Import emoji from a TAR archive at PATH'
  16. long_desc <<-LONG_DESC
  17. Imports custom emoji from a TAR archive specified by PATH.
  18. Existing emoji will be skipped unless the --overwrite option
  19. is provided, in which case they will be overwritten.
  20. With the --prefix option, a prefix can be added to all
  21. generated shortcodes. Likewise, the --suffix option controls
  22. the suffix of all shortcodes.
  23. With the --unlisted option, the processed emoji will not be
  24. visible in the emoji picker (but still usable via other means)
  25. LONG_DESC
  26. def import(path)
  27. imported = 0
  28. skipped = 0
  29. failed = 0
  30. Gem::Package::TarReader.new(Zlib::GzipReader.open(path)) do |tar|
  31. tar.each do |entry|
  32. next unless entry.file? && entry.full_name.end_with?('.png')
  33. shortcode = [options[:prefix], File.basename(entry.full_name, '.*'), options[:suffix]].compact.join
  34. custom_emoji = CustomEmoji.local.find_by(shortcode: shortcode)
  35. if custom_emoji && !options[:overwrite]
  36. skipped += 1
  37. next
  38. end
  39. custom_emoji ||= CustomEmoji.new(shortcode: shortcode, domain: nil)
  40. custom_emoji.image = StringIO.new(entry.read)
  41. custom_emoji.image_file_name = File.basename(entry.full_name)
  42. custom_emoji.visible_in_picker = !options[:unlisted]
  43. if custom_emoji.save
  44. imported += 1
  45. else
  46. failed += 1
  47. say('Failure/Error: ', :red)
  48. say(entry.full_name)
  49. say(' ' + custom_emoji.errors[:image].join(', '), :red)
  50. end
  51. end
  52. end
  53. puts
  54. say("Imported #{imported}, skipped #{skipped}, failed to import #{failed}", color(imported, skipped, failed))
  55. end
  56. private
  57. def color(green, _yellow, red)
  58. if !green.zero? && red.zero?
  59. :green
  60. elsif red.zero?
  61. :yellow
  62. else
  63. :red
  64. end
  65. end
  66. end
  67. end