trabajo-afectivo/lib/service/image/zammad.rb

88 lines
2.2 KiB
Ruby
Raw Normal View History

2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
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
def self.user(email)
raise Exceptions::UnprocessableEntity, 'no email given' if email.blank?
email.downcase!
return if email.match?(%r{@example.com$})
# fetch image
response = UserAgent.post(
2018-04-12 14:57:37 +00:00
"#{API_HOST}/api/v1/person/image",
{
email: email,
},
{
2018-04-12 14:57:37 +00:00
open_timeout: OPEN_TIMEOUT,
read_timeout: READ_TIMEOUT,
total_timeout: TOTAL_TIMEOUT,
verify_ssl: true,
},
)
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)
raise Exceptions::UnprocessableEntity, 'no domain given' if domain.blank?
# strip, just use domain name
domain = domain.sub(%r{^.+?@(.+?)$}, '\1')
domain.downcase!
return if domain == 'example.com'
# fetch org logo
response = UserAgent.post(
2018-04-12 14:57:37 +00:00
"#{API_HOST}/api/v1/organization/image",
{
domain: domain
},
{
2018-04-12 14:57:37 +00:00
open_timeout: OPEN_TIMEOUT,
read_timeout: READ_TIMEOUT,
total_timeout: TOTAL_TIMEOUT,
verify_ssl: true,
},
)
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)
return false if !image
# store image 1:1
product_logo = StaticAssets.store_raw(image[:content], image[:mime_type])
Setting.set('product_logo', product_logo)
true
end
end