zammad.rb 2.1 KB

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