Commit f31e6437 authored by Thong Kuah's avatar Thong Kuah

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

Initial introduction of Gitlab::Utils::MimeType class

See merge request gitlab-org/gitlab!57421
parents a35854fe d6f6d0d9
...@@ -226,10 +226,10 @@ class Projects::JobsController < Projects::ApplicationController ...@@ -226,10 +226,10 @@ class Projects::JobsController < Projects::ApplicationController
end end
def raw_trace_content_disposition(raw_data) def raw_trace_content_disposition(raw_data)
mime_type = MimeMagic.by_magic(raw_data) mime_type = Gitlab::Utils::MimeType.from_string(raw_data)
# if mime_type is nil can also represent 'text/plain' # if mime_type is nil can also represent 'text/plain'
return 'inline' if mime_type.nil? || mime_type.type == 'text/plain' return 'inline' if mime_type.nil? || mime_type == 'text/plain'
'attachment' 'attachment'
end end
......
...@@ -43,7 +43,7 @@ module ContentTypeWhitelist ...@@ -43,7 +43,7 @@ module ContentTypeWhitelist
def mime_magic_content_type(path) def mime_magic_content_type(path)
if path if path
File.open(path) do |file| File.open(path) do |file|
MimeMagic.by_magic(file).try(:type) || 'invalid/invalid' Gitlab::Utils::MimeType.from_io(file) || 'invalid/invalid'
end end
end end
rescue Errno::ENOENT rescue Errno::ENOENT
......
---
title: Refactor MimeMagic calls to new MimeType class
merge_request: 57421
author:
type: other
# frozen_string_literal: true
# This wraps calls to a gem which support mime type detection.
# Currently uses `mimemagic`, but that will be replaced shortly.
module Gitlab
module Utils
class MimeType
class << self
def from_io(io)
return unless io.is_a?(IO) || io.is_a?(StringIO)
MimeMagic.by_magic(io).try(:type)
end
def from_string(string)
return unless string.is_a?(String)
MimeMagic.by_magic(string)
end
end
end
end
end
# frozen_string_literal: true
require "fast_spec_helper"
require "rspec/parameterized"
RSpec.describe Gitlab::Utils::MimeType do
describe ".from_io" do
subject { described_class.from_io(io) }
context "input isn't an IO" do
let(:io) { "test" }
it "returns nil" do
expect(subject).to be_nil
end
end
context "input is a file" do
using RSpec::Parameterized::TableSyntax
where(:fixture, :mime_type) do
"banana_sample.gif" | "image/gif"
"rails_sample.jpg" | "image/jpeg"
"rails_sample.png" | "image/png"
"rails_sample.bmp" | "image/bmp"
"rails_sample.tif" | "image/tiff"
"blockquote_fence_before.md" | nil
"csv_empty.csv" | nil
end
with_them do
let(:io) { File.open(File.join(__dir__, "../../../fixtures", fixture)) }
it { is_expected.to eq(mime_type) }
end
end
end
describe ".from_string" do
subject { described_class.from_string(str) }
context "input isn't a string" do
let(:str) { nil }
it "returns nil" do
expect(subject).to be_nil
end
end
context "input is a string" do
let(:str) { "plain text" }
it { is_expected.to eq(nil) }
end
end
end
...@@ -13,7 +13,6 @@ end ...@@ -13,7 +13,6 @@ end
# @param mime_type [String] mime type to forcibly detect. # @param mime_type [String] mime type to forcibly detect.
RSpec.shared_context 'force content type detection to mime_type' do RSpec.shared_context 'force content type detection to mime_type' do
before do before do
magic_mime_obj = MimeMagic.new(mime_type) allow(Gitlab::Utils::MimeType).to receive(:from_io).and_return(mime_type)
allow(MimeMagic).to receive(:by_magic).with(anything).and_return(magic_mime_obj)
end 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