Files
danbooru/app/models/upload_media_asset.rb
evazion 02edb52569 uploads: enable multi-file uploads when uploading from source.
Make the upload page automatically detect when a source URL has multiple images
and let the user choose which images to post.

For example, when uploading a Twitter or Pixiv post with more than one image, we
direct the user to a page showing a thumbnail for each image and letting
them choose which ones to post.

This is similar to the batch upload page, except we actually download each image
in the background, instead of just hotlinking or proxying the thumbnails through
our servers. This avoids various problems with proxying and makes new features
possible, like showing which images in the batch have already been posted.
2022-02-14 16:13:55 -06:00

67 lines
1.4 KiB
Ruby

# frozen_string_literal: true
class UploadMediaAsset < ApplicationRecord
extend Memoist
belongs_to :upload
belongs_to :media_asset, optional: true
after_create :async_process_upload!
after_save :update_upload_status, if: :saved_change_to_status?
enum status: {
pending: 0,
processing: 100,
active: 200,
failed: 300,
}
def self.visible(user)
if user.is_admin?
all
else
where(upload: { uploader: user })
end
end
def self.search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :status, :source_url, :page_url, :error, :upload, :media_asset)
q.apply_default_order(params)
end
def finished?
active? || failed?
end
def source_strategy
return nil if source_url.blank?
Sources::Strategies.find(source_url, page_url)
end
def async_process_upload!
ProcessUploadMediaAssetJob.perform_later(self)
end
def process_upload!
update!(status: :processing)
strategy = Sources::Strategies.find(source_url)
media_file = strategy.download_file!(source_url)
MediaAsset.upload!(media_file) do |media_asset|
update!(media_asset: media_asset)
end
update!(status: :active)
rescue Exception => e
update!(status: :failed, error: e.message)
end
def update_upload_status
upload.with_lock do
upload.update!(status: "completed") if upload.upload_media_assets.all?(&:finished?)
end
end
memoize :source_strategy
end