From b014a6647731cfa6bd4161c0af1a23de4ae8088e Mon Sep 17 00:00:00 2001 From: Igor Depolli Date: Fri, 5 Jul 2024 17:47:21 -0300 Subject: [PATCH] bugfix: Fix file name too long --- lib/vitals_image/analyzer/url_analyzer.rb | 16 ++++++++++++---- test/analyzer/url_analyzer_test.rb | 10 ++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/vitals_image/analyzer/url_analyzer.rb b/lib/vitals_image/analyzer/url_analyzer.rb index a78fa06..0a0d000 100644 --- a/lib/vitals_image/analyzer/url_analyzer.rb +++ b/lib/vitals_image/analyzer/url_analyzer.rb @@ -9,7 +9,7 @@ def self.accept?(source) end def analyze - file = download + file = download image = open(file) mime = Marcel::MimeType.for(Pathname.new file.path) @@ -32,9 +32,10 @@ def open(file) end def download - uri = URI.parse(source.key) - io = uri.open(ssl_verify_mode: ssl_verify_mode) - downloaded = Tempfile.new([File.basename(uri.path), File.extname(uri.path)], binmode: true) + uri = URI.parse(source.key) + io = uri.open(ssl_verify_mode: ssl_verify_mode) + uri_path = reduce_uri_path(uri.path) + downloaded = Tempfile.new([File.basename(uri_path), File.extname(uri_path)], binmode: true) if io.is_a?(Tempfile) FileUtils.mv io.path, downloaded.path @@ -46,6 +47,13 @@ def download downloaded end + def reduce_uri_path(uri_path) + filename, extension = uri_path.split(".") + filename = filename[0..200] + + [filename, extension].join(".") + end + def ssl_verify_mode VitalsImage.skip_ssl_verification ? OpenSSL::SSL::VERIFY_NONE : nil end diff --git a/test/analyzer/url_analyzer_test.rb b/test/analyzer/url_analyzer_test.rb index 4f6e98b..56b8f73 100644 --- a/test/analyzer/url_analyzer_test.rb +++ b/test/analyzer/url_analyzer_test.rb @@ -39,5 +39,15 @@ class Analyzer::UrlAnalyzerTest < ActiveSupport::TestCase assert_not vitals_image_sources(:invalid).identified end end + + test "file name too long" do + vitals_image_sources(:dog).update_column :key, "https://festalab-fixtures.s3.amazonaws.com/dog-#{'a' * 255}.jpg" + + with_image_library(:vips) do + assert_nothing_raised do + Analyzer::UrlAnalyzer.new(vitals_image_sources(:dog)).analyze + end + end + end end end