media assets: add fix script to refresh metadata.

Add a script to go through every media asset and check the metadata
(width, height, duration, filesize, md5, EXIF metadata) and update it
if it's changed. This is necessary after upgrading ExifTool because the
metadata it returns may have changed.
This commit is contained in:
evazion
2022-10-30 04:31:23 -05:00
parent 5456a2ea29
commit d65a35d4ae
2 changed files with 40 additions and 0 deletions

View File

@@ -78,6 +78,12 @@ class MediaAsset < ApplicationRecord
end
def open_file
open_file!
rescue
nil
end
def open_file!
file = storage_service.open(file_path)
frame_delays = media_asset.frame_delays if media_asset.is_ugoira?
MediaFile.open(file, frame_delays: frame_delays, strict: false)

View File

@@ -0,0 +1,34 @@
#!/usr/bin/env ruby
require_relative "base"
with_confirmation do
condition = ENV.fetch("COND", "TRUE")
fix = ENV.fetch("FIX", "false").truthy?
MediaAsset.active.where(condition).find_each do |asset|
variant = asset.variant(:original)
media_file = variant.open_file
if media_file.nil?
puts ({ id: asset.id, error: "file doesn't exist", path: variant.file_path }).to_json
next
end
# Setting `file` updates the metadata if it's different.
asset.file = media_file
asset.media_metadata.file = media_file
old = asset.media_metadata.metadata_was.to_h
new = asset.media_metadata.metadata.to_h
metadata_changes = { added_metadata: (new.to_a - old.to_a).to_h, removed_metadata: (old.to_a - new.to_a).to_h }.compact_blank
puts ({ id: asset.id, **asset.changes, **metadata_changes }).to_json
if fix
asset.save! if asset.changed?
asset.media_metadata.save! if asset.media_metadata.changed?
end
media_file.close
end
end