Properly detect duplicate uploads
This commit is contained in:
parent
46352afc58
commit
1a21007068
2 changed files with 34 additions and 24 deletions
|
|
@ -105,7 +105,8 @@ function upload_file_with_ajax(file)
|
||||||
error: function(xhr, txt, err)
|
error: function(xhr, txt, err)
|
||||||
{
|
{
|
||||||
console.log("AJAX file upload returned error: "+txt+" / "+err);
|
console.log("AJAX file upload returned error: "+txt+" / "+err);
|
||||||
display_error('Did you copy a proper image into your clipboard?');
|
response = JSON.parse(xhr.responseText);
|
||||||
|
display_error(response);
|
||||||
},
|
},
|
||||||
success: function(res)
|
success: function(res)
|
||||||
{
|
{
|
||||||
|
|
@ -119,5 +120,5 @@ function upload_file_with_ajax(file)
|
||||||
function display_error(msg)
|
function display_error(msg)
|
||||||
{
|
{
|
||||||
$('#messages').html(
|
$('#messages').html(
|
||||||
'<div class="callout alert alert-callout-subtle radius">Sorry, pasting did not work.' + msg + '</div>')
|
'<div class="callout alert alert-callout-subtle radius">Sorry, pasting did not work. ' + msg + '</div>')
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -70,21 +70,32 @@ class PackagesController < ApplicationController
|
||||||
new_screenshot.version = @package.version
|
new_screenshot.version = @package.version
|
||||||
# ActiveStorage does not yet create a file checksum automatically.
|
# ActiveStorage does not yet create a file checksum automatically.
|
||||||
# Let's do that. It helps detect duplicate uploads later.
|
# Let's do that. It helps detect duplicate uploads later.
|
||||||
new_screenshot.image_fingerprint = Digest::MD5.hexdigest(img.path)
|
new_screenshot.image_fingerprint = Digest::MD5.hexdigest(File.read img.path)
|
||||||
|
|
||||||
# Can the upload get approved automatically?
|
# Check that this screenshot is not a duplicate for this package
|
||||||
if user_signed_in?
|
if @package.screenshots.where(image_fingerprint: new_screenshot.image_fingerprint).any?
|
||||||
new_screenshot.user = current_user
|
Log.log "Duplicate image with fingerprint #{new_screenshot.image_fingerprint} found. Rejecting."
|
||||||
new_screenshot.approve! if auto_approve?
|
all_errors << "Your file #{img.original_filename} is a duplicate."
|
||||||
|
else
|
||||||
|
# Can the upload get approved automatically?
|
||||||
|
if user_signed_in?
|
||||||
|
new_screenshot.user = current_user
|
||||||
|
new_screenshot.approve! if auto_approve?
|
||||||
|
end
|
||||||
|
|
||||||
|
new_screenshot.simage_derivatives!
|
||||||
|
Log.log "Derivatives created"
|
||||||
|
|
||||||
|
new_screenshot.save!
|
||||||
|
Log.log "Screenshot #{new_screenshot.id} uploaded successfully. " + \
|
||||||
|
"ip=#{new_screenshot.uploaderip}. "+ \
|
||||||
|
"user-hash=#{new_screenshot.uploaderhash}. "+ \
|
||||||
|
"user-name=#{current_user} "+ \
|
||||||
|
"image-fingerprint=#{new_screenshot.image_fingerprint} "+ \
|
||||||
|
"image-path=#{img.path}"
|
||||||
|
|
||||||
|
@valid_images.push new_screenshot
|
||||||
end
|
end
|
||||||
|
|
||||||
new_screenshot.simage_derivatives!
|
|
||||||
Log.log "Derivatives created"
|
|
||||||
|
|
||||||
new_screenshot.save!
|
|
||||||
Log.log "Screenshot #{new_screenshot.id} uploaded successfully. ip=#{new_screenshot.uploaderip}. user-hash=#{new_screenshot.uploaderhash}. user-name=#{current_user}"
|
|
||||||
|
|
||||||
@valid_images.push new_screenshot
|
|
||||||
else
|
else
|
||||||
errors = new_screenshot.errors[:simage]
|
errors = new_screenshot.errors[:simage]
|
||||||
Log.log "Screenshot #{img.original_filename} invalid (#{errors})."
|
Log.log "Screenshot #{img.original_filename} invalid (#{errors})."
|
||||||
|
|
@ -98,11 +109,11 @@ class PackagesController < ApplicationController
|
||||||
flash[:error] = all_errors
|
flash[:error] = all_errors
|
||||||
end
|
end
|
||||||
|
|
||||||
# Redirect back to upload form if all uploads were invalid
|
# # Redirect back to upload form if all uploads were invalid
|
||||||
unless @valid_images.any?
|
# unless @valid_images.any?
|
||||||
Log.log "No valid images uploaded. Back to upload form."
|
# Log.log "No valid images uploaded. Back to upload form."
|
||||||
redirect_to(upload_path, error: all_errors) and return
|
# redirect_to(upload_path, error: all_errors) and return
|
||||||
end
|
# end
|
||||||
|
|
||||||
# Show a list of invalid uploads by default. Or redirect to the review page
|
# Show a list of invalid uploads by default. Or redirect to the review page
|
||||||
# if all uploads were okay.
|
# if all uploads were okay.
|
||||||
|
|
@ -117,12 +128,10 @@ class PackagesController < ApplicationController
|
||||||
# is set in routes.rb to signal that this method was called by AJAX.
|
# is set in routes.rb to signal that this method was called by AJAX.
|
||||||
if params[:returns] == :json
|
if params[:returns] == :json
|
||||||
# TODO: send all_errors back as JSON and make Javascript display it in #messages
|
# TODO: send all_errors back as JSON and make Javascript display it in #messages
|
||||||
render :json => true
|
render :json => {errors: all_errors.join(' ')}
|
||||||
else
|
else
|
||||||
redirect_to package_path
|
redirect_to package_path
|
||||||
end
|
end
|
||||||
|
|
||||||
# render :details
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Legacy action to upload an image along with metadata.
|
# Legacy action to upload an image along with metadata.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue