uploads: refactor to simplify ugoira-handling and replacements:

* Make it so replacing a post doesn't generate a dummy upload as a side effect.
* Make it so you can't replace a post with itself (the post should be regenerated instead).
* Refactor uploads and replacements to save the ugoira frame data when
  the MediaAsset is created, not when the post is created. This way it's
  possible to view the ugoira before the post is created.
* Make `download_file!` in the Pixiv source strategy return a MediaFile
  with the ugoira frame data already attached to it, instead of returning it
  in the `data` field then passing it around separately in the `context`
  field of the upload.
This commit is contained in:
evazion
2021-10-18 04:54:52 -05:00
parent 85c3b4f2d1
commit bc506ed1b8
17 changed files with 76 additions and 221 deletions

View File

@@ -131,9 +131,10 @@ module Downloads
context "An ugoira site for pixiv" do
should "capture the data" do
@strategy = Sources::Strategies.find("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247364")
media_file = @strategy.download_file!
assert_equal(2, @strategy.data[:ugoira_frame_data].size)
assert_equal([{"file" => "000000.jpg", "delay" => 125}, {"file" => "000001.jpg", "delay" => 125}], @strategy.data[:ugoira_frame_data])
assert_equal(2, media_file.frame_data.size)
assert_equal([{"file" => "000000.jpg", "delay" => 125}, {"file" => "000001.jpg", "delay" => 125}], media_file.frame_data)
end
end
end

View File

@@ -48,10 +48,10 @@ module Sources
end
should "capture the frame data" do
ugoira_frame_data = @site.data[:ugoira_frame_data]
media_file = @site.download_file!
assert_equal(2, ugoira_frame_data.size)
assert_equal([{"file" => "000000.jpg", "delay" => 125}, {"file" => "000001.jpg", "delay" => 125}], ugoira_frame_data)
assert_equal(2, media_file.frame_data.size)
assert_equal([{"file" => "000000.jpg", "delay" => 125}, {"file" => "000001.jpg", "delay" => 125}], media_file.frame_data)
end
end

View File

@@ -23,59 +23,6 @@ class UploadServiceTest < ActiveSupport::TestCase
end
context "::Utils" do
context "#get_file_for_upload" do
context "for a non-source site" do
setup do
@source = "https://upload.wikimedia.org/wikipedia/commons/c/c5/Moraine_Lake_17092005.jpg"
@upload = Upload.new
@upload.source = @source
end
should "work on a jpeg" do
file = UploadService::Utils.get_file_for_upload(@upload)
assert_operator(File.size(file.path), :>, 0)
file.close
end
end
context "for a pixiv" do
setup do
skip "Pixiv credentials not configured" unless Sources::Strategies::Pixiv.enabled?
@source = "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247350"
@upload = Upload.new
@upload.source = @source
end
should "work on an ugoira url" do
begin
file = UploadService::Utils.get_file_for_upload(@upload)
assert_operator(File.size(file.path), :>, 0)
file.close
end
end
end
context "for a pixiv ugoira" do
setup do
skip "Pixiv credentials not configured" unless Sources::Strategies::Pixiv.enabled?
@source = "https://i.pximg.net/img-zip-ugoira/img/2017/04/04/08/57/38/62247364_ugoira1920x1080.zip"
@referer = "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247364"
@upload = Upload.new
@upload.source = @source
@upload.referer_url = @referer
end
should "work on an ugoira url" do
file = UploadService::Utils.get_file_for_upload(@upload)
assert_not_nil(@upload.context["ugoira"])
assert_operator(File.size(file.path), :>, 0)
end
end
end
context ".process_file" do
setup do
@upload = FactoryBot.build(:jpg_upload)
@@ -286,12 +233,6 @@ class UploadServiceTest < ActiveSupport::TestCase
subject { UploadService::Replacer.new(post: @post, replacement: @replacement) }
context "#process!" do
should "create a new upload" do
assert_difference(-> { Upload.count }) do
as(@user) { subject.process! }
end
end
should "create a comment" do
assert_difference(-> { @post.comments.count }) do
as(@user) { subject.process! }
@@ -342,22 +283,13 @@ class UploadServiceTest < ActiveSupport::TestCase
end
context "a post with the same file" do
should "not raise a duplicate error" do
should "raise an error" do
upload_file("test/files/test.png") do |file|
assert_nothing_raised do
assert_raises(UploadService::Replacer::Error) do
as(@user) { @post.replace!(replacement_file: file, replacement_url: "") }
end
end
end
should "not queue a deletion or log a comment" do
upload_file("test/files/test.png") do |file|
assert_no_difference(-> { @post.comments.count }) do
as(@user) { @post.replace!(replacement_file: file, replacement_url: "") }
@post.reload
end
end
end
end
end
@@ -407,9 +339,10 @@ class UploadServiceTest < ActiveSupport::TestCase
subject { UploadService::Replacer.new(post: @post, replacement: @replacement) }
context "when replacing with its own source" do
should "work" do
as(@user) { @post.replace!(replacement_url: @post.source) }
assert_equal(@post_md5, @post.md5)
should "raise an error" do
assert_raises(UploadService::Replacer::Error) do
as(@user) { @post.replace!(replacement_url: @post.source) }
end
end
end
@@ -480,12 +413,6 @@ class UploadServiceTest < ActiveSupport::TestCase
end
context "#process!" do
should "create a new upload" do
assert_difference(-> { Upload.count }) do
as(@user) { subject.process! }
end
end
should "create a comment" do
assert_difference(-> { @post.comments.count }) do
as(@user) { subject.process! }
@@ -911,20 +838,6 @@ class UploadServiceTest < ActiveSupport::TestCase
end
end
context "for a pixiv ugoira" do
setup do
@upload = FactoryBot.create(:ugoira_upload, file_size: 1000, md5: "12345", file_ext: "jpg", image_width: 100, image_height: 100, context: UGOIRA_CONTEXT)
end
should "create a post" do
assert_difference(-> { PixivUgoiraFrameData.count }) do
post = subject.new({}).create_post_from_upload(@upload)
assert_equal([], post.errors.full_messages)
assert_not_nil(post.id)
end
end
end
context "for nijie" do
should "record the canonical source" do
page_url = "https://nijie.info/view.php?id=728995"