From 15799f8af7ad999f01d7a87b4c90ffc0fc912bdb Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 9 Jun 2020 15:45:56 -0500 Subject: [PATCH] Fix #4260: Unable to replace cdn.donmai.us images? Bug: Replacing posts hosted on cdn.donmai.us didn't work. Cause: Original files on cdn.donmai.us are hosted under /var/www/danbooru/original/, but replacements were trying to store them directly under /var/www/danbooru, which failed with a permission error. We were trying to store them in the wrong directory because we didn't respect the `original_subdir` option when generating file paths. --- app/logical/storage_manager.rb | 2 +- test/unit/storage_manager_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/logical/storage_manager.rb b/app/logical/storage_manager.rb index 93ad48b27..991ac7cf9 100644 --- a/app/logical/storage_manager.rb +++ b/app/logical/storage_manager.rb @@ -85,7 +85,7 @@ class StorageManager when :large "#{base_dir}/sample/#{subdir}#{file}" when :original - "#{base_dir}/#{subdir}#{file}" + "#{base_dir}/#{original_subdir}#{subdir}#{file}" end end diff --git a/test/unit/storage_manager_test.rb b/test/unit/storage_manager_test.rb index 9af8c6ede..9759a2918 100644 --- a/test/unit/storage_manager_test.rb +++ b/test/unit/storage_manager_test.rb @@ -130,6 +130,18 @@ class StorageManagerTest < ActiveSupport::TestCase assert_equal("http://localhost/images/download-preview.png", @storage_manager.file_url(@post, :preview)) end end + + context "when the original_subdir option is used" do + should "store original files at the correct path" do + @post = FactoryBot.create(:post, file_ext: "png") + @storage_manager = StorageManager::Local.new(base_dir: BASE_DIR, base_url: "/data", original_subdir: "original/") + + assert_equal("#{BASE_DIR}/original/#{@post.md5}.png", @storage_manager.file_path(@post, @post.file_ext, :original)) + + @storage_manager.store_file(StringIO.new("data"), @post, :original) + assert_equal(true, File.exist?("#{BASE_DIR}/original/#{@post.md5}.png")) + end + end end context "StorageManager::Hybrid" do