diff --git a/app/logical/exif_tool.rb b/app/logical/exif_tool.rb index 9b2db8a2b..f48368c76 100644 --- a/app/logical/exif_tool.rb +++ b/app/logical/exif_tool.rb @@ -4,8 +4,6 @@ require "shellwords" class ExifTool extend Memoist - class Error < StandardError; end - # @see https://exiftool.org/exiftool_pod.html#OPTIONS DEFAULT_OPTIONS = %q( -G1 -duplicates -unknown -struct --binary @@ -26,17 +24,11 @@ class ExifTool # @param options [String] the options to pass to exiftool # @return [Hash] the file's metadata def metadata(options: DEFAULT_OPTIONS) - output = shell!("exiftool #{options} -json #{file.path.shellescape}") + output = %x(exiftool #{options} -json #{file.path.shellescape}) json = JSON.parse(output).first json = json.except("SourceFile") json.with_indifferent_access end - def shell!(command) - output, status = Open3.capture2e(command) - raise Error, "#{command}` failed: #{output}" if !status.success? - output - end - memoize :metadata end diff --git a/test/unit/media_file_test.rb b/test/unit/media_file_test.rb index a9ed28065..3d6fd4b26 100644 --- a/test/unit/media_file_test.rb +++ b/test/unit/media_file_test.rb @@ -190,6 +190,40 @@ class MediaFileTest < ActiveSupport::TestCase end end + context "a corrupt GIF" do + should "still read the metadata" do + @file = MediaFile.open("test/files/test-corrupt.gif") + @metadata = @file.metadata + + assert_equal(true, @file.is_corrupt?) + assert_equal("File format error", @metadata["ExifTool:Error"]) + assert_equal("89a", @metadata["GIF:GIFVersion"]) + assert_equal(6, @metadata.count) + end + end + + context "a corrupt PNG" do + should "still read the metadata" do + @file = MediaFile.open("test/files/test-corrupt.png") + @metadata = @file.metadata + + assert_equal(true, @file.is_corrupt?) + assert_equal("Grayscale", @metadata["PNG:ColorType"]) + assert_equal(6, @metadata.count) + end + end + + context "a corrupt JPEG" do + should "still read the metadata" do + @file = MediaFile.open("test/files/test-corrupt.jpg") + @metadata = @file.metadata + + assert_equal(true, @file.is_corrupt?) + assert_equal(1, @metadata["File:ColorComponents"]) + assert_equal(7, @metadata.count) + end + end + context "a greyscale image without an embedded color profile" do should "successfully generate a thumbnail" do @image = MediaFile.open("test/files/test-grey-no-profile.jpg")