From 7696f77245c2302787d239da50248385b3292a5e Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 20 Jun 2019 02:52:34 +0200 Subject: Add moderation API (#9387) Fix #8580 Fix #7143 --- app/models/concerns/user_roles.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'app/models/concerns') diff --git a/app/models/concerns/user_roles.rb b/app/models/concerns/user_roles.rb index 58dffdc46..a42b4a172 100644 --- a/app/models/concerns/user_roles.rb +++ b/app/models/concerns/user_roles.rb @@ -13,6 +13,20 @@ module UserRoles admin? || moderator? end + def role=(value) + case value + when 'admin' + self.admin = true + self.moderator = false + when 'moderator' + self.admin = false + self.moderator = true + else + self.admin = false + self.moderator = false + end + end + def role if admin? 'admin' -- cgit From 8f23726918fd8ded18ce9e55e6199df87abcc7cf Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 20 Jun 2019 10:52:36 +0200 Subject: Fix converted media being saved with original extension and mime type (#11130) --- app/models/concerns/attachmentable.rb | 2 +- app/models/media_attachment.rb | 8 +++++--- config/application.rb | 1 + lib/paperclip/type_corrector.rb | 19 +++++++++++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 lib/paperclip/type_corrector.rb (limited to 'app/models/concerns') diff --git a/app/models/concerns/attachmentable.rb b/app/models/concerns/attachmentable.rb index de4cf8775..f4e37f1e6 100644 --- a/app/models/concerns/attachmentable.rb +++ b/app/models/concerns/attachmentable.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'mime/types' +require 'mime/types/columnar' module Attachmentable extend ActiveSupport::Concern diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index 5ca218411..ae94ce68a 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -70,12 +70,14 @@ class MediaAttachment < ApplicationRecord AUDIO_STYLES = { original: { format: 'ogg', + content_type: 'audio/ogg', convert_options: {}, }, }.freeze VIDEO_FORMAT = { format: 'mp4', + content_type: 'video/mp4', convert_options: { output: { 'loglevel' => 'fatal', @@ -189,11 +191,11 @@ class MediaAttachment < ApplicationRecord if f.file_content_type == 'image/gif' [:gif_transcoder, :blurhash_transcoder] elsif VIDEO_MIME_TYPES.include?(f.file_content_type) - [:video_transcoder, :blurhash_transcoder] + [:video_transcoder, :blurhash_transcoder, :type_corrector] elsif AUDIO_MIME_TYPES.include?(f.file_content_type) - [:transcoder] + [:transcoder, :type_corrector] else - [:lazy_thumbnail, :blurhash_transcoder] + [:lazy_thumbnail, :blurhash_transcoder, :type_corrector] end end end diff --git a/config/application.rb b/config/application.rb index 74006f5fa..4534ede49 100644 --- a/config/application.rb +++ b/config/application.rb @@ -10,6 +10,7 @@ require_relative '../app/lib/exceptions' require_relative '../lib/paperclip/lazy_thumbnail' require_relative '../lib/paperclip/gif_transcoder' require_relative '../lib/paperclip/video_transcoder' +require_relative '../lib/paperclip/type_corrector' require_relative '../lib/mastodon/snowflake' require_relative '../lib/mastodon/version' require_relative '../lib/devise/ldap_authenticatable' diff --git a/lib/paperclip/type_corrector.rb b/lib/paperclip/type_corrector.rb new file mode 100644 index 000000000..0b0c10a56 --- /dev/null +++ b/lib/paperclip/type_corrector.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'mime/types/columnar' + +module Paperclip + class TypeCorrector < Paperclip::Processor + def make + target_extension = options[:format] + extension = File.extname(attachment.instance.file_file_name) + + return @file unless options[:style] == :original && target_extension && extension != target_extension + + attachment.instance.file_content_type = options[:content_type] || attachment.instance.file_content_type + attachment.instance.file_file_name = File.basename(attachment.instance.file_file_name, '.*') + '.' + target_extension + + @file + end + end +end -- cgit From b927bb3f07b88d0a7bc39a7661d26dd1c9fc05d4 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 22 Jun 2019 16:54:06 +0200 Subject: Fix audio-only OGG and WebM files not being processed as such (#11151) Also, because Chrome sends audio/mp3 instead of audio/mpeg as it's supposed to, we need to whitelist that mime type as well --- app/models/concerns/attachmentable.rb | 17 +++++++++++++++++ app/models/media_attachment.rb | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'app/models/concerns') diff --git a/app/models/concerns/attachmentable.rb b/app/models/concerns/attachmentable.rb index f4e37f1e6..24f5968de 100644 --- a/app/models/concerns/attachmentable.rb +++ b/app/models/concerns/attachmentable.rb @@ -10,10 +10,21 @@ module Attachmentable included do before_post_process :set_file_extensions before_post_process :check_image_dimensions + before_post_process :set_file_content_type end private + def set_file_content_type + self.class.attachment_definitions.each_key do |attachment_name| + attachment = send(attachment_name) + + next if attachment.blank? || attachment.queued_for_write[:original].blank? + + attachment.instance_write :content_type, calculated_content_type(attachment) + end + end + def set_file_extensions self.class.attachment_definitions.each_key do |attachment_name| attachment = send(attachment_name) @@ -47,4 +58,10 @@ module Attachmentable extension end + + def calculated_content_type(attachment) + Paperclip.run('file', '-b --mime :file', file: attachment.queued_for_write[:original].path).split(/[:;\s]+/).first.chomp + rescue Terrapin::CommandLineError + '' + end end diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb index a9b6d96c6..30d9a9851 100644 --- a/app/models/media_attachment.rb +++ b/app/models/media_attachment.rb @@ -31,9 +31,9 @@ class MediaAttachment < ApplicationRecord AUDIO_FILE_EXTENSIONS = ['.ogg', '.oga', '.mp3', '.wav', '.flac', '.opus'].freeze IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze - VIDEO_MIME_TYPES = ['video/webm', 'video/mp4', 'video/quicktime'].freeze + VIDEO_MIME_TYPES = ['video/webm', 'video/mp4', 'video/quicktime', 'video/ogg'].freeze VIDEO_CONVERTIBLE_MIME_TYPES = ['video/webm', 'video/quicktime'].freeze - AUDIO_MIME_TYPES = ['audio/wave', 'audio/wav', 'audio/x-wav', 'audio/x-pn-wave', 'audio/ogg', 'audio/mpeg', 'audio/webm', 'audio/flac'].freeze + AUDIO_MIME_TYPES = ['audio/wave', 'audio/wav', 'audio/x-wav', 'audio/x-pn-wave', 'audio/ogg', 'audio/mpeg', 'audio/mp3', 'audio/webm', 'audio/flac'].freeze BLURHASH_OPTIONS = { x_comp: 4, -- cgit