Monkey patch Active Storage to set content type when uploading to S3
[rails.git] / config / initializers / active_storage.rb
1 Rails.configuration.after_initialize do
2   require "active_storage/service/s3_service"
3   require_dependency "active_storage/variant"
4
5   module OpenStreetMap
6     module ActiveStorage
7       module Variant
8         private
9
10         def upload(image)
11           File.open(image.path, "r") { |file| service.upload(key, file, :content_type => content_type) }
12         end
13       end
14
15       module S3Service
16         def upload(key, io, content_type:, **options)
17           @upload_options[:content_type] = content_type
18           super(key, io, **options)
19           @upload_options.delete(:content_type)
20         end
21       end
22     end
23   end
24
25   ActiveStorage::Variant.prepend(OpenStreetMap::ActiveStorage::Variant)
26   ActiveStorage::Service::S3Service.prepend(OpenStreetMap::ActiveStorage::S3Service)
27
28   ActiveSupport::Reloader.to_complete do
29     ActiveStorage::Variant.prepend(OpenStreetMap::ActiveStorage::Variant)
30   end
31 end