[#2180] Fix MIME types

This commit is contained in:
Frederic Merizen 2018-12-03 18:49:17 +01:00
parent 045579f7ae
commit 477af84186

View file

@ -3,6 +3,7 @@ namespace :'2018_12_03_finish_piece_jointe_transfer' do
Class.new do Class.new do
def run def run
notify_dry_run notify_dry_run
fix_openstack_mime_types
notify_dry_run notify_dry_run
end end
@ -27,6 +28,44 @@ namespace :'2018_12_03_finish_piece_jointe_transfer' do
@verbose @verbose
end end
# For OpenStack, the content type cannot be forced dynamically from a direct download URL.
#
# The ActiveStorage-OpenStack adapter works around this by monkey patching ActiveStorage
# to statically set the correct MIME type on each OpenStack object.
#
# However, for objects that have been migrated from another storage, the content-type might
# be wrong, so we manually fix it.
def fix_openstack_mime_types
if !ActiveStorage::Blob.service.respond_to?(:change_content_type)
rake_puts "Not running on openstack, not fixing MIME types"
return
end
rake_puts "Fix MIME types"
bar = RakeProgressbar.new(ActiveStorage::Blob.count)
failed_keys = []
updated_keys = []
ActiveStorage::Blob.find_each do |blob|
if blob.identified? && blob.content_type.present?
updated_keys.push(blob.key)
if force?
if !blob.service.change_content_type(blob.key, blob.content_type)
failed_keys.push(blob.key)
end
end
end
bar.inc
end
bar.finished
if verbose?
rake_puts "Updated MIME Type for #{updated_keys.count} keys\n#{updated_keys.join(', ')}"
end
if failed_keys.present?
rake_puts "failed to update #{failed_keys.count} keys (dangling blob?)\n#{failed_keys.join(', ')}"
end
end
end.new.run end.new.run
end end
end end