Commit a6e972a6 authored by Albert Yi's avatar Albert Yi

abort on dup md5 preprocessing

parent b0c2ddba
......@@ -148,6 +148,11 @@ class UploadService
upload.file_size = file.size
upload.md5 = Digest::MD5.file(file.path).hexdigest
if Post.where(md5: upload.md5).exists?
# abort early if this post already exists
raise Upload::Error.new("Post with MD5 #{upload.md5} already exists")
end
Utils.calculate_dimensions(upload, file) do |width, height|
upload.image_width = width
upload.image_height = height
......@@ -398,7 +403,9 @@ class UploadService
file: replacement.replacement_file
)
upload = preprocessor.start!(CurrentUser.id)
return if upload.is_errored?
upload = preprocessor.finish!(upload)
return if upload.is_errored?
md5_changed = upload.md5 != post.md5
if replacement.replacement_file.present?
......
......@@ -114,6 +114,10 @@ class Upload < ApplicationRecord
status =~ /duplicate/
end
def is_errored?
status =~ /error:/
end
def duplicate_post_id
@duplicate_post_id ||= status[/duplicate: (\d+)/, 1]
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment