logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe
commit: 1899cf5f04400f8055e45ceda941a9580b93fa1e
parent: 5259319cf5d01a23a0e9517b9dc91c0a1f7e2ae9
Author: Yamagishi Kazutoshi <ykzts@desire.sh>
Date:   Mon,  1 May 2017 23:20:57 +0900

Detect extension for preview card (#2679)

* Detect extension for preview card

* next

Diffstat:

Mapp/models/account.rb16+---------------
Aapp/models/concerns/attachmentable.rb21+++++++++++++++++++++
Mapp/models/preview_card.rb2++
3 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/app/models/account.rb b/app/models/account.rb @@ -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 diff --git a/app/models/concerns/attachmentable.rb b/app/models/concerns/attachmentable.rb @@ -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 diff --git a/app/models/preview_card.rb b/app/models/preview_card.rb @@ -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