uploads: factor out image dimension and filetype detection code.

* Add MediaFile abstraction. A MediaFile represents an image or video file.
* Move filetype detection and dimension parsing code from uploads to MediaFile.
This commit is contained in:
evazion
2020-05-06 00:33:35 -05:00
parent eab413199c
commit e477232e02
10 changed files with 215 additions and 154 deletions

View File

@@ -0,0 +1,85 @@
require 'test_helper'
class MediaFileTest < ActiveSupport::TestCase
context "#dimensions" do
should "determine the correct dimensions for a jpeg file" do
assert_equal([500, 335], MediaFile.open("test/files/test.jpg").dimensions)
assert_equal([668, 996], MediaFile.open("test/files/test-blank.jpg").dimensions)
assert_equal([529, 600], MediaFile.open("test/files/test-exif-small.jpg").dimensions)
assert_equal([1356, 911], MediaFile.open("test/files/test-large.jpg").dimensions)
end
should "determine the correct dimensions for a png file" do
assert_equal([768, 1024], MediaFile.open("test/files/test.png").dimensions)
assert_equal([150, 150], MediaFile.open("test/files/apng/normal_apng.png").dimensions)
assert_equal([85, 62], MediaFile.open("test/files/alpha.png").dimensions)
end
should "determine the correct dimensions for a gif file" do
assert_equal([400, 400], MediaFile.open("test/files/test.gif").dimensions)
assert_equal([86, 52], MediaFile.open("test/files/test-animated-86x52.gif").dimensions)
assert_equal([32, 32], MediaFile.open("test/files/test-static-32x32.gif").dimensions)
end
should "determine the correct dimensions for a webm file" do
assert_equal([512, 512], MediaFile.open("test/files/test-512x512.webm").dimensions)
end
should "determine the correct dimensions for a mp4 file" do
assert_equal([300, 300], MediaFile.open("test/files/test-300x300.mp4").dimensions)
end
should "determine the correct dimensions for a ugoira file" do
assert_equal([60, 60], MediaFile.open("test/files/valid_ugoira.zip").dimensions)
end
should "determine the correct dimensions for a flash file" do
assert_equal([607, 756], MediaFile.open("test/files/compressed.swf").dimensions)
end
end
context "#file_ext" do
should "determine the correct extension for a jpeg file" do
assert_equal(:jpg, MediaFile.open("test/files/test.jpg").file_ext)
assert_equal(:jpg, MediaFile.open("test/files/test-blank.jpg").file_ext)
assert_equal(:jpg, MediaFile.open("test/files/test-exif-small.jpg").file_ext)
assert_equal(:jpg, MediaFile.open("test/files/test-large.jpg").file_ext)
end
should "determine the correct extension for a png file" do
assert_equal(:png, MediaFile.open("test/files/test.png").file_ext)
assert_equal(:png, MediaFile.open("test/files/apng/normal_apng.png").file_ext)
assert_equal(:png, MediaFile.open("test/files/alpha.png").file_ext)
end
should "determine the correct extension for a gif file" do
assert_equal(:gif, MediaFile.open("test/files/test.gif").file_ext)
assert_equal(:gif, MediaFile.open("test/files/test-animated-86x52.gif").file_ext)
assert_equal(:gif, MediaFile.open("test/files/test-static-32x32.gif").file_ext)
end
should "determine the correct extension for a webm file" do
assert_equal(:webm, MediaFile.open("test/files/test-512x512.webm").file_ext)
end
should "determine the correct extension for a mp4 file" do
assert_equal(:mp4, MediaFile.open("test/files/test-300x300.mp4").file_ext)
end
should "determine the correct extension for a ugoira file" do
assert_equal(:zip, MediaFile.open("test/files/valid_ugoira.zip").file_ext)
end
should "determine the correct extension for a flash file" do
assert_equal(:swf, MediaFile.open("test/files/compressed.swf").file_ext)
end
end
should "determine the correct md5 for a jpeg file" do
assert_equal("ecef68c44edb8a0d6a3070b5f8e8ee76", MediaFile.open("test/files/test.jpg").md5)
end
should "determine the correct filesize for a jpeg file" do
assert_equal(28086, MediaFile.open("test/files/test.jpg").file_size)
end
end

View File

