From e46051ca94690782ca24741a570f2051296cd2d5 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 4 Jan 2020 01:54:07 +0100 Subject: port tootsuite#12748 to monsterfork: Fix base64-encoded file uploads not being possible Fix #3804, Fix #5776 --- app/models/concerns/attachmentable.rb | 19 +++++++++++++++++++ app/models/media_attachment.rb | 3 +++ 2 files changed, 22 insertions(+) (limited to 'app/models') diff --git a/app/models/concerns/attachmentable.rb b/app/models/concerns/attachmentable.rb index de4cf8775..dd11e0814 100644 --- a/app/models/concerns/attachmentable.rb +++ b/app/models/concerns/attachmentable.rb @@ -8,6 +8,7 @@ module Attachmentable MAX_MATRIX_LIMIT = 16_777_216 # 4096x4096px or approx. 16MB included do + before_post_process :obfuscate_file_name before_post_process :set_file_extensions before_post_process :check_image_dimensions end @@ -47,4 +48,22 @@ module Attachmentable extension end + + def calculated_content_type(attachment) + content_type = Paperclip.run('file', '-b --mime :file', file: attachment.queued_for_write[:original].path).split(/[:;\s]+/).first.chomp + content_type = 'video/mp4' if content_type == 'video/x-m4v' + content_type + rescue Terrapin::CommandLineError + '' + end + + def obfuscate_file_name + self.class.attachment_definitions.each_key do |attachment_name| + attachment = send(attachment_name) + + next if attachment.blank? + + attachment.instance_write :file_name, SecureRandom.hex(8) + File.extname(attachment.instance_read(:file_name)) + end + end end diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index 4f0a09784..d2db899ab 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -184,9 +184,12 @@ class MediaAttachment < ApplicationRecord end after_commit :reset_parent_cache, on: :update + before_create :prepare_description, unless: :local? before_create :set_shortcode + before_post_process :set_type_and_extension + before_save :set_meta class << self -- cgit