Skip to content

Commit

Permalink
[Analyzer] Fix issue with tempfile (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mth0158 committed Nov 25, 2024
1 parent 15524f2 commit 0ef64e4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 38 deletions.
36 changes: 12 additions & 24 deletions lib/active_storage_validations/analyzer/image_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,15 @@ def metadata

private

def image
def image(tempfile)
case @attachable
when ActiveStorage::Blob, String
blob = @attachable.is_a?(String) ? ActiveStorage::Blob.find_signed!(@attachable) : @attachable
tempfile_from_blob(blob) do |tempfile|
image_from_path(tempfile.path)
end
image_from_tempfile_path(tempfile, blob)
when Hash
io = @attachable[:io]
if io.is_a?(StringIO)
tempfile_from_io(io) do |tempfile|
image_from_path(tempfile.path)
end
image_from_tempfile_path(tempfile, io)
else
File.open(io) do |file|
image_from_path(file.path)
Expand All @@ -53,25 +49,17 @@ def image
end
end

def tempfile_from_blob(blob)
Tempfile.create(["ActiveStorage-#{blob.id}-", blob.filename.extension_with_delimiter], binmode: true) do |tempfile|
blob.download { |chunk| tempfile.write(chunk) }

tempfile.flush
tempfile.rewind
yield tempfile
def image_from_tempfile_path(tempfile, file_representation)
if file_representation.is_a?(ActiveStorage::Blob)
file_representation.download { |chunk| tempfile.write(chunk) }
else
IO.copy_stream(file_representation, tempfile)
file_representation.rewind
end
end

def tempfile_from_io(io)
Tempfile.create([File.basename(@attachable[:filename], '.*'), File.extname(@attachable[:filename])], binmode: true) do |tempfile|
IO.copy_stream(io, tempfile)
io.rewind

tempfile.flush
tempfile.rewind
yield tempfile
end
tempfile.flush
tempfile.rewind
image_from_path(tempfile.path)
end

def read_image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ def read_image
return {}
end

if image.valid?
yield image
else
logger.info "Skipping image analysis because ImageMagick doesn't support the file"
{}
Tempfile.create(binmode: true) do |tempfile|
if image(tempfile).valid?
yield image(tempfile)
else
logger.info "Skipping image analysis because ImageMagick doesn't support the file"
{}
end
end
rescue MiniMagick::Error => error
logger.error "Skipping image analysis due to an ImageMagick error: #{error.message}"
Expand Down
12 changes: 7 additions & 5 deletions lib/active_storage_validations/analyzer/image_analyzer/vips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ def read_image
return {}
end

if image
yield image
else
logger.info "Skipping image analysis because Vips doesn't support the file"
{}
Tempfile.create(binmode: true) do |tempfile|
if image(tempfile)
yield image(tempfile)
else
logger.info "Skipping image analysis because Vips doesn't support the file"
{}
end
end
rescue ::Vips::Error => error
logger.error "Skipping image analysis due to a Vips error: #{error.message}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def matches?(subject)
@subject = subject.is_a?(Class) ? subject.new : subject

is_a_valid_active_storage_attribute? &&
is_context_valid? &&
is_custom_message_valid? &&
is_valid_when_image_processable? &&
is_invalid_when_image_not_processable?
is_context_valid? &&
is_custom_message_valid? &&
is_valid_when_image_processable? &&
is_invalid_when_image_not_processable?
end

private
Expand Down

0 comments on commit 0ef64e4

Please sign in to comment.