OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Refactor MimeType into OpenProject scope and allow video inlines
Oliver Günther32b67aeca337
diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rbindex fa54b815e61..7b0ec95394e 100644--- a/app/controllers/repositories_controller.rb+++ b/app/controllers/repositories_controller.rb@@ -199,7 +199,7 @@ class RepositoriesController < ApplicationController# UTF-16 contains "\x00".# It is very strict that file contains less than 30% of ascii symbols# in non Western Europe.- return true if Redmine::MimeType.is_type?('text', path)+ return true if OpenProject::MimeType.is_type?('text', path)# Ruby 1.8.6 has a bug of integer divisions.# http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3Fif ent.respond_to?('is_binary_data?') && ent.is_binary_data? # Ruby 1.8.x and <1.9.2@@ -481,7 +481,7 @@ class RepositoriesController < ApplicationControllerdef send_raw(content, path)# Force the downloadsend_opt = { filename: filename_for_content_disposition(path.split('/').last) }- send_type = Redmine::MimeType.of(path)+ send_type = OpenProject::MimeType.of(path)send_opt[:type] = send_type.to_s if send_typesend_data content, send_optenddiff --git a/app/models/attachment.rb b/app/models/attachment.rbindex 8411b575ab1..25c1bf1bc87 100644--- a/app/models/attachment.rb+++ b/app/models/attachment.rb@@ -31,8 +31,6 @@require 'digest/md5'class Attachment < ApplicationRecord- ALLOWED_TEXT_TYPES = %w[text/plain].freeze- ALLOWED_IMAGE_TYPES = %w[image/gif image/jpeg image/png image/tiff image/bmp].freezebelongs_to :container, polymorphic: truebelongs_to :author, class_name: 'User', foreign_key: 'author_id'@@ -115,15 +113,20 @@ class Attachment < ApplicationRecord# images are sent inlinedef inlineable?- is_plain_text? || is_image? || is_pdf?+ is_plain_text? || is_image? || is_movie? || is_pdf?end+ # rubocop:disable Naming/PredicateNamedef is_plain_text?- ALLOWED_TEXT_TYPES.include?(content_type)+ OpenProject::MimeType.plain_text?(content_type)enddef is_image?- ALLOWED_IMAGE_TYPES.include?(content_type)+ OpenProject::MimeType.image?(content_type)+ end++ def is_movie?+ OpenProject::MimeType.movie?(content_type)end# backwards compatibility for plugins@@ -140,6 +143,7 @@ class Attachment < ApplicationRecorddef is_diff?is_text? && filename =~ /\.(patch|diff)\z/iend+ # rubocop:enable Naming/PredicateName# Returns true if the file is readabledef readable?@@ -200,7 +204,7 @@ class Attachment < ApplicationRecordenddef self.content_type_for(file_path, fallback = OpenProject::ContentTypeDetector::SENSIBLE_DEFAULT)- content_type = Redmine::MimeType.narrow_type file_path, OpenProject::ContentTypeDetector.new(file_path).detect+ content_type = OpenProject::MimeType.narrow_type file_path, OpenProject::ContentTypeDetector.new(file_path).detectcontent_type || fallbackenddiff --git a/app/views/repositories/_dir_list_content.html.erb b/app/views/repositories/_dir_list_content.html.erbindex 6ecacbd666a..0988036b4fb 100644--- a/app/views/repositories/_dir_list_content.html.erb+++ b/app/views/repositories/_dir_list_content.html.erb@@ -64,7 +64,7 @@ See COPYRIGHT and LICENSE files for more details.<%= link_to({ action: (entry.dir? ? 'show' : 'changes'), project_id: @project, repo_path: to_path_param(ent_path), rev: @rev },class: 'icon-context',aria: { label: (entry.dir? ? t('label_folder') : t('activerecord.attributes.attachment.file')) + " #{h(ent_name)}" }) do %>- <%= op_icon(entry.dir? ? 'icon-folder' : "icon-ticket #{Redmine::MimeType.css_class_of(ent_name)}") %>+ <%= op_icon(entry.dir? ? 'icon-folder' : "icon-ticket #{OpenProject::MimeType.css_class_of(ent_name)}") %><%= h(ent_name) %><% end %></td>diff --git a/lib/open_project.rb b/lib/open_project.rbindex 9fbe975db61..13bfe0c9387 100644--- a/lib/open_project.rb+++ b/lib/open_project.rb@@ -33,7 +33,7 @@ require 'redmine/search'require 'open_project/custom_field_format'require 'open_project/logging'require 'open_project/patches'-require 'redmine/mime_type'+require 'open_project/mime_type'require 'open_project/custom_styles/design'require 'open_project/hook'require 'open_project/hooks'diff --git a/lib/redmine/mime_type.rb b/lib/open_project/mime_type.rbsimilarity index 88%rename from lib/redmine/mime_type.rbrename to lib/open_project/mime_type.rbindex 576a2dbce89..d49ad8b1a71 100644--- a/lib/redmine/mime_type.rb+++ b/lib/open_project/mime_type.rb@@ -28,7 +28,7 @@# See COPYRIGHT and LICENSE files for more details.#++-module Redmine+module OpenProjectmodule MimeTypeMIME_TYPES = {'text/plain' => 'txt,tpl,properties,patch,diff,ini,readme,install,upgrade',@@ -79,16 +79,33 @@ module Redmine'video/mpeg' => 'mpeg,mpg,mpe','video/mp4' => 'mp4','video/x-ms-wmv' => 'wmv',+ 'video/webm' => 'webm','video/quicktime' => 'qt,mov','video/vnd.vivo' => 'viv,vivo','video/x-msvideo' => 'avi'}.freeze+ PLAIN_TEXT_TYPES = %w[text/plain].freeze+ INLINE_IMAGE_TYPES = %w[image/gif image/jpeg image/png image/apng image/tiff image/bmp image/webp].freeze+ INLINE_MOVIE_TYPES = %w[video/x-msvideo video/quicktime video/mpeg video/mp4 video/webm video/x-ms-wmv].freeze+EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)|exts.split(',').each { |ext| map[ext.strip] = type }mapend+ def self.movie?(mime_type)+ INLINE_MOVIE_TYPES.include?(mime_type)+ end++ def self.image?(mime_type)+ INLINE_IMAGE_TYPES.include?(mime_type)+ end++ def self.plain_text?(mime_type)+ PLAIN_TEXT_TYPES.include?(mime_type)+ end+# returns mime type for name or nil if unknowndef self.of(name)return nil unless namediff --git a/spec_legacy/unit/lib/redmine/mime_type_spec.rb b/spec/lib/open_project/mime_type_spec.rbsimilarity index 70%rename from spec_legacy/unit/lib/redmine/mime_type_spec.rbrename to spec/lib/open_project/mime_type_spec.rbindex 4f8d2086c6a..701ef010118 100644--- a/spec_legacy/unit/lib/redmine/mime_type_spec.rb+++ b/spec/lib/open_project/mime_type_spec.rb@@ -27,50 +27,59 @@## See COPYRIGHT and LICENSE files for more details.#++-require_relative '../../../legacy_spec_helper'+require 'spec_helper'-describe Redmine::MimeType do- it 'should of' do+describe OpenProject::MimeType do+ describe '#of' doto_test = { 'test.unk' => nil,'test.txt' => 'text/plain','test.c' => 'text/x-c' }to_test.each do |name, expected|- assert_equal expected, Redmine::MimeType.of(name)+ it do+ expect(described_class.of(name)).to eq expected+ endendend- it 'should css class of' do+ describe '#css_class_of' doto_test = { 'test.unk' => nil,'test.txt' => 'text-plain','test.c' => 'text-x-c' }to_test.each do |name, expected|- assert_equal expected, Redmine::MimeType.css_class_of(name)+ it do+ expect(described_class.css_class_of(name)).to eq expected+ endendend- it 'should main mimetype of' do+ describe '#main_mimetype_of' doto_test = { 'test.unk' => nil,'test.txt' => 'text','test.c' => 'text' }to_test.each do |name, expected|- assert_equal expected, Redmine::MimeType.main_mimetype_of(name)+ it do+ expect(described_class.main_mimetype_of(name)).to eq expected+ endendend- it 'should is type' do+ describe '#is_type?' doto_test = { ['text', 'test.unk'] => false,['text', 'test.txt'] => true,['text', 'test.c'] => true }+to_test.each do |args, expected|- assert_equal expected, Redmine::MimeType.is_type?(*args)+ it do+ expect(described_class.is_type?(*args)).to eq expected+ endendend- it 'should narrow type for equal main type' do- assert_equal 'text/x-ruby', Redmine::MimeType.narrow_type('rubyfile.rb', 'text/plain')+ it 'equals the main type for the narrow type' do+ expect(described_class.narrow_type('rubyfile.rb', 'text/plain')).to eq 'text/x-ruby'end- it 'should use original type if main type differs' do- assert_equal 'application/zip', Redmine::MimeType.narrow_type('rubyfile.rb', 'application/zip')+ it 'uses original type if main type differs' do+ expect(described_class.narrow_type('rubyfile.rb', 'application/zip')).to eq 'application/zip'endenddiff --git a/spec/models/attachment_spec.rb b/spec/models/attachment_spec.rbindex f816b38fe06..02434562605 100644--- a/spec/models/attachment_spec.rb+++ b/spec/models/attachment_spec.rb@@ -170,6 +170,7 @@ describe Attachment, type: :model dodescribe 'two attachments with same file name' dolet(:second_file) { FactoryBot.create :uploaded_jpg, name: file.original_filename }+it 'does not interfere' doa1 = Attachment.create!(container: work_package,file: file,@@ -205,6 +206,7 @@ describe Attachment, type: :model do# We just use with_direct_uploads here to make sure the# FogAttachment class is defined and Fog is mocked.+ # rubocop:disable RSpec/MultipleMemoizedHelpersdescribe "#external_url", with_direct_uploads: true dolet(:author) { FactoryBot.create :user }@@ -265,6 +267,17 @@ describe Attachment, type: :model doendend+ describe 'for a video file' do+ let(:attachment) { described_class.new }++ it 'assumes it to be inlineable' do+ %w[video/webm video/mp4 video/quicktime].each do |content_type|+ attachment.content_type = content_type+ expect(attachment).to be_inlineable, "#{content_type} should be inlineable"+ end+ end+ end+describe "for a binary file" dobefore { binary_attachment.save! }@@ -274,6 +287,7 @@ describe Attachment, type: :model doendendend+ # rubocop:enable RSpec/MultipleMemoizedHelpersdescribe 'full text extraction job on commit' dolet(:created_attachment) do