posts: store duration of animations and videos.

Start storing the duration of animations and videos in the `duration`
field on the media_assets table. This had to wait until 3d30bfd69 was
deployed, which had to wait until Postgres was upgraded in order to add
the duration column to the media_assets table without downtime.

Also add a fix script to backfill the duration on existing posts. Usage:

    TAGS=animated ./script/fixes/079_fix_duration.rb
This commit is contained in:
evazion
2021-10-06 19:59:19 -05:00
parent 2595f18b2f
commit 0731b07d27
2 changed files with 16 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
#!/usr/bin/env ruby
require_relative "../../config/environment"
tags = ENV.fetch("TAGS", "animated")
posts = Post.system_tag_match(tags)
assets = posts.joins(:media_asset).where(media_asset: { duration: nil })
posts.find_each do |post|
media_file = MediaFile.open(post.file(:original), frame_data: post.pixiv_ugoira_frame_data&.data)
post.media_asset.update!(duration: media_file.duration)
puts "id=#{post.id}, md5=#{post.md5}, ext=#{post.file_ext}, duration=#{post.media_asset.duration}"
rescue StandardError => e
puts "error[id=#{post.id}]: #{e}"
end