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.
This commit is contained in:
evazion
2022-02-10 14:03:13 -06:00
parent 5b2fcaaafc
commit 02edb52569
5 changed files with 45 additions and 8 deletions

View File

@@ -6,6 +6,9 @@ class UploadMediaAsset < ApplicationRecord
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,
@@ -35,5 +38,29 @@ class UploadMediaAsset < ApplicationRecord
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