2014-04-14 15:55:57 -05:00
|
|
|
require_dependency "file_helper"
|
|
|
|
|
2013-11-11 11:51:14 -06:00
|
|
|
class AvatarUploadService
|
|
|
|
|
|
|
|
attr_accessor :source
|
2014-04-14 15:55:57 -05:00
|
|
|
attr_reader :filesize, :filename, :file
|
2013-11-11 11:51:14 -06:00
|
|
|
|
|
|
|
def initialize(file, source)
|
|
|
|
@source = source
|
2014-04-14 15:55:57 -05:00
|
|
|
@file, @filename, @filesize = construct(file)
|
2013-11-11 11:51:14 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def construct(file)
|
|
|
|
case source
|
|
|
|
when :url
|
2014-04-14 15:55:57 -05:00
|
|
|
tmp = FileHelper.download(file, SiteSetting.max_image_size_kb.kilobytes, "discourse-avatar")
|
|
|
|
[tmp, File.basename(URI.parse(file).path), File.size(tmp)]
|
2013-11-11 11:51:14 -06:00
|
|
|
when :image
|
2014-04-14 15:55:57 -05:00
|
|
|
[file.tempfile, file.original_filename, File.size(file.tempfile)]
|
2013-11-11 11:51:14 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|