* 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.
42 lines
1.2 KiB
Ruby
42 lines
1.2 KiB
Ruby
class UploadService
|
|
module Utils
|
|
module_function
|
|
|
|
def is_downloadable?(source)
|
|
source =~ %r{\Ahttps?://}
|
|
end
|
|
|
|
def process_file(upload, file, original_post_id: nil)
|
|
media_file = MediaFile.open(file)
|
|
|
|
upload.file = media_file
|
|
upload.file_ext = media_file.file_ext.to_s
|
|
upload.file_size = media_file.file_size
|
|
upload.md5 = media_file.md5
|
|
upload.image_width = media_file.width
|
|
upload.image_height = media_file.height
|
|
|
|
upload.validate!(:file)
|
|
upload.tag_string = "#{upload.tag_string} #{Utils.automatic_tags(media_file)}"
|
|
|
|
MediaAsset.upload!(media_file)
|
|
end
|
|
|
|
def automatic_tags(media_file)
|
|
tags = []
|
|
tags << "sound" if media_file.has_audio?
|
|
tags.join(" ")
|
|
end
|
|
|
|
def get_file_for_upload(source_url, referer_url, file)
|
|
return MediaFile.open(file) if file.present?
|
|
raise "No file or source URL provided" if source_url.blank?
|
|
|
|
strategy = Sources::Strategies.find(source_url, referer_url)
|
|
raise NotImplementedError, "No login credentials configured for #{strategy.site_name}." unless strategy.class.enabled?
|
|
|
|
strategy.download_file!
|
|
end
|
|
end
|
|
end
|