diff options
author | David Yip <yipdw@member.fsf.org> | 2018-06-07 05:13:49 -0500 |
---|---|---|
committer | David Yip <yipdw@member.fsf.org> | 2018-06-07 05:13:49 -0500 |
commit | 8142bd2553e7819722fdfc401e06cb10eeddd230 (patch) | |
tree | d771e65a58d3c69ba1c08c3fc145f24b5b806274 /app/models/concerns/remotable.rb | |
parent | f8d50a40701f344ea7c2a0e5475bbcbc727ef930 (diff) | |
parent | 2304d52599bfd3a907931971a94b9b68f229ab0a (diff) |
Merge remote-tracking branch 'tootsuite/master' into merge-upstream
Conflicts: app/models/status.rb The conflict in the Status model was due to https://github.com/tootsuite/mastodon/commit/5bf500338478f819a65d25636a0af61a482972d3. It was resolved by accepting tootsuite's changes.
Diffstat (limited to 'app/models/concerns/remotable.rb')
-rw-r--r-- | app/models/concerns/remotable.rb | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/app/models/concerns/remotable.rb b/app/models/concerns/remotable.rb index c17f04776..c17f19a60 100644 --- a/app/models/concerns/remotable.rb +++ b/app/models/concerns/remotable.rb @@ -24,14 +24,17 @@ module Remotable Request.new(:get, url).perform do |response| next if response.code != 200 - matches = response.headers['content-disposition']&.match(/filename="([^"]*)"/) - filename = matches.nil? ? parsed_url.path.split('/').last : matches[1] + content_type = parse_content_type(response.headers.get('content-type').last) + extname = detect_extname_from_content_type(content_type) + + if extname.nil? + disposition = response.headers.get('content-disposition').last + matches = disposition&.match(/filename="([^"]*)"/) + filename = matches.nil? ? parsed_url.path.split('/').last : matches[1] + extname = filename.nil? ? '' : File.extname(filename) + end + basename = SecureRandom.hex(8) - extname = if filename.nil? - '' - else - File.extname(filename) - end send("#{attachment_name}=", StringIO.new(response.body_with_limit(limit))) send("#{attachment_name}_file_name=", basename + extname) @@ -57,4 +60,26 @@ module Remotable end end end + + private + + def detect_extname_from_content_type(content_type) + return if content_type.nil? + + type = MIME::Types[content_type].first + + return if type.nil? + + extname = type.extensions.first + + return if extname.nil? + + ".#{extname}" + end + + def parse_content_type(content_type) + return if content_type.nil? + + content_type.split(/\s*;\s*/).first + end end |