StorageManager: remove Post-specific code.

Refactor StorageManager to remove all image URL generation code. Instead
the image URL generation code lives in MediaAsset.

Now StorageManager is only concerned with how to read and write files to
remote storage backends like S3 or SFTP, not with how image URLs should
be generated. This way the file storage code isn't tightly coupled to
posts, so it can be used to store any kind of file, not just images
belonging to posts.
This commit is contained in:
evazion
2021-10-26 19:11:37 -05:00
parent afe5095ee6
commit 082544ab03
7 changed files with 33 additions and 172 deletions

View File

@@ -40,8 +40,8 @@ class PostRegenerationsControllerTest < ActionDispatch::IntegrationTest
context "for an image sample regeneration" do
should "regenerate missing thumbnails" do
@preview_file_size = @post.file(:preview).size
@post.storage_manager.delete_file(@post.id, @post.md5, @post.file_ext, :preview)
@preview_file_size = @post.media_asset.variant(:preview).open_file.size
@post.media_asset.variant(:preview).delete_file!
assert_raise(Errno::ENOENT) { @post.file(:preview) }
post_auth post_regenerations_path, @mod, params: { post_id: @post.id }

View File

@@ -1626,14 +1626,11 @@ class PostTest < ActiveSupport::TestCase
context "URLs:" do
should "generate the correct urls for animated gifs" do
manager = StorageManager::Local.new(base_url: "https://test.com/data", base_dir: "/")
Danbooru.config.stubs(:storage_manager).returns(manager)
@post = create(:post_with_file, filename: "test-animated-86x52.gif")
@post = build(:post, md5: "deadbeef", file_ext: "gif", tag_string: "animated_gif")
assert_equal("https://test.com/data/preview/de/ad/deadbeef.jpg", @post.preview_file_url)
assert_equal("https://test.com/data/original/de/ad/deadbeef.gif", @post.large_file_url)
assert_equal("https://test.com/data/original/de/ad/deadbeef.gif", @post.file_url)
assert_equal("https://www.example.com/data/preview/77/d8/77d89bda37ea3af09158ed3282f8334f.jpg", @post.preview_file_url)
assert_equal("https://www.example.com/data/original/77/d8/77d89bda37ea3af09158ed3282f8334f.gif", @post.large_file_url)
assert_equal("https://www.example.com/data/original/77/d8/77d89bda37ea3af09158ed3282f8334f.gif", @post.file_url)
end
end
@@ -1642,7 +1639,6 @@ class PostTest < ActiveSupport::TestCase
setup do
@post = FactoryBot.create(:post)
@post.stubs(:queue_delete_files)
end
should "update the post" do

View File

@@ -37,58 +37,5 @@ class StorageManagerTest < ActiveSupport::TestCase
assert_nothing_raised { @storage_manager.delete("dne.txt") }
end
end
context "#store_file and #delete_file methods" do
setup do
@post = FactoryBot.create(:post, file_ext: "png")
@storage_manager.store_file(StringIO.new("data"), @post, :preview)
@storage_manager.store_file(StringIO.new("data"), @post, :large)
@storage_manager.store_file(StringIO.new("data"), @post, :original)
subdir = "#{@post.md5[0..1]}/#{@post.md5[2..3]}"
@file_path = "#{@temp_dir}/preview/#{subdir}/#{@post.md5}.jpg"
@large_file_path = "#{@temp_dir}/sample/#{subdir}/sample-#{@post.md5}.jpg"
@preview_file_path = "#{@temp_dir}/original/#{subdir}/#{@post.md5}.#{@post.file_ext}"
end
should "store the files at the correct path" do
assert(File.exist?(@file_path))
assert(File.exist?(@large_file_path))
assert(File.exist?(@preview_file_path))
end
should "delete the files" do
@storage_manager.delete_file(@post.id, @post.md5, @post.file_ext, :preview)
@storage_manager.delete_file(@post.id, @post.md5, @post.file_ext, :large)
@storage_manager.delete_file(@post.id, @post.md5, @post.file_ext, :original)
assert_not(File.exist?(@file_path))
assert_not(File.exist?(@large_file_path))
assert_not(File.exist?(@preview_file_path))
end
end
context "#file_url method" do
should "return the correct urls" do
@post = FactoryBot.create(:post, file_ext: "png")
@storage_manager.stubs(:tagged_filenames).returns(false)
subdir = "#{@post.md5[0..1]}/#{@post.md5[2..3]}"
assert_equal("/data/original/#{subdir}/#{@post.md5}.png", @storage_manager.file_url(@post, :original))
assert_equal("/data/sample/#{subdir}/sample-#{@post.md5}.jpg", @storage_manager.file_url(@post, :large))
assert_equal("/data/preview/#{subdir}/#{@post.md5}.jpg", @storage_manager.file_url(@post, :preview))
end
should "return the correct url for flash files" do
@post = FactoryBot.create(:post, file_ext: "swf")
@storage_manager.stubs(:base_url).returns("/data")
assert_equal("/images/download-preview.png", @storage_manager.file_url(@post, :preview))
@storage_manager.stubs(:base_url).returns("http://localhost/data")
assert_equal("http://localhost/images/download-preview.png", @storage_manager.file_url(@post, :preview))
end
end
end
end