From dc73241bd9a529edc5573e10b5899cb47ca479ae Mon Sep 17 00:00:00 2001 From: Yamagishi Kazutoshi Date: Tue, 5 Jun 2018 01:58:36 +0900 Subject: Detect extname from Content-Type (#7733) --- app/models/concerns/remotable.rb | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'app/models/concerns') diff --git a/app/models/concerns/remotable.rb b/app/models/concerns/remotable.rb index c17f04776..dc2e12b67 100644 --- a/app/models/concerns/remotable.rb +++ b/app/models/concerns/remotable.rb @@ -24,14 +24,16 @@ 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['content-type']) + extname = detect_extname_from_content_type(content_type) + + if extname.nil? + matches = response.headers['content-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 +59,22 @@ 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? + + type.extensions.first + end + + def parse_content_type(content_type) + return if content_type.nil? + + content_type.split(/\s*;\s*/).first + end end -- cgit From 5fb013878fa7cb82c887d5215b2d6c8294db6b21 Mon Sep 17 00:00:00 2001 From: nightpool Date: Mon, 4 Jun 2018 16:20:12 -0400 Subject: Fix context performance by partially reverting #7083 (#7734) * Fix context performance by partially reverting #7083 * Fix code style issue * fix off-by-1 error in thread limits * code style fix --- app/models/concerns/status_threading_concern.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'app/models/concerns') diff --git a/app/models/concerns/status_threading_concern.rb b/app/models/concerns/status_threading_concern.rb index 1ba8fc693..fa441469c 100644 --- a/app/models/concerns/status_threading_concern.rb +++ b/app/models/concerns/status_threading_concern.rb @@ -51,12 +51,16 @@ module StatusThreadingConcern end def descendant_statuses(limit, max_child_id, since_child_id, depth) - Status.find_by_sql([<<-SQL.squish, id: id, limit: limit, max_child_id: max_child_id, since_child_id: since_child_id, depth: depth]) + # use limit + 1 and depth + 1 because 'self' is included + depth += 1 if depth.present? + limit += 1 if limit.present? + + descendants_with_self = Status.find_by_sql([<<-SQL.squish, id: id, limit: limit, max_child_id: max_child_id, since_child_id: since_child_id, depth: depth]) WITH RECURSIVE search_tree(id, path) AS ( SELECT id, ARRAY[id] FROM statuses - WHERE in_reply_to_id = :id AND COALESCE(id < :max_child_id, TRUE) AND COALESCE(id > :since_child_id, TRUE) + WHERE id = :id AND COALESCE(id < :max_child_id, TRUE) AND COALESCE(id > :since_child_id, TRUE) UNION ALL SELECT statuses.id, path || statuses.id FROM search_tree @@ -68,6 +72,8 @@ module StatusThreadingConcern ORDER BY path LIMIT :limit SQL + + descendants_with_self - [self] end def find_statuses_from_tree_path(ids, account) -- cgit From b7b331ad0dd061b55435265009af51cdeef29fdc Mon Sep 17 00:00:00 2001 From: Yamagishi Kazutoshi Date: Thu, 7 Jun 2018 03:49:39 +0900 Subject: Add missing dot for remote image (#7751) --- app/models/concerns/remotable.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'app/models/concerns') diff --git a/app/models/concerns/remotable.rb b/app/models/concerns/remotable.rb index dc2e12b67..90c6894e0 100644 --- a/app/models/concerns/remotable.rb +++ b/app/models/concerns/remotable.rb @@ -69,7 +69,11 @@ module Remotable return if type.nil? - type.extensions.first + extname = type.extensions.first + + return if extname.nil? + + ".#{extname}" end def parse_content_type(content_type) -- cgit From 683707839f6e61df95a4958675883c8f80aa6a84 Mon Sep 17 00:00:00 2001 From: Yamagishi Kazutoshi Date: Thu, 7 Jun 2018 03:50:07 +0900 Subject: Fix fetch of remote image with multiple Content-Type headers (#7749) --- app/models/concerns/remotable.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'app/models/concerns') diff --git a/app/models/concerns/remotable.rb b/app/models/concerns/remotable.rb index 90c6894e0..c17f19a60 100644 --- a/app/models/concerns/remotable.rb +++ b/app/models/concerns/remotable.rb @@ -24,13 +24,14 @@ module Remotable Request.new(:get, url).perform do |response| next if response.code != 200 - content_type = parse_content_type(response.headers['content-type']) + content_type = parse_content_type(response.headers.get('content-type').last) extname = detect_extname_from_content_type(content_type) if extname.nil? - matches = response.headers['content-disposition']&.match(/filename="([^"]*)"/) - filename = matches.nil? ? parsed_url.path.split('/').last : matches[1] - extname = filename.nil? ? '' : File.extname(filename) + 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) -- cgit