Commit 95c20d77 authored by Albert Yi's avatar Albert Yi

fix bug with copy-pasted urls in uploads/new erroring out

parent a9c4210b
......@@ -213,6 +213,39 @@ class UploadService
tags << "animated_png" if is_animated_png?(upload, file)
tags.join(" ")
end
def self.download_from_source(source, referer_url: nil)
download = Downloads::File.new(source, referer_url: referer_url)
file = download.download!
context = {
downloaded_source: download.downloaded_source,
source: download.source
}
if download.data[:is_ugoira]
context[:ugoira] = {
frame_data: download.data[:ugoira_frame_data],
content_type: download.data[:ugoira_content_type]
}
end
yield(context)
return file
end
def self.download_for_upload(source, upload)
file = download_from_source(source, referer_url: upload.referer_url) do |context|
upload.downloaded_source = context[:downloaded_source]
upload.source = context[:source]
if context[:ugoira]
upload.context = { ugoira: context[:ugoira] }
end
end
return file
end
end
class Preprocessor
......@@ -281,14 +314,7 @@ class UploadService
begin
if source.present?
file = download_from_source(source, referer_url: upload.referer_url) do |context|
upload.downloaded_source = context[:downloaded_source]
upload.source = context[:source]
if context[:ugoira]
upload.context = { ugoira: context[:ugoira] }
end
end
file = Utils.download_for_upload(source, upload)
elsif params[:file].present?
file = params[:file]
end
......@@ -314,26 +340,6 @@ class UploadService
pred.save
return pred
end
def download_from_source(source, referer_url: nil)
download = Downloads::File.new(source, referer_url: referer_url)
file = download.download!
context = {
downloaded_source: download.downloaded_source,
source: download.source
}
if download.data[:is_ugoira]
context[:ugoira] = {
frame_data: download.data[:ugoira_frame_data],
content_type: download.data[:ugoira_content_type]
}
end
yield(context)
return file
end
end
class Replacer
......@@ -506,10 +512,12 @@ class UploadService
@upload.update(status: "processing")
if @upload.file.nil? && source.present?
@upload.file = Utils.download_for_upload(source, @upload)
end
if @upload.file.present?
Utils.process_file(upload, @upload.file)
else
# sources will be handled in preprocessing now
end
@upload.save!
......
......@@ -59,7 +59,7 @@ class Upload < ApplicationRecord
validates :image_height, numericality: { less_than_or_equal_to: Danbooru.config.max_image_height }, allow_nil: true
validates :image_width, numericality: { less_than_or_equal_to: Danbooru.config.max_image_width }, allow_nil: true
validates :rating, inclusion: { in: %w(q e s) }, allow_nil: true
validates :md5, confirmation: true
validates :md5, confirmation: true, if: -> (rec) { rec.md5_confirmation.present? }
validates :file_ext, format: { with: /jpg|gif|png|swf|webm|mp4|zip/ }, allow_nil: true
validates_with Validator
serialize :context, JSON
......
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