@@ -77,105 +77,21 @@ class UploadServiceTest < ActiveSupport::TestCase
end
end
context ".calculate_ugoira_dimensions" do
context "for a valid ugoira file" do
setup do
@path = "test/files/valid_ugoira.zip"
end
should "extract the dimensions" do
w, h = subject.calculate_ugoira_dimensions(@path)
assert_operator(w, :>, 0)
assert_operator(h, :>, 0)
end
end
context "for an invalid ugoira file" do
setup do
@path = "test/files/invalid_ugoira.zip"
end
should "raise an error" do
assert_raises(ImageSpec::Error) do
subject.calculate_ugoira_dimensions(@path)
end
end
end
end
context ".calculate_dimensions" do
context "for an ugoira" do
setup do
@file = File.open("test/files/valid_ugoira.zip", "rb")
@upload = Upload.new(file_ext: "zip")
end
teardown do
@file.close
end
should "return the dimensions" do
subject.expects(:calculate_ugoira_dimensions).once.returns([60, 60])
subject.calculate_dimensions(@upload, @file) do |w, h|
assert_operator(w, :>, 0)
assert_operator(h, :>, 0)
end
end
end
context "for a video" do
setup do
@file = File.open("test/files/test-300x300.mp4", "rb")
@upload = Upload.new(file_ext: "mp4")
end
teardown do
@file.close
end
should "return the dimensions" do
subject.calculate_dimensions(@upload, @file) do |w, h|
assert_operator(w, :>, 0)
assert_operator(h, :>, 0)
end
end
end
context "for an image" do
setup do
@file = File.open("test/files/test.jpg", "rb")
@upload = Upload.new(file_ext: "jpg")
end
teardown do
@file.close
end
should "find the dimensions" do
subject.calculate_dimensions(@upload, @file) do |w, h|
assert_operator(w, :>, 0)
assert_operator(h, :>, 0)
end
end
end
end
context ".process_file" do
setup do
@upload = FactoryBot.build(:jpg_upload)
@file = @upload.file
end
context "with an original_post_id" do
should "run" do
subject.expects(:distribute_files).times(3)
subject.process_file(@upload, @file, original_post_id: 12345)
subject.process_file(@upload, @upload.file.tempfile, original_post_id: 12345)
end
end
should "run" do
subject.expects(:distribute_files).times(3)
subject.process_file(@upload, @file)
subject.process_file(@upload, @upload.file.tempfile)
assert_equal("jpg", @upload.file_ext)
assert_equal(28086, @upload.file_size)
assert_equal("ecef68c44edb8a0d6a3070b5f8e8ee76", @upload.md5)
@@ -188,7 +104,7 @@ class UploadServiceTest < ActiveSupport::TestCase
context "for an ugoira" do
setup do
context = UGOIRA_CONTEXT
@file = File.open("test/fixtures/ugoira.zip", "rb")
@file = upload_file("test/fixtures/ugoira.zip")
@upload = mock
@upload.stubs(:is_video?).returns(false)
@upload.stubs(:is_ugoira?).returns(true)
@@ -220,7 +136,7 @@ class UploadServiceTest < ActiveSupport::TestCase
context "for an mp4" do
setup do
@file = File.open("test/files/test-300x300.mp4", "rb")
@file = upload_file("test/files/test-300x300.mp4")
@upload = mock
@upload.stubs(:is_video?).returns(true)
@upload.stubs(:is_ugoira?).returns(false)
@@ -243,7 +159,7 @@ class UploadServiceTest < ActiveSupport::TestCase
context "for a webm" do
setup do
@file = File.open("test/files/test-512x512.webm", "rb")
@file = upload_file("test/files/test-512x512.webm")
@upload = mock
@upload.stubs(:is_video?).returns(true)
@upload.stubs(:is_ugoira?).returns(false)
@@ -281,7 +197,7 @@ class UploadServiceTest < ActiveSupport::TestCase
context "for a jpeg" do
setup do
@file = File.open("test/files/test.jpg", "rb")
@file = upload_file("test/files/test.jpg")
end
should "generate a preview" do
@@ -298,7 +214,7 @@ class UploadServiceTest < ActiveSupport::TestCase
context "for a png" do
setup do
@file = File.open("test/files/test.png", "rb")
@file = upload_file("test/files/test.png")
end
should "generate a preview" do
@@ -315,7 +231,7 @@ class UploadServiceTest < ActiveSupport::TestCase
context "for a gif" do
setup do
@file = File.open("test/files/test.png", "rb")
@file = upload_file("test/files/test.png")
end
should "generate a preview" do