mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Refactor Users#upload_avatar method
Moved avatar file upload to ```AvatarUploadService``` class and ```AvatarUploadPolicy``` Address review comments + require missing file in spec
This commit is contained in:
43
lib/avatar_upload_service.rb
Normal file
43
lib/avatar_upload_service.rb
Normal file
@@ -0,0 +1,43 @@
|
||||
class AvatarUploadService
|
||||
|
||||
attr_accessor :source
|
||||
attr_reader :filesize, :file
|
||||
|
||||
def initialize(file, source)
|
||||
@source = source
|
||||
@file , @filesize = construct(file)
|
||||
end
|
||||
|
||||
def construct(file)
|
||||
case source
|
||||
when :url
|
||||
build_from_url(file)
|
||||
when :image
|
||||
[file, File.size(file.tempfile)]
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_from_url(url)
|
||||
temp = ::UriAdapter.new(url)
|
||||
return temp.build_uploaded_file, temp.file_size
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class AvatarUploadPolicy
|
||||
|
||||
def initialize(avatar)
|
||||
@avatar = avatar
|
||||
end
|
||||
|
||||
def max_size_kb
|
||||
SiteSetting.max_image_size_kb * 1024
|
||||
end
|
||||
|
||||
def too_big?
|
||||
@avatar.filesize > max_size_kb
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user