2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2015-07-15 19:45:40 +00:00
|
|
|
|
|
|
|
class Service::Image::Zammad
|
|
|
|
|
2018-04-12 14:57:37 +00:00
|
|
|
API_HOST = 'https://images.zammad.com'.freeze
|
|
|
|
OPEN_TIMEOUT = 4
|
|
|
|
READ_TIMEOUT = 6
|
|
|
|
TOTAL_TIMEOUT = 6
|
2022-02-04 10:56:24 +00:00
|
|
|
DISABLE_IN_TEST_ENV = true
|
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?
|
|
|
|
|
2022-02-04 10:56:24 +00:00
|
|
|
return if Rails.env.test? && DISABLE_IN_TEST_ENV
|
|
|
|
|
2017-09-29 16:03:09 +00:00
|
|
|
email.downcase!
|
|
|
|
|
2021-05-12 11:37:44 +00:00
|
|
|
return if email.match?(%r{@example.com$})
|
2015-07-15 19:45:40 +00:00
|
|
|
|
|
|
|
# fetch image
|
|
|
|
response = UserAgent.post(
|
2018-04-12 14:57:37 +00:00
|
|
|
"#{API_HOST}/api/v1/person/image",
|
2015-07-15 19:45:40 +00:00
|
|
|
{
|
|
|
|
email: email,
|
|
|
|
},
|
|
|
|
{
|
2018-04-12 14:57:37 +00:00
|
|
|
open_timeout: OPEN_TIMEOUT,
|
|
|
|
read_timeout: READ_TIMEOUT,
|
|
|
|
total_timeout: TOTAL_TIMEOUT,
|
2021-07-20 13:31:46 +00:00
|
|
|
verify_ssl: true,
|
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}"
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
content: response.body,
|
2022-02-04 10:56:24 +00:00
|
|
|
mime_type: 'image/jpeg',
|
2015-07-15 19:45:40 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2022-02-04 10:56:24 +00:00
|
|
|
return if Rails.env.test? && DISABLE_IN_TEST_ENV
|
|
|
|
|
2015-07-15 19:45:40 +00:00
|
|
|
# strip, just use domain name
|
2022-02-04 10:56:24 +00:00
|
|
|
domain = domain.sub(%r{^.+?@(.+?)$}, '\1').downcase
|
2015-07-15 19:45:40 +00:00
|
|
|
|
2017-09-29 16:03:09 +00:00
|
|
|
return if domain == 'example.com'
|
|
|
|
|
2015-07-15 19:45:40 +00:00
|
|
|
# fetch org logo
|
|
|
|
response = UserAgent.post(
|
2018-04-12 14:57:37 +00:00
|
|
|
"#{API_HOST}/api/v1/organization/image",
|
2015-07-15 19:45:40 +00:00
|
|
|
{
|
|
|
|
domain: domain
|
|
|
|
},
|
|
|
|
{
|
2018-04-12 14:57:37 +00:00
|
|
|
open_timeout: OPEN_TIMEOUT,
|
|
|
|
read_timeout: READ_TIMEOUT,
|
|
|
|
total_timeout: TOTAL_TIMEOUT,
|
2021-07-20 13:31:46 +00:00
|
|
|
verify_ssl: true,
|
2015-07-15 19:45:40 +00:00
|
|
|
},
|
|
|
|
)
|
2022-02-04 10:56:24 +00:00
|
|
|
response_code = response.code
|
2015-07-15 19:45:40 +00:00
|
|
|
if !response.success?
|
2022-02-04 10:56:24 +00:00
|
|
|
Rails.logger.info "Can't fetch image for '#{domain}' (maybe no avatar available), http code: #{response_code}"
|
2015-07-15 19:45:40 +00:00
|
|
|
return
|
|
|
|
end
|
2022-02-04 10:56:24 +00:00
|
|
|
Rails.logger.info "Fetched image for '#{domain}', http code: #{response_code}"
|
2015-07-15 19:45:40 +00:00
|
|
|
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
content: response.body,
|
2022-02-04 10:56:24 +00:00
|
|
|
mime_type: 'image/png',
|
2015-07-15 19:45:40 +00:00
|
|
|
}
|
|
|
|
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
|