FEATURE: support email attachments

This commit is contained in:
Régis Hanol
2014-04-14 22:55:57 +02:00
parent ed6e2b1d79
commit 2505d18aa9
29 changed files with 432 additions and 538 deletions

34
lib/file_helper.rb Normal file
View File

@@ -0,0 +1,34 @@
class FileHelper
def self.is_image?(filename)
filename =~ images_regexp
end
def self.download(url, max_file_size, tmp_file_name)
raise Discourse::InvalidParameters unless url =~ /^https?:\/\//
extension = File.extname(URI.parse(url).path)
tmp = Tempfile.new([tmp_file_name, extension])
File.open(tmp.path, "wb") do |f|
avatar = open(url, "rb", read_timeout: 5)
while f.size <= max_file_size && data = avatar.read(max_file_size)
f.write(data)
end
avatar.close!
end
tmp
end
private
def self.images
@@images ||= Set.new ["jpg", "jpeg", "png", "gif", "tif", "tiff", "bmp"]
end
def self.images_regexp
@@images_regexp ||= /\.(#{images.to_a.join("|").gsub(".", "\.")})$/i
end
end