This commit is contained in:
Albert Yi
2018-09-04 11:30:49 -07:00
parent 3bed5f3cf9
commit b0bd20c436
2 changed files with 50 additions and 6 deletions

View File

@@ -1,5 +1,7 @@
class UploadService
class Replacer
extend Memoist
attr_reader :post, :replacement
def initialize(post:, replacement:)
@@ -58,6 +60,26 @@ class UploadService
undoer.process!
end
def source_strategy(upload)
return Sources::Strategies.find(upload.source, upload.referer_url)
end
def find_replacement_url(repl, upload)
if repl.replacement_file.present?
return "file://#{repl.replacement_file.original_filename}"
end
if !upload.source.present?
raise "No source found in upload for replacement"
end
if source_strategy(upload).canonical_url.present?
return source_strategy(upload).canonical_url
end
return upload.source
end
def process!
preprocessor = Preprocessor.new(
rating: post.rating,
@@ -72,11 +94,7 @@ class UploadService
return if upload.is_errored?
md5_changed = upload.md5 != post.md5
if replacement.replacement_file.present?
replacement.replacement_url = "file://#{replacement.replacement_file.original_filename}"
elsif upload.source.present?
replacement.replacement_url = Sources::Strategies.canonical(upload.source, upload.referer_url)
end
replacement.replacement_url = find_replacement_url(replacement, upload)
if md5_changed
post.queue_delete_files(PostReplacement::DELETION_GRACE_PERIOD)
@@ -93,7 +111,7 @@ class UploadService
post.image_width = upload.image_width
post.image_height = upload.image_height
post.file_size = upload.file_size
post.source = Sources::Strategies.canonical(upload.source, upload.referer_url)
post.source = replacement.replacement_url
post.tag_string = upload.tag_string
update_ugoira_frame_data(post, upload)