Detect extension for preview card (#2679)

* Detect extension for preview card

* next
This commit is contained in:
Yamagishi Kazutoshi 2017-05-01 23:20:57 +09:00 committed by Eugen Rochko
parent 5259319cf5
commit 1899cf5f04
3 changed files with 24 additions and 15 deletions

View file

@ -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

View 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

View file

@ -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