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")
|
2015-02-03 12:44:18 -05:00
|
|
|
[tmp, File.basename(URI.parse(file).path), tmp.size]
|
2013-11-11 12:51:14 -05:00
|
|
|
when :image
|
2015-02-03 12:44:18 -05:00
|
|
|
[file.tempfile, file.original_filename, file.tempfile.size]
|
2013-11-11 12:51:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|