Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show warning if #filename is safeguarded as in the pre-3.x style #2718

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/carrierwave/uploader/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ def initialize(*)
@file, @filename, @cache_id, @identifier, @deduplication_index = nil
end
}

after :store, :show_warning_when_filename_is_unavailable

class_attribute :filename_safeguard_checked
end

module ClassMethods
private

def inherited(subclass)
# To perform the filename safeguard check once per a class
self.filename_safeguard_checked = false
super
end
end

##
Expand Down Expand Up @@ -133,6 +147,18 @@ def full_filename(for_file)
forcing_extension(for_file)
end

def show_warning_when_filename_is_unavailable(_)
return if self.class.filename_safeguard_checked
self.class.filename_safeguard_checked = true
return if filename

warn <<~MESSAGE
[WARNING] Your uploader's #filename method defined at #{method(:filename).source_location.join(':')} didn't return value after storing the file.
It's likely that the method is safeguarded with `if original_filename`, which were necessary for pre-3.x CarrierWave but is no longer needed.
Removing it is recommended, as it is known to cause issues depending on the use case: https://github.com/carrierwaveuploader/carrierwave/issues/2708
MESSAGE
end

def storage
@storage ||= self.class.storage.new(self)
end
Expand Down
18 changes: 18 additions & 0 deletions spec/uploader/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,24 @@ def filename; "foo.jpg"; end
end
end

context "with a filename safeguarded by 'if original_filename'" do
before do
@uploader_class.class_eval do
def filename
"foo.jpg" if original_filename
end
end
end

it "shows warning on store only once" do
expect(@uploader).to receive(:warn).with(/Your uploader's #filename method .+ didn't return value/).once
@file = File.open(file_path('test.jpg'))
@uploader.store!(@file)
@file = File.open(file_path('bork.txt'))
@uploader.store!(@file)
end
end

describe 'without a store dir' do
before do
@uploader_class.class_eval do
Expand Down
Loading