zammad.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Service::Image::Zammad
  3. # rubocop:disable Style/ClassVars
  4. @@api_host = 'https://images.zammad.com'
  5. @@open_timeout = 4
  6. @@read_timeout = 6
  7. @@total_timeout = 6
  8. def self.user(email)
  9. raise Exceptions::UnprocessableEntity, 'no email given' if email.blank?
  10. email.downcase!
  11. return if email =~ /@example.com$/
  12. # fetch image
  13. response = UserAgent.post(
  14. "#{@@api_host}/api/v1/person/image",
  15. {
  16. email: email,
  17. },
  18. {
  19. open_timeout: @@open_timeout,
  20. read_timeout: @@read_timeout,
  21. total_timeout: @@total_timeout,
  22. },
  23. )
  24. if !response.success?
  25. Rails.logger.info "Can't fetch image for '#{email}' (maybe no avatar available), http code: #{response.code}"
  26. return
  27. end
  28. Rails.logger.info "Fetched image for '#{email}', http code: #{response.code}"
  29. mime_type = 'image/jpeg'
  30. {
  31. content: response.body,
  32. mime_type: mime_type,
  33. }
  34. end
  35. def self.organization(domain)
  36. raise Exceptions::UnprocessableEntity, 'no domain given' if domain.blank?
  37. # strip, just use domain name
  38. domain = domain.sub(/^.+?@(.+?)$/, '\1')
  39. domain.downcase!
  40. return if domain == 'example.com'
  41. # fetch org logo
  42. response = UserAgent.post(
  43. "#{@@api_host}/api/v1/organization/image",
  44. {
  45. domain: domain
  46. },
  47. {
  48. open_timeout: @@open_timeout,
  49. read_timeout: @@read_timeout,
  50. total_timeout: @@total_timeout,
  51. },
  52. )
  53. if !response.success?
  54. Rails.logger.info "Can't fetch image for '#{domain}' (maybe no avatar available), http code: #{response.code}"
  55. return
  56. end
  57. Rails.logger.info "Fetched image for '#{domain}', http code: #{response.code}"
  58. mime_type = 'image/png'
  59. {
  60. content: response.body,
  61. mime_type: mime_type,
  62. }
  63. end
  64. def self.organization_suggest(domain)
  65. image = organization(domain)
  66. return false if !image
  67. # store image 1:1
  68. product_logo = StaticAssets.store_raw(image[:content], image[:mime_type])
  69. Setting.set('product_logo', product_logo)
  70. true
  71. end
  72. end