Moved to bigdata service to get person based images.
This commit is contained in:
parent
2a101fc9de
commit
359f276c87
1 changed files with 47 additions and 19 deletions
|
@ -25,16 +25,11 @@ add an avatar based on auto detection (email address)
|
||||||
return if !data[:url]
|
return if !data[:url]
|
||||||
return if data[:url].empty?
|
return if data[:url].empty?
|
||||||
|
|
||||||
# dry gravatar lookup
|
|
||||||
hash = Digest::MD5.hexdigest(data[:url])
|
|
||||||
url = "http://www.gravatar.com/avatar/#{hash}.jpg?s=160&d=404"
|
|
||||||
logger.info "Avatar.auto_detection found #{data[:url]}: #{url}"
|
|
||||||
|
|
||||||
Avatar.add(
|
Avatar.add(
|
||||||
object: data[:object],
|
object: data[:object],
|
||||||
o_id: data[:o_id],
|
o_id: data[:o_id],
|
||||||
url: url,
|
url: data[:url],
|
||||||
source: 'gravatar.com',
|
source: 'zammad.com',
|
||||||
deletable: false,
|
deletable: false,
|
||||||
updated_by_id: 1,
|
updated_by_id: 1,
|
||||||
created_by_id: 1,
|
created_by_id: 1,
|
||||||
|
@ -96,16 +91,12 @@ add a avatar
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
# fetch image
|
# fetch image based on http url
|
||||||
if data[:url] && data[:url] =~ /^http/
|
if data[:url] && data[:url] =~ /^http/
|
||||||
|
|
||||||
# check if source ist already updated within last 2 minutes
|
# check if source ist already updated within last 2 minutes
|
||||||
if avatar_already_exists
|
if avatar_already_exists && avatar_already_exists.source_url == data[:url]
|
||||||
if avatar_already_exists.source_url == data[:url]
|
return if avatar_already_exists.updated_at > 2.minutes.ago
|
||||||
if avatar_already_exists.updated_at > 2.minutes.ago
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# twitter workaround to get bigger avatar images
|
# twitter workaround to get bigger avatar images
|
||||||
|
@ -127,13 +118,13 @@ add a avatar
|
||||||
logger.info "Can't fetch '#{data[:url]}' (maybe no avatar available), http code: #{response.code}"
|
logger.info "Can't fetch '#{data[:url]}' (maybe no avatar available), http code: #{response.code}"
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
logger.info "Fetch '#{data[:url]}', http code: #{response.code}"
|
logger.info "Fetchd image '#{data[:url]}', http code: #{response.code}"
|
||||||
mime_type = 'image'
|
mime_type = 'image'
|
||||||
if data[:url] =~ /\.png/i
|
if data[:url] =~ /\.png/i
|
||||||
mime_type = 'image/png'
|
mime_type = 'image/png'
|
||||||
end
|
end
|
||||||
if data[:url] =~ /\.(jpg|jpeg)/i
|
if data[:url] =~ /\.(jpg|jpeg)/i
|
||||||
mime_type = 'image/png'
|
mime_type = 'image/jpeg'
|
||||||
end
|
end
|
||||||
if !data[:resize]
|
if !data[:resize]
|
||||||
data[:resize] = {}
|
data[:resize] = {}
|
||||||
|
@ -145,12 +136,49 @@ add a avatar
|
||||||
end
|
end
|
||||||
data[:full][:content] = response.body
|
data[:full][:content] = response.body
|
||||||
data[:full][:mime_type] = mime_type
|
data[:full][:mime_type] = mime_type
|
||||||
|
|
||||||
|
# try zammad backend to find image based on email
|
||||||
|
elsif data[:url] && data[:url] =~ /@/
|
||||||
|
|
||||||
|
# check if source ist already updated within last 3 minutes
|
||||||
|
if avatar_already_exists && avatar_already_exists.source_url == data[:url]
|
||||||
|
return if avatar_already_exists.updated_at > 2.minutes.ago
|
||||||
|
end
|
||||||
|
|
||||||
|
# fetch image
|
||||||
|
response = UserAgent.post(
|
||||||
|
'https://bigdata.zammad.com/api/v1/person/image',
|
||||||
|
{
|
||||||
|
email: data[:url]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
open_timeout: 4,
|
||||||
|
read_timeout: 6,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if !response.success?
|
||||||
|
logger.info "Can't fetch image for '#{data[:url]}' (maybe no avatar available), http code: #{response.code}"
|
||||||
|
return
|
||||||
|
end
|
||||||
|
logger.info "Fetched image for '#{data[:url]}', http code: #{response.code}"
|
||||||
|
mime_type = 'image/jpeg'
|
||||||
|
if !data[:resize]
|
||||||
|
data[:resize] = {}
|
||||||
|
end
|
||||||
|
data[:resize][:content] = response.body
|
||||||
|
data[:resize][:mime_type] = mime_type
|
||||||
|
if !data[:full]
|
||||||
|
data[:full] = {}
|
||||||
|
end
|
||||||
|
data[:full][:content] = response.body
|
||||||
|
data[:full][:mime_type] = mime_type
|
||||||
end
|
end
|
||||||
|
|
||||||
# check if avatar need to be updated
|
# check if avatar need to be updated
|
||||||
record[:store_hash] = Digest::MD5.hexdigest( data[:resize][:content] )
|
record[:store_hash] = Digest::MD5.hexdigest( data[:resize][:content] )
|
||||||
if avatar_already_exists
|
if avatar_already_exists && avatar_already_exists.store_hash == record[:store_hash]
|
||||||
return if avatar_already_exists.store_hash == record[:store_hash]
|
avatar_already_exists.touch
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
# store images
|
# store images
|
||||||
|
@ -186,7 +214,7 @@ add a avatar
|
||||||
|
|
||||||
# update existing
|
# update existing
|
||||||
if avatar_already_exists
|
if avatar_already_exists
|
||||||
avatar_already_exists.update_attributes( record )
|
avatar_already_exists.update_attributes(record)
|
||||||
avatar = avatar_already_exists
|
avatar = avatar_already_exists
|
||||||
|
|
||||||
# add new one and set it as default
|
# add new one and set it as default
|
||||||
|
|
Loading…
Reference in a new issue