product_logo.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Service
  3. class SystemAssets
  4. class ProductLogo
  5. PRODUCT_LOGO_RESIZED = 2
  6. PRODUCT_LOGO_RAW = 1
  7. PRODUCT_LOGO_FIT_WIDTH = 200
  8. PRODUCT_LOGO_FIT_HEIGHT = 100
  9. def self.sendable_asset
  10. if (asset = custom_logo)
  11. return SendableAsset.new(
  12. content: asset.content,
  13. filename: asset.filename,
  14. type: asset.preferences['Content-Type']
  15. )
  16. end
  17. SendableAsset.new(
  18. content: Rails.public_path.join('assets/images/logo.svg').read,
  19. filename: 'logo.svg',
  20. type: 'image/svg+xml'
  21. )
  22. end
  23. def self.store_logo(file)
  24. clear_all
  25. store_one PRODUCT_LOGO_RAW, file, 'logo_raw'
  26. Time.current.to_i
  27. end
  28. def self.store(logo, logo_resize = nil) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  29. return if !logo && !logo_resize
  30. if logo&.match?(%r{<svg}i)
  31. return store_logo({ content: logo, mime_type: 'image/svg+xml' })
  32. end
  33. begin
  34. original_image = Rszr::Image.load_data logo if logo
  35. resized_image = if logo_resize
  36. Rszr::Image.load_data logo_resize
  37. else
  38. original_image.resize(PRODUCT_LOGO_FIT_WIDTH, PRODUCT_LOGO_FIT_HEIGHT)
  39. end
  40. raw_preprocessed = { content: logo, mime_type: "image/#{original_image.format}" } if logo
  41. resized_preprocessed = { content: logo_resize || resized_image.save_data, mime_type: "image/#{resized_image.format}" } if resized_image
  42. rescue Rszr::LoadError, Rszr::TransformationError
  43. return
  44. end
  45. clear_all
  46. raw = store_one PRODUCT_LOGO_RAW, raw_preprocessed, 'logo_raw'
  47. resized = store_one PRODUCT_LOGO_RESIZED, resized_preprocessed, 'logo'
  48. Time.current.to_i if resized || raw
  49. end
  50. def self.custom_logo
  51. [PRODUCT_LOGO_RESIZED, PRODUCT_LOGO_RAW]
  52. .lazy
  53. .map { |elem| Store.list(object: 'System::Logo', o_id: elem).first }
  54. .find(&:present?)
  55. end
  56. private_class_method :custom_logo
  57. def self.preprocess(data)
  58. return if !data&.match? %r{^data:image}i
  59. ImageHelper.data_url_attributes(data)
  60. end
  61. private_class_method :preprocess
  62. def self.clear_all
  63. [PRODUCT_LOGO_RAW, PRODUCT_LOGO_RESIZED].each do |elem|
  64. Store.remove(object: 'System::Logo', o_id: elem)
  65. end
  66. end
  67. private_class_method :clear_all
  68. def self.store_one(o_id, file, filename)
  69. return if !file
  70. Store.create!(
  71. object: 'System::Logo',
  72. o_id: o_id,
  73. data: file[:content],
  74. filename: filename,
  75. preferences: {
  76. 'Content-Type' => file[:mime_type]
  77. },
  78. created_by_id: 1,
  79. )
  80. end
  81. private_class_method :store_one
  82. end
  83. end
  84. end