Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions app/models/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,10 @@ class Image < ApplicationRecord
belongs_to :user
has_one_attached :image

after_commit :strip_exif, on: :create
validates :image, attached: true

private

def strip_exif
original_image = image
copied_image = MiniMagick::Image.read(original_image.download)
copied_image.strip

ext = File.extname(original_image.filename.to_s)
timestamp = Time.current.strftime('%Y%m%d%H%M%S%L')
File.open(copied_image.path) do |file|
original_image.attach(io: file, filename: "#{user.id}_#{timestamp}#{ext}")
end
rescue StandardError => e
Rails.logger.error("Failed to strip EXIF: #{e.message}")
ensure
copied_image&.destroy!
def image=(attachable)
MiniMagick::Image.new(attachable.tempfile.path).strip if attachable.respond_to?(:tempfile)
super
end
end
9 changes: 7 additions & 2 deletions test/integration/api/image_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ class API::ImageTest < ActionDispatch::IntegrationTest
post api_image_path(format: :json), params: { file: image_uploaded }
assert_response :created

saved_image = Image.order(:created_at).last
processed_image = MiniMagick::Image.read(saved_image.image.download)
saved_image = Image.last

api_response = JSON.parse(response.body)
returned_url = api_response['url']
expected_blob = saved_image.image.blob
assert_includes(returned_url, expected_blob.signed_id)

processed_image = MiniMagick::Image.read(saved_image.image.download)
assert_empty processed_image.exif
end
end
15 changes: 15 additions & 0 deletions test/models/image_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'test_helper'

class ImageTest < ActiveSupport::TestCase
test 'remove exif data when image is updated' do
image_path = Rails.root.join('test/fixtures/files/articles/ogp_images/test.jpg')
image = Image.create!(user: users(:hajime), image: Rack::Test::UploadedFile.new(image_path, 'image/jpeg'))

image.update!(image: Rack::Test::UploadedFile.new(image_path, 'image/jpeg'))

updated_image = MiniMagick::Image.read(image.image.download)
assert_empty updated_image.exif
end
end
Loading