From 867406f4102c383da3473fc079bbdab4c89e1a9a Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Mon, 2 Jul 2018 14:47:23 -0700 Subject: [PATCH] handle the case where a file is uploaded that doesn't match whatever was preprocessed --- app/logical/upload_service.rb | 6 ++++++ test/functional/uploads_controller_test.rb | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/logical/upload_service.rb b/app/logical/upload_service.rb index ceb664bac..354aedc41 100644 --- a/app/logical/upload_service.rb +++ b/app/logical/upload_service.rb @@ -355,6 +355,12 @@ class UploadService pred.initialize_attributes pred.attributes = self.params + + # if a file was uploaded after the preprocessing occurred, + # then process the file and overwrite whatever the preprocessor + # did + Utils.process_file(pred, pred.file) if pred.file.present? + pred.status = "completed" pred.save return pred diff --git a/test/functional/uploads_controller_test.rb b/test/functional/uploads_controller_test.rb index b8325e320..20880e3c2 100644 --- a/test/functional/uploads_controller_test.rb +++ b/test/functional/uploads_controller_test.rb @@ -35,7 +35,7 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest context "preprocess action" do should "prefer the file over the source when preprocessing" do file = Rack::Test::UploadedFile.new("#{Rails.root}/test/files/test.jpg", "image/jpeg") - post_auth preprocess_uploads_path, @user, params: {:url => "http://www.google.com/intl/en_ALL/images/logo.gif", :file => file} + post_auth preprocess_uploads_path, @user, params: {:url => "https://raikou1.donmai.us/d3/4e/d34e4cf0a437a5d65f8e82b7bcd02606.jpg", :file => file} assert_response :success Delayed::Worker.new.work_off assert_equal("ecef68c44edb8a0d6a3070b5f8e8ee76", Upload.last.md5) @@ -51,10 +51,21 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest context "with a url" do should "preprocess" do assert_difference(-> { Upload.count }) do - get_auth new_upload_path, @user, params: {:url => "http://www.google.com/intl/en_ALL/images/logo.gif"} + get_auth new_upload_path, @user, params: {:url => "https://raikou1.donmai.us/d3/4e/d34e4cf0a437a5d65f8e82b7bcd02606.jpg"} assert_response :success end end + + should "prefer the file" do + get_auth new_upload_path, @user, params: {url: "https://raikou1.donmai.us/d3/4e/d34e4cf0a437a5d65f8e82b7bcd02606.jpg"} + Delayed::Worker.new.work_off + file = Rack::Test::UploadedFile.new("#{Rails.root}/test/files/test.jpg", "image/jpeg") + assert_difference(-> { Post.count }) do + post_auth uploads_path, @user, params: {upload: {file: file, tag_string: "aaa", rating: "q", source: "https://raikou1.donmai.us/d3/4e/d34e4cf0a437a5d65f8e82b7bcd02606.jpg"}} + end + post = Post.last + assert_equal("ecef68c44edb8a0d6a3070b5f8e8ee76", post.md5) + end end context "for a twitter post" do