fix bug with copy-pasted urls in uploads/new erroring out
This commit is contained in:
@@ -213,6 +213,39 @@ class UploadService
|
|||||||
tags << "animated_png" if is_animated_png?(upload, file)
|
tags << "animated_png" if is_animated_png?(upload, file)
|
||||||
tags.join(" ")
|
tags.join(" ")
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
class Preprocessor
|
class Preprocessor
|
||||||
@@ -281,14 +314,7 @@ class UploadService
|
|||||||
|
|
||||||
begin
|
begin
|
||||||
if source.present?
|
if source.present?
|
||||||
file = download_from_source(source, referer_url: upload.referer_url) do |context|
|
file = Utils.download_for_upload(source, upload)
|
||||||
upload.downloaded_source = context[:downloaded_source]
|
|
||||||
upload.source = context[:source]
|
|
||||||
|
|
||||||
if context[:ugoira]
|
|
||||||
upload.context = { ugoira: context[:ugoira] }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
elsif params[:file].present?
|
elsif params[:file].present?
|
||||||
file = params[:file]
|
file = params[:file]
|
||||||
end
|
end
|
||||||
@@ -314,26 +340,6 @@ class UploadService
|
|||||||
pred.save
|
pred.save
|
||||||
return pred
|
return pred
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
class Replacer
|
class Replacer
|
||||||
@@ -506,10 +512,12 @@ class UploadService
|
|||||||
|
|
||||||
@upload.update(status: "processing")
|
@upload.update(status: "processing")
|
||||||
|
|
||||||
|
if @upload.file.nil? && source.present?
|
||||||
|
@upload.file = Utils.download_for_upload(source, @upload)
|
||||||
|
end
|
||||||
|
|
||||||
if @upload.file.present?
|
if @upload.file.present?
|
||||||
Utils.process_file(upload, @upload.file)
|
Utils.process_file(upload, @upload.file)
|
||||||
else
|
|
||||||
# sources will be handled in preprocessing now
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@upload.save!
|
@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_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 :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 :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 :file_ext, format: { with: /jpg|gif|png|swf|webm|mp4|zip/ }, allow_nil: true
|
||||||
validates_with Validator
|
validates_with Validator
|
||||||
serialize :context, JSON
|
serialize :context, JSON
|
||||||
|
|||||||
@@ -275,19 +275,16 @@ class UploadServiceTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
context "::Preprocessor" do
|
context ".download_from_source" do
|
||||||
subject { UploadService::Preprocessor }
|
|
||||||
|
|
||||||
context "#download_from_source" do
|
|
||||||
setup do
|
setup do
|
||||||
@jpeg = "https://upload.wikimedia.org/wikipedia/commons/c/c5/Moraine_Lake_17092005.jpg"
|
@ugoira_source = "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247364"
|
||||||
@ugoira = "https://i.pximg.net/img-zip-ugoira/img/2017/04/04/08/57/38/62247364_ugoira1920x1080.zip"
|
@jpeg_source = "https://upload.wikimedia.org/wikipedia/commons/c/c5/Moraine_Lake_17092005.jpg"
|
||||||
|
@upload = Upload.new
|
||||||
end
|
end
|
||||||
|
|
||||||
should "work on a jpeg" do
|
should "work on a jpeg" do
|
||||||
file = subject.new({}).download_from_source(@jpeg) do |context|
|
file = subject.download_from_source(@jpeg_source) do |context|
|
||||||
assert_not_nil(context[:downloaded_source])
|
assert_not_nil(context[:downloaded_source])
|
||||||
assert_not_nil(context[:source])
|
assert_not_nil(context[:source])
|
||||||
end
|
end
|
||||||
@@ -297,7 +294,7 @@ class UploadServiceTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
should "work on an ugoira url" do
|
should "work on an ugoira url" do
|
||||||
file = subject.new({}).download_from_source(@ugoira, referer_url: "https://www.pixiv.net") do |context|
|
file = subject.download_from_source(@ugoira_source, referer_url: "https://www.pixiv.net") do |context|
|
||||||
assert_not_nil(context[:downloaded_source])
|
assert_not_nil(context[:downloaded_source])
|
||||||
assert_not_nil(context[:source])
|
assert_not_nil(context[:source])
|
||||||
assert_not_nil(context[:ugoira])
|
assert_not_nil(context[:ugoira])
|
||||||
@@ -306,7 +303,17 @@ class UploadServiceTest < ActiveSupport::TestCase
|
|||||||
assert_operator(File.size(file.path), :>, 0)
|
assert_operator(File.size(file.path), :>, 0)
|
||||||
file.close
|
file.close
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "initialize fields on the upload" do
|
||||||
|
subject.download_for_upload(@ugoira_source, @upload)
|
||||||
|
assert_not_nil(@upload.source)
|
||||||
|
assert_not_nil(@upload.context)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "::Preprocessor" do
|
||||||
|
subject { UploadService::Preprocessor }
|
||||||
|
|
||||||
context "#start!" do
|
context "#start!" do
|
||||||
setup do
|
setup do
|
||||||
|
|||||||
Reference in New Issue
Block a user