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.
This commit is contained in:
evazion
2021-10-18 04:54:52 -05:00
parent 85c3b4f2d1
commit bc506ed1b8
17 changed files with 76 additions and 221 deletions

View File

@@ -1,5 +1,7 @@
class MediaAsset < ApplicationRecord
has_one :media_metadata, dependent: :destroy
has_one :pixiv_ugoira_frame_data, class_name: "PixivUgoiraFrameData", dependent: :destroy, foreign_key: :md5, primary_key: :md5
delegate :metadata, to: :media_metadata
delegate :is_non_repeating_animation?, :is_greyscale?, :is_rotated?, to: :metadata
@@ -136,6 +138,7 @@ class MediaAsset < ApplicationRecord
self.image_height = media_file.height
self.duration = media_file.duration
self.media_metadata = MediaMetadata.new(file: media_file)
self.pixiv_ugoira_frame_data = PixivUgoiraFrameData.new(data: media_file.frame_data, content_type: "image/jpeg") if is_ugoira?
end
def delete_files!

View File

@@ -1,5 +1,6 @@
class PixivUgoiraFrameData < ApplicationRecord
belongs_to :post
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
@@ -9,7 +10,7 @@ class PixivUgoiraFrameData < ApplicationRecord
end
def self.search(params)
q = search_attributes(params, :id, :data, :content_type, :post)
q = search_attributes(params, :id, :data, :content_type, :post, :md5)
q.apply_default_order(params)
end

View File

@@ -40,7 +40,7 @@ class Post < ApplicationRecord
has_one :media_asset, foreign_key: :md5, primary_key: :md5
has_one :upload, :dependent => :destroy
has_one :artist_commentary, :dependent => :destroy
has_one :pixiv_ugoira_frame_data, :class_name => "PixivUgoiraFrameData", :dependent => :destroy
has_one :pixiv_ugoira_frame_data, class_name: "PixivUgoiraFrameData", foreign_key: :md5, primary_key: :md5
has_many :flags, :class_name => "PostFlag", :dependent => :destroy
has_many :appeals, :class_name => "PostAppeal", :dependent => :destroy
has_many :votes, :class_name => "PostVote", :dependent => :destroy

View File

@@ -19,7 +19,7 @@ class Upload < ApplicationRecord
end
def validate_integrity(record)
if record.media_file.is_corrupt?
if record.file.is_corrupt?
record.errors.add(:file, "is corrupted")
end
end
@@ -55,7 +55,7 @@ class Upload < ApplicationRecord
end
def validate_video_duration(record)
if !record.uploader.is_admin? && record.media_file.is_video? && record.media_file.duration > MAX_VIDEO_DURATION
if !record.uploader.is_admin? && record.file.is_video? && record.file.duration > MAX_VIDEO_DURATION
record.errors.add(:base, "video must not be longer than #{MAX_VIDEO_DURATION.seconds.inspect}")
end
end
@@ -105,10 +105,6 @@ class Upload < ApplicationRecord
end
concerning :FileMethods do
def media_file
@media_file ||= MediaFile.open(file, frame_data: context.to_h.dig("ugoira", "frame_data"))
end
def delete_files
# md5 is blank if the upload errored out before downloading the file.
if is_completed? || md5.blank? || Upload.exists?(md5: md5) || Post.exists?(md5: md5)