Detect extension for preview card (#2679)

* Detect extension for preview card

* next
th-downstream
Yamagishi Kazutoshi 8 years ago committed by Eugen Rochko
parent 4f155a3c86
commit 9f6363a319

@ -21,7 +21,7 @@ class Account < ApplicationRecord
validates_attachment_content_type :header, content_type: IMAGE_MIME_TYPES validates_attachment_content_type :header, content_type: IMAGE_MIME_TYPES
validates_attachment_size :header, less_than: 2.megabytes validates_attachment_size :header, less_than: 2.megabytes
before_post_process :set_file_extensions include Attachmentable
# Local user profile validations # Local user profile validations
validates :display_name, length: { maximum: 30 }, if: 'local?' validates :display_name, length: { maximum: 30 }, if: 'local?'
@ -363,18 +363,4 @@ class Account < ApplicationRecord
self.domain = TagManager.instance.normalize_domain(domain) self.domain = TagManager.instance.normalize_domain(domain)
end 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 end

@ -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' } has_attached_file :image, styles: { original: '120x120#' }, convert_options: { all: '-quality 80 -strip' }
include Attachmentable
validates :url, presence: true validates :url, presence: true
validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES
validates_attachment_size :image, less_than: 1.megabytes validates_attachment_size :image, less_than: 1.megabytes

Loading…
Cancel
Save