Files
danbooru/app/models/pixiv_ugoira_frame_data.rb
evazion bc506ed1b8 uploads: refactor to simplify ugoira-handling and replacements:
* Make it so replacing a post doesn't generate a dummy upload as a side effect.
* Make it so you can't replace a post with itself (the post should be regenerated instead).
* Refactor uploads and replacements to save the ugoira frame data when
  the MediaAsset is created, not when the post is created. This way it's
  possible to view the ugoira before the post is created.
* Make `download_file!` in the Pixiv source strategy return a MediaFile
  with the ugoira frame data already attached to it, instead of returning it
  in the `data` field then passing it around separately in the `context`
  field of the upload.
2021-10-18 05:18:46 -05:00

28 lines
700 B
Ruby

class PixivUgoiraFrameData < ApplicationRecord
belongs_to :post, optional: true, foreign_key: :md5, primary_key: :md5
belongs_to :media_asset, foreign_key: :md5, primary_key: :md5
serialize :data
before_validation :normalize_data, on: :create
def self.available_includes
[:post]
end
def self.search(params)
q = search_attributes(params, :id, :data, :content_type, :post, :md5)
q.apply_default_order(params)
end
def normalize_data
return if data.nil?
if data[0]["delay_msec"]
self.data = data.map.with_index do |datum, i|
filename = format("%06d.jpg", i)
{"delay" => datum["delay_msec"], "file" => filename}
end
end
end
end