2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2015-07-15 19:45:40 +00:00
|
|
|
|
|
|
|
class Service::Image::Zammad
|
|
|
|
|
|
|
|
# rubocop:disable Style/ClassVars
|
2015-07-16 06:16:16 +00:00
|
|
|
@@api_host = 'https://images.zammad.com'
|
2015-07-15 19:45:40 +00:00
|
|
|
@@open_timeout = 4
|
|
|
|
@@read_timeout = 6
|
2016-01-28 09:34:41 +00:00
|
|
|
@@total_timeout = 6
|
2015-07-15 19:45:40 +00:00
|
|
|
|
|
|
|
def self.user(email)
|
2017-09-29 16:03:09 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'no email given' if email.blank?
|
|
|
|
|
|
|
|
email.downcase!
|
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
return if email.match?(/@example.com$/)
|
2015-07-15 19:45:40 +00:00
|
|
|
|
|
|
|
# fetch image
|
|
|
|
response = UserAgent.post(
|
|
|
|
"#{@@api_host}/api/v1/person/image",
|
|
|
|
{
|
|
|
|
email: email,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
open_timeout: @@open_timeout,
|
|
|
|
read_timeout: @@read_timeout,
|
2016-01-21 01:07:50 +00:00
|
|
|
total_timeout: @@total_timeout,
|
2015-07-15 19:45:40 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if !response.success?
|
|
|
|
Rails.logger.info "Can't fetch image for '#{email}' (maybe no avatar available), http code: #{response.code}"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
Rails.logger.info "Fetched image for '#{email}', http code: #{response.code}"
|
|
|
|
mime_type = 'image/jpeg'
|
|
|
|
{
|
|
|
|
content: response.body,
|
|
|
|
mime_type: mime_type,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.organization(domain)
|
2017-09-29 16:03:09 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, 'no domain given' if domain.blank?
|
2015-07-15 19:45:40 +00:00
|
|
|
|
|
|
|
# strip, just use domain name
|
|
|
|
domain = domain.sub(/^.+?@(.+?)$/, '\1')
|
|
|
|
|
2017-09-29 16:03:09 +00:00
|
|
|
domain.downcase!
|
|
|
|
return if domain == 'example.com'
|
|
|
|
|
2015-07-15 19:45:40 +00:00
|
|
|
# fetch org logo
|
|
|
|
response = UserAgent.post(
|
|
|
|
"#{@@api_host}/api/v1/organization/image",
|
|
|
|
{
|
|
|
|
domain: domain
|
|
|
|
},
|
|
|
|
{
|
|
|
|
open_timeout: @@open_timeout,
|
|
|
|
read_timeout: @@read_timeout,
|
2016-01-21 01:07:50 +00:00
|
|
|
total_timeout: @@total_timeout,
|
2015-07-15 19:45:40 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if !response.success?
|
|
|
|
Rails.logger.info "Can't fetch image for '#{domain}' (maybe no avatar available), http code: #{response.code}"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
Rails.logger.info "Fetched image for '#{domain}', http code: #{response.code}"
|
|
|
|
mime_type = 'image/png'
|
|
|
|
|
|
|
|
{
|
|
|
|
content: response.body,
|
|
|
|
mime_type: mime_type,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.organization_suggest(domain)
|
2015-07-16 06:27:17 +00:00
|
|
|
image = organization(domain)
|
2015-07-15 19:45:40 +00:00
|
|
|
return false if !image
|
|
|
|
|
|
|
|
# store image 1:1
|
2016-01-21 01:07:50 +00:00
|
|
|
product_logo = StaticAssets.store_raw(image[:content], image[:mime_type])
|
2015-07-15 19:45:40 +00:00
|
|
|
Setting.set('product_logo', product_logo)
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|