From 0731b07d27b95153fc9791b21dd1326699d1998b Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 6 Oct 2021 19:59:19 -0500 Subject: [PATCH] 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 --- app/models/media_asset.rb | 1 + script/fixes/079_fix_duration.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100755 script/fixes/079_fix_duration.rb diff --git a/app/models/media_asset.rb b/app/models/media_asset.rb index 074bd5458..19b6a4076 100644 --- a/app/models/media_asset.rb +++ b/app/models/media_asset.rb @@ -28,6 +28,7 @@ class MediaAsset < ApplicationRecord self.file_size = media_file.file_size self.image_width = media_file.width self.image_height = media_file.height + self.duration = media_file.duration self.media_metadata = MediaMetadata.new(file: media_file) end end diff --git a/script/fixes/079_fix_duration.rb b/script/fixes/079_fix_duration.rb new file mode 100755 index 000000000..19ad10ee6 --- /dev/null +++ b/script/fixes/079_fix_duration.rb @@ -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