about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--app/models/account.rb18
-rw-r--r--app/models/media_attachment.rb10
-rw-r--r--config/initializers/paperclip.rb6
3 files changed, 29 insertions, 5 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index f8a6dd703..9b4f7e2ca 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -21,6 +21,8 @@ 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
+
   # Local user profile validations
   validates :display_name, length: { maximum: 30 }, if: 'local?'
   validates :note, length: { maximum: 160 }, if: 'local?'
@@ -332,4 +334,20 @@ class Account < ApplicationRecord
       self.public_key  = keypair.public_key.to_pem
     end
   end
+
+  private
+
+  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/media_attachment.rb b/app/models/media_attachment.rb
index 85e82e12b..ec1808790 100644
--- a/app/models/media_attachment.rb
+++ b/app/models/media_attachment.rb
@@ -50,7 +50,7 @@ class MediaAttachment < ApplicationRecord
   end
 
   before_create :set_shortcode
-  before_post_process :set_type
+  before_post_process :set_type_and_extension
 
   class << self
     private
@@ -103,7 +103,13 @@ class MediaAttachment < ApplicationRecord
     end
   end
 
-  def set_type
+  def set_type_and_extension
     self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
+
+    unless file.blank?
+      extension = Paperclip::Interpolations.content_type_extension(file, :original)
+      basename  = Paperclip::Interpolations.basename(file, :original)
+      file.instance_write :file_name, [basename, extension].delete_if(&:empty?).join('.')
+    end
   end
 end
diff --git a/config/initializers/paperclip.rb b/config/initializers/paperclip.rb
index 77bc13b3a..ffaa2566e 100644
--- a/config/initializers/paperclip.rb
+++ b/config/initializers/paperclip.rb
@@ -4,7 +4,7 @@ Paperclip.options[:read_timeout] = 60
 
 Paperclip.interpolates :filename do |attachment, style|
   return attachment.original_filename if style == :original
-  [basename(attachment, style), content_type_extension(attachment, style)].delete_if(&:empty?).join('.')
+  [basename(attachment, style), extension(attachment, style)].delete_if(&:empty?).join('.')
 end
 
 if ENV['S3_ENABLED'] == 'true'
@@ -39,6 +39,6 @@ if ENV['S3_ENABLED'] == 'true'
     Paperclip::Attachment.default_options[:s3_host_alias] = ENV['S3_CLOUDFRONT_HOST']
   end
 else
-  Paperclip::Attachment.default_options[:path]           = (ENV['PAPERCLIP_ROOT_PATH'] || ':rails_root/public/system') + '/:class/:attachment/:id_partition/:style/:filename'
-  Paperclip::Attachment.default_options[:url]            = (ENV['PAPERCLIP_ROOT_URL'] || '/system') + '/:class/:attachment/:id_partition/:style/:filename'
+  Paperclip::Attachment.default_options[:path] = (ENV['PAPERCLIP_ROOT_PATH'] || ':rails_root/public/system') + '/:class/:attachment/:id_partition/:style/:filename'
+  Paperclip::Attachment.default_options[:url]  = (ENV['PAPERCLIP_ROOT_URL'] || '/system') + '/:class/:attachment/:id_partition/:style/:filename'
 end