mirror of
https://github.com/mastodon/mastodon.git
synced 2024-11-20 03:25:17 +01:00
Detect extension for preview card (#2679)
* Detect extension for preview card * next
This commit is contained in:
parent
5259319cf5
commit
1899cf5f04
@ -21,7 +21,7 @@ class Account < ApplicationRecord
|
||||
validates_attachment_content_type :header, content_type: IMAGE_MIME_TYPES
|
||||
validates_attachment_size :header, less_than: 2.megabytes
|
||||
|
||||
before_post_process :set_file_extensions
|
||||
include Attachmentable
|
||||
|
||||
# Local user profile validations
|
||||
validates :display_name, length: { maximum: 30 }, if: 'local?'
|
||||
@ -363,18 +363,4 @@ class Account < ApplicationRecord
|
||||
|
||||
self.domain = TagManager.instance.normalize_domain(domain)
|
||||
end
|
||||
|
||||
def set_file_extensions
|
||||
unless avatar.blank?
|
||||
extension = Paperclip::Interpolations.content_type_extension(avatar, :original)
|
||||
basename = Paperclip::Interpolations.basename(avatar, :original)
|
||||
avatar.instance_write :file_name, [basename, extension].delete_if(&:empty?).join('.')
|
||||
end
|
||||
|
||||
unless header.blank?
|
||||
extension = Paperclip::Interpolations.content_type_extension(header, :original)
|
||||
basename = Paperclip::Interpolations.basename(header, :original)
|
||||
header.instance_write :file_name, [basename, extension].delete_if(&:empty?).join('.')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
21
app/models/concerns/attachmentable.rb
Normal file
21
app/models/concerns/attachmentable.rb
Normal file
@ -0,0 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Attachmentable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
before_post_process :set_file_extensions
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_file_extensions
|
||||
self.class.attachment_definitions.each_key do |attachment_name|
|
||||
attachment = send(attachment_name)
|
||||
next if attachment.blank?
|
||||
extension = Paperclip::Interpolations.content_type_extension(attachment, :original)
|
||||
basename = Paperclip::Interpolations.basename(attachment, :original)
|
||||
attachment.instance_write :file_name, [basename, extension].delete_if(&:empty?).join('.')
|
||||
end
|
||||
end
|
||||
end
|
@ -11,6 +11,8 @@ class PreviewCard < ApplicationRecord
|
||||
|
||||
has_attached_file :image, styles: { original: '120x120#' }, convert_options: { all: '-quality 80 -strip' }
|
||||
|
||||
include Attachmentable
|
||||
|
||||
validates :url, presence: true
|
||||
validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES
|
||||
validates_attachment_size :image, less_than: 1.megabytes
|
||||
|
Loading…
Reference in New Issue
Block a user