Skip to content

Commit

Permalink
[#2180] Fix MIME types
Browse files Browse the repository at this point in the history
  • Loading branch information
fredZen committed Dec 3, 2018
1 parent 1071286 commit d7fd055
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/tasks/2018_12_03_finish_piece_jointe_transfer.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace :'2018_12_03_finish_piece_jointe_transfer' do
def run
check_env
notify_dry_run
fix_openstack_mime_types
notify_dry_run
end

Expand Down Expand Up @@ -34,6 +35,40 @@ namespace :'2018_12_03_finish_piece_jointe_transfer' do

@verbose
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
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
end

0 comments on commit d7fd055

Please sign in to comment.