Skip to content

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

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.rb
index 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%3F
if ent.respond_to?('is_binary_data?') && ent.is_binary_data? # Ruby 1.8.x and <1.9.2
@@ -481,7 +481,7 @@ class RepositoriesController < ApplicationController
def send_raw(content, path)
# Force the download
send_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_type
send_data content, send_opt
end
diff --git a/app/models/attachment.rb b/app/models/attachment.rb
index 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].freeze
belongs_to :container, polymorphic: true
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
@@ -115,15 +113,20 @@ class Attachment < ApplicationRecord
# images are sent inline
def inlineable?
- is_plain_text? || is_image? || is_pdf?
+ is_plain_text? || is_image? || is_movie? || is_pdf?
end
+ # rubocop:disable Naming/PredicateName
def is_plain_text?
- ALLOWED_TEXT_TYPES.include?(content_type)
+ OpenProject::MimeType.plain_text?(content_type)
end
def 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 < ApplicationRecord
def is_diff?
is_text? && filename =~ /\.(patch|diff)\z/i
end
+ # rubocop:enable Naming/PredicateName
# Returns true if the file is readable
def readable?
@@ -200,7 +204,7 @@ class Attachment < ApplicationRecord
end
def 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).detect
content_type || fallback
end
diff --git a/app/views/repositories/_dir_list_content.html.erb b/app/views/repositories/_dir_list_content.html.erb
index 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.rb
index 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.rb
similarity index 88%
rename from lib/redmine/mime_type.rb
rename to lib/open_project/mime_type.rb
index 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 OpenProject
module MimeType
MIME_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 }
map
end
+ 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 unknown
def self.of(name)
return nil unless name
diff --git a/spec_legacy/unit/lib/redmine/mime_type_spec.rb b/spec/lib/open_project/mime_type_spec.rb
similarity index 70%
rename from spec_legacy/unit/lib/redmine/mime_type_spec.rb
rename to spec/lib/open_project/mime_type_spec.rb
index 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' do
to_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
+ end
end
end
- it 'should css class of' do
+ describe '#css_class_of' do
to_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
+ end
end
end
- it 'should main mimetype of' do
+ describe '#main_mimetype_of' do
to_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
+ end
end
end
- it 'should is type' do
+ describe '#is_type?' do
to_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
+ end
end
end
- 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'
end
end
diff --git a/spec/models/attachment_spec.rb b/spec/models/attachment_spec.rb
index f816b38fe06..02434562605 100644
--- a/spec/models/attachment_spec.rb
+++ b/spec/models/attachment_spec.rb
@@ -170,6 +170,7 @@ describe Attachment, type: :model do
describe 'two attachments with same file name' do
let(:second_file) { FactoryBot.create :uploaded_jpg, name: file.original_filename }
+
it 'does not interfere' do
a1 = 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/MultipleMemoizedHelpers
describe "#external_url", with_direct_uploads: true do
let(:author) { FactoryBot.create :user }
@@ -265,6 +267,17 @@ describe Attachment, type: :model do
end
end
+ 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" do
before { binary_attachment.save! }
@@ -274,6 +287,7 @@ describe Attachment, type: :model do
end
end
end
+ # rubocop:enable RSpec/MultipleMemoizedHelpers
describe 'full text extraction job on commit' do
let(:created_attachment) do