Files
danbooru/test/test_helpers/upload_test_helper.rb
evazion 3d660953d4 Add MediaMetadata model.
Add a model for storing image and video metadata for uploaded files.

Metadata is extracted using ExifTool. You will need to install ExifTool
after this commit. ExifTool 12.22 is the minimum required version
because we use the `--binary` option, which was added in this release.

The MediaMetadata model is separate from the MediaAsset model because
some files contain tons of metadata, and most of it is non-essential.
The MediaAsset model represents an uploaded file and contains essential
metadata, like the file's size and type, while the MediaMetadata model
represents all the other non-essential metadata associated with a file.

Metadata is stored as a JSON column in the database.

ExifTool returns all the file's metadata, not just the EXIF metadata.
EXIF is one of several types of image metadata, hence why we call
it MediaMetadata instead of EXIFMetadata.
2021-09-08 05:00:54 -05:00

48 lines
1.5 KiB
Ruby

module UploadTestHelper
extend ActiveSupport::Concern
def upload_from_file(filepath)
UploadService.new(file: upload_file(filepath)).start!
end
def upload_file(path)
file = Tempfile.new(binmode: true)
IO.copy_stream("#{Rails.root}/#{path}", file.path)
uploaded_file = ActionDispatch::Http::UploadedFile.new(tempfile: file, filename: File.basename(path))
yield uploaded_file if block_given?
uploaded_file
end
def assert_successful_upload(source_or_file_path, user: @user, **params)
if source_or_file_path =~ %r{\Ahttps?://}i
source = { source: source_or_file_path }
else
file = Rack::Test::UploadedFile.new(Rails.root.join(source_or_file_path))
source = { file: file }
end
assert_difference(["Upload.count"]) do
post_auth uploads_path, user, params: { upload: { tag_string: "abc", rating: "e", **source, **params }}
end
upload = Upload.last
assert_response :redirect
assert_redirected_to upload
assert_equal("completed", upload.status)
assert_equal(Post.last, upload.post)
assert_equal(upload.post.md5, upload.md5)
assert_not_nil(upload.media_asset)
assert_operator(upload.media_asset.media_metadata.metadata.count, :>=, 1)
upload
end
class_methods do
def should_upload_successfully(source)
should "upload successfully from #{source}" do
assert_successful_upload(source, user: create(:user, created_at: 1.month.ago))
end
end
end
end