Skip to content

Commit

Permalink
Merge pull request #5671 from avalonmediasystem/migrate_captions
Browse files Browse the repository at this point in the history
Add rake task to migrate IndexedFile captions to ActiveStorage
  • Loading branch information
masaball authored Feb 19, 2024
2 parents 23646c3 + 10d22e1 commit a6e08f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,6 @@ GEM
signet (>= 0.16, < 2.a)
hashdiff (1.0.1)
hashie (5.0.0)
highline (3.0.1)
hooks (0.4.1)
uber (~> 0.0.14)
htmlentities (4.3.4)
Expand Down Expand Up @@ -546,8 +545,6 @@ GEM
rdf-turtle
rdf-vocab (>= 0.8)
slop
license_header (0.0.4)
highline
link_header (0.0.8)
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
Expand Down Expand Up @@ -1047,7 +1044,6 @@ DEPENDENCIES
jquery-rails
jquery-ui-rails
ldp (~> 1.1.0)
license_header
listen
lograge
mail (> 2.8.0.1)
Expand Down
28 changes: 28 additions & 0 deletions lib/tasks/avalon_migrations.rake
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,33 @@ namespace :avalon do
collection.save!(validate: false)
end
end
desc "Migrate legacy IndexedFile captions to ActiveStorage as part of supporting upload of multiple captions files"
task caption_files: :environment do
count = 0
# Iterate through all caption IndexedFiles
IndexedFile.where("id: */captions").each do |caption_file|
# Retrieve parent master file
master_file_id = caption_file.id.split('/').first
master_file = MasterFile.find(master_file_id) rescue nil
next unless master_file && caption_file.present?
# Grab original file metadata from IndexedFile
filename = caption_file.original_name
content_type = caption_file.mime_type
# Create and populate new SupplementalFile record using original metadata
supplemental_file = SupplementalFile.new(label: filename, tags: ['caption'], language: 'eng')
supplemental_file.file.attach(io: ActiveFedora::FileIO.new(caption_file), filename: filename, content_type: content_type, identify: false)
supplemental_file.save
# Link new SupplementalFile to the MasterFile
master_file.supplemental_files += [supplemental_file]
# Delete legacy caption file
master_file.captions.content = ''
master_file.captions.original_name = ''
master_file.save

count += 1
end

count > 0 ? puts("Successfully updated #{count} records") : puts("All files are already up to date. No records updated.")
end
end
end

0 comments on commit a6e08f2

Please sign in to comment.