Fix #3983: Uploads: NoMethodError - undefined method `>=' for nil:NilClass

* Rename Upload#download_for_upload to #get_file_for_upload.

* Fix #get_file_for_upload to raise error if no file or source url was given.

* Fix javascript upload validation to disallow submitting form if file is
  not present and the source is not an url.
This commit is contained in:
evazion
2018-11-11 14:06:35 -06:00
parent e10c6c6a30
commit a20eba9ef7
6 changed files with 36 additions and 22 deletions

View File

@@ -35,7 +35,7 @@ Upload.initialize_submit = function() {
Upload.validate_upload = function (e) {
var error_messages = [];
if (($("#upload_file").val() === "") && ($("#upload_source").val() === "") && $("#upload_md5_confirmation").val() === "") {
if (($("#upload_file").val() === "") && !/^https?:\/\//i.test($("#upload_source").val()) && $("#upload_md5_confirmation").val() === "") {
error_messages.push("Must choose file or specify source");
}
if (!$("#upload_rating_s").prop("checked") && !$("#upload_rating_q").prop("checked") && !$("#upload_rating_e").prop("checked") &&

View File

@@ -48,13 +48,8 @@ class UploadService
@upload.update(status: "processing")
if @upload.file.nil? && @upload.source_url.present?
@upload.file = Utils.download_for_upload(@upload)
end
if @upload.file.present?
Utils.process_file(upload, @upload.file)
end
@upload.file = Utils.get_file_for_upload(@upload, file: @upload.file)
Utils.process_file(upload, @upload.file)
@upload.save!
@post = create_post_from_upload(@upload)

View File

@@ -91,12 +91,7 @@ class UploadService
begin
upload.update(status: "preprocessing")
if params[:file].present?
file = params[:file]
elsif upload.source_url.present?
file = Utils.download_for_upload(upload)
end
file = Utils.get_file_for_upload(upload, file: params[:file])
Utils.process_file(upload, file, original_post_id: original_post_id)
upload.rating = params[:rating]

View File

@@ -206,7 +206,10 @@ class UploadService
tags.join(" ")
end
def download_for_upload(upload)
def get_file_for_upload(upload, file: nil)
return file if file.present?
raise RuntimeError, "No file or source URL provided" if upload.source_url.blank?
attempts = 0
begin

View File

@@ -115,11 +115,11 @@ class Upload < ApplicationRecord
end
def is_duplicate?
status =~ /duplicate: \d+/
status.match?(/duplicate: \d+/)
end
def is_errored?
status =~ /error:/
status.match?(/error:/)
end
def sanitized_status