Switch from unmaintained paperclip to kt-paperclip (#16724)
* Switch from unmaintained paperclip to kt-paperclip * Drop some compatibility monkey-patches not required by kt-paperclip * Drop media spoof check monkey-patching It's broken with kt-paperclip and hopefully it won't be needed anymore * Fix regression introduced by paperclip 6.1.0 * Do not rely on pathname to call FastImage * Add test for ogg vorbis file with cover art * Add audio/vorbis to the accepted content-types This seems erroneous as this would be the content-type for a vorbis stream without an ogg container, but that's what the `marcel` gem outputs, so… * Restore missing for_as_default method * Refactor Attachmentable concern and delay Paperclip's content-type spoof check Check for content-type spoofing *after* setting the extension ourselves, this fixes a regression with kt-paperclip's validations being more strict than paperclip 6.0.0 and rejecting some Pleroma uploads because of unknown extensions. * Please CodeClimate * Add audio/vorbis to the unreliable set It doesn't correspond to a file format and thus has no extension associated.th-downstream
parent
266a67121b
commit
7d0089033f
@ -1,35 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Paperclip
|
||||
module MediaTypeSpoofDetectorExtensions
|
||||
def mapping_override_mismatch?
|
||||
!Array(mapped_content_type).include?(calculated_content_type) && !Array(mapped_content_type).include?(type_from_mime_magic)
|
||||
end
|
||||
|
||||
def calculated_media_type_from_mime_magic
|
||||
@calculated_media_type_from_mime_magic ||= type_from_mime_magic.split('/').first
|
||||
end
|
||||
|
||||
def calculated_type_mismatch?
|
||||
!media_types_from_name.include?(calculated_media_type) && !media_types_from_name.include?(calculated_media_type_from_mime_magic)
|
||||
end
|
||||
|
||||
def type_from_mime_magic
|
||||
@type_from_mime_magic ||= begin
|
||||
begin
|
||||
File.open(@file.path) do |file|
|
||||
MimeMagic.by_magic(file)&.type || ''
|
||||
end
|
||||
rescue Errno::ENOENT
|
||||
''
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def type_from_file_command
|
||||
@type_from_file_command ||= FileCommandContentTypeDetector.new(@file.path).detect
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Paperclip::MediaTypeSpoofDetector.prepend(Paperclip::MediaTypeSpoofDetectorExtensions)
|
@ -1,37 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Monkey-patch various Paperclip methods for Ruby 3.0 compatibility
|
||||
|
||||
module Paperclip
|
||||
module Schema
|
||||
module StatementsExtensions
|
||||
def add_attachment(table_name, *attachment_names)
|
||||
raise ArgumentError, 'Please specify attachment name in your add_attachment call in your migration.' if attachment_names.empty?
|
||||
|
||||
options = attachment_names.extract_options!
|
||||
|
||||
attachment_names.each do |attachment_name|
|
||||
COLUMNS.each_pair do |column_name, column_type|
|
||||
column_options = options.merge(options[column_name.to_sym] || {})
|
||||
add_column(table_name, "#{attachment_name}_#{column_name}", column_type, **column_options)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module TableDefinitionExtensions
|
||||
def attachment(*attachment_names)
|
||||
options = attachment_names.extract_options!
|
||||
attachment_names.each do |attachment_name|
|
||||
COLUMNS.each_pair do |column_name, column_type|
|
||||
column_options = options.merge(options[column_name.to_sym] || {})
|
||||
column("#{attachment_name}_#{column_name}", column_type, **column_options)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Paperclip::Schema::Statements.prepend(Paperclip::Schema::StatementsExtensions)
|
||||
Paperclip::Schema::TableDefinition.prepend(Paperclip::Schema::TableDefinitionExtensions)
|
@ -1,58 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Monkey-patch various Paperclip validators for Ruby 3.0 compatibility
|
||||
|
||||
module Paperclip
|
||||
module Validators
|
||||
module AttachmentSizeValidatorExtensions
|
||||
def validate_each(record, attr_name, _value)
|
||||
base_attr_name = attr_name
|
||||
attr_name = "#{attr_name}_file_size".to_sym
|
||||
value = record.send(:read_attribute_for_validation, attr_name)
|
||||
|
||||
if value.present?
|
||||
options.slice(*Paperclip::Validators::AttachmentSizeValidator::AVAILABLE_CHECKS).each do |option, option_value|
|
||||
option_value = option_value.call(record) if option_value.is_a?(Proc)
|
||||
option_value = extract_option_value(option, option_value)
|
||||
|
||||
next if value.send(Paperclip::Validators::AttachmentSizeValidator::CHECKS[option], option_value)
|
||||
|
||||
error_message_key = options[:in] ? :in_between : option
|
||||
[attr_name, base_attr_name].each do |error_attr_name|
|
||||
record.errors.add(error_attr_name, error_message_key, **filtered_options(value).merge(
|
||||
min: min_value_in_human_size(record),
|
||||
max: max_value_in_human_size(record),
|
||||
count: human_size(option_value)
|
||||
))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module AttachmentContentTypeValidatorExtensions
|
||||
def mark_invalid(record, attribute, types)
|
||||
record.errors.add attribute, :invalid, **options.merge({ types: types.join(', ') })
|
||||
end
|
||||
end
|
||||
|
||||
module AttachmentPresenceValidatorExtensions
|
||||
def validate_each(record, attribute, _value)
|
||||
if record.send("#{attribute}_file_name").blank?
|
||||
record.errors.add(attribute, :blank, **options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module AttachmentFileNameValidatorExtensions
|
||||
def mark_invalid(record, attribute, patterns)
|
||||
record.errors.add attribute, :invalid, options.merge({ names: patterns.join(', ') })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Paperclip::Validators::AttachmentSizeValidator.prepend(Paperclip::Validators::AttachmentSizeValidatorExtensions)
|
||||
Paperclip::Validators::AttachmentContentTypeValidator.prepend(Paperclip::Validators::AttachmentContentTypeValidatorExtensions)
|
||||
Paperclip::Validators::AttachmentPresenceValidator.prepend(Paperclip::Validators::AttachmentPresenceValidatorExtensions)
|
||||
Paperclip::Validators::AttachmentFileNameValidator.prepend(Paperclip::Validators::AttachmentFileNameValidatorExtensions)
|
Binary file not shown.
Loading…
Reference in new issue