Use ExifTool to get the dimensions of Flash files instead of calculating it ourselves. Avoids copying third-party code. Fixes a bug where Flash files with fractional dimensions (e.g. 607.6 x 756.6) had their dimensions rounded down instead of rounded up. Fixes another bug where Flash files could return negative dimensions. This happened for two files: * https://danbooru.donmai.us/media_assets/228662 (-179.2 x -339.2) * https://danbooru.donmai.us/media_assets/228664 (-179.2 x -339.2) Now we round these up to 1x1. This is still wrong, but it's less wrong than before.
36 lines
1.2 KiB
Ruby
Executable File
36 lines
1.2 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
require_relative "base"
|
|
|
|
CurrentUser.user = User.system
|
|
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
|
|
asset.post.assign_attributes(image_width: asset.image_width, image_height: asset.image_height, file_ext: asset.file_ext, file_size: asset.file_size) if asset.post.present?
|
|
|
|
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.post.save! if asset.post&.changed?
|
|
asset.save! if asset.changed?
|
|
asset.media_metadata.save! if asset.media_metadata.changed?
|
|
end
|
|
|
|
media_file.close
|
|
end
|