Commit 658fef82 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'remove-direct-mimemagic-dependency' into 'master'

Replace mimemagic dependency and introduce a Gitlab::Utils::MimeType class

See merge request gitlab-org/gitlab!57387
parents 1f835de1 c3b64d9c
......@@ -274,7 +274,7 @@ gem 'licensee', '~> 9.14.1'
gem 'charlock_holmes', '~> 0.7.7'
# Detect mime content type from content
gem 'mimemagic', '~> 0.3.2'
gem 'ruby-magic-static'
# Faster blank
gem 'fast_blank'
......
......@@ -1109,6 +1109,7 @@ GEM
i18n
ruby-fogbugz (0.2.1)
crack (~> 0.4)
ruby-magic-static (0.3.0)
ruby-prof (1.3.1)
ruby-progressbar (1.11.0)
ruby-saml (1.7.2)
......@@ -1481,7 +1482,6 @@ DEPENDENCIES
marginalia (~> 1.10.0)
memory_profiler (~> 0.9)
method_source (~> 1.0)
mimemagic (~> 0.3.2)
mini_magick (~> 4.10.1)
minitest (~> 5.11.0)
multi_json (~> 1.14.1)
......@@ -1555,6 +1555,7 @@ DEPENDENCIES
rspec_junit_formatter
rspec_profiling (~> 0.0.6)
ruby-fogbugz (~> 0.2.1)
ruby-magic-static
ruby-prof (~> 1.3.0)
ruby-progressbar (~> 1.10)
ruby_parser (~> 3.15)
......
---
title: Remove direct mimemagic dependency
merge_request: 57387
author:
type: other
# frozen_string_literal: true
require 'magic'
# This wraps calls to a gem which support mime type detection.
# Currently uses `mimemagic`, but that will be replaced shortly.
# We use the `ruby-magic` gem instead of `mimemagic` due to licensing issues
module Gitlab
module Utils
class MimeType
......@@ -9,13 +10,14 @@ module Gitlab
def from_io(io)
return unless io.is_a?(IO) || io.is_a?(StringIO)
MimeMagic.by_magic(io).try(:type)
mime_type = File.magic(io, Magic::MIME_TYPE)
mime_type == 'inode/x-empty' ? nil : mime_type
end
def from_string(string)
return unless string.is_a?(String)
MimeMagic.by_magic(string)
string.type
end
end
end
......
......@@ -24,8 +24,9 @@ RSpec.describe Gitlab::Utils::MimeType do
"rails_sample.png" | "image/png"
"rails_sample.bmp" | "image/bmp"
"rails_sample.tif" | "image/tiff"
"blockquote_fence_before.md" | nil
"csv_empty.csv" | nil
"sample.ico" | "image/vnd.microsoft.icon"
"blockquote_fence_before.md" | "text/plain"
"csv_empty.csv" | "application/x-empty"
end
with_them do
......@@ -50,7 +51,7 @@ RSpec.describe Gitlab::Utils::MimeType do
context "input is a string" do
let(:str) { "plain text" }
it { is_expected.to eq(nil) }
it { is_expected.to eq('text/plain') }
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment