All checks were successful
continuous-integration/drone/push Build is passing
Prevent future mistakes
41 lines
1.2 KiB
Ruby
41 lines
1.2 KiB
Ruby
module UserManager
|
|
class ImportRemoteAvatar < UserManagerService
|
|
def initialize(user:, avatar_url:)
|
|
@user = user
|
|
@avatar_url = avatar_url
|
|
end
|
|
|
|
def call
|
|
if import_remote_avatar
|
|
UserManager::UpdateAvatar.call(user: @user)
|
|
end
|
|
end
|
|
|
|
def import_remote_avatar
|
|
tempfile = Down.download(@avatar_url)
|
|
content_type = tempfile.content_type
|
|
unless %w[image/jpeg image/png].include?(content_type)
|
|
Rails.logger.warn { "Wrong content type of remote avatar for user #{user.cn}: '#{content_type}'" }
|
|
return false
|
|
end
|
|
|
|
img_data = UserManager::ProcessAvatar.call(io: tempfile)
|
|
tempfile = Tempfile.create
|
|
tempfile.binmode
|
|
tempfile.write(img_data)
|
|
tempfile.rewind
|
|
|
|
hash = Digest::SHA256.hexdigest(img_data)
|
|
ext = content_type == "image/png" ? "png" : "jpg"
|
|
filename = "#{hash}.#{ext}"
|
|
key = "users/#{@user.cn}/avatars/#{filename}"
|
|
|
|
@user.avatar.attach io: tempfile, key: key, filename: filename
|
|
rescue => e
|
|
Sentry.capture_exception(e) if Setting.sentry_enabled?
|
|
Rails.logger.warn "Importing remote avatar failed: \"#{e.message}\""
|
|
false
|
|
end
|
|
end
|
|
end
|