2014-04-14 16:55:57 -04:00
|
|
|
require_dependency "file_helper"
|
|
|
|
|
2013-11-11 12:51:14 -05:00
|
|
|
class AvatarUploadService
|
|
|
|
|
|
|
|
attr_accessor :source
|
2014-04-14 16:55:57 -04:00
|
|
|
attr_reader :filesize, :filename, :file
|
2013-11-11 12:51:14 -05:00
|
|
|
|
|
|
|
def initialize(file, source)
|
|
|
|
@source = source
|
2014-04-14 16:55:57 -04:00
|
|
|
@file, @filename, @filesize = construct(file)
|
2013-11-11 12:51:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def construct(file)
|
|
|
|
case source
|
|
|
|
when :url
|
2014-04-14 16:55:57 -04: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 12:51:14 -05:00
|
|
|
when :image
|
2014-04-14 16:55:57 -04:00
|
|
|
[file.tempfile, file.original_filename, File.size(file.tempfile)]
|
2013-11-11 12:51:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|