Commit 448bc31b authored by Robert May's avatar Robert May Committed by Brett Walker

Create Gitlab::Utils::MimeType that wraps calls

to mimemagic gem.

A different gem will replace this soon.
parent 726c581a
......@@ -226,10 +226,12 @@ class Projects::JobsController < Projects::ApplicationController
end
def raw_trace_content_disposition(raw_data)
mime_type = MimeMagic.by_magic(raw_data)
return 'inline' if raw_data.nil?
mime_type = Gitlab::Utils::MimeType.from_string(raw_data)
# 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'
end
......
......@@ -43,7 +43,7 @@ module ContentTypeWhitelist
def mime_magic_content_type(path)
if path
File.open(path) do |file|
MimeMagic.by_magic(file).try(:type) || 'invalid/invalid'
Gitlab::Utils::MimeType.from_io(file) || 'invalid/invalid'
end
end
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
Error = Class.new(StandardError)
class << self
def from_io(io)
raise Error, "input isn't an IO object" unless io.is_a?(IO) || io.is_a?(StringIO)
MimeMagic.by_magic(io).try(:type)
end
def from_string(string)
raise Error, "input isn't a string" 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 "raises an error" do
expect { subject }.to raise_error(Gitlab::Utils::MimeType::Error)
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 "raises an error" do
expect { subject }.to raise_error(Gitlab::Utils::MimeType::Error)
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
# @param mime_type [String] mime type to forcibly detect.
RSpec.shared_context 'force content type detection to mime_type' do
before do
magic_mime_obj = MimeMagic.new(mime_type)
allow(MimeMagic).to receive(:by_magic).with(anything).and_return(magic_mime_obj)
allow(Gitlab::Utils::MimeType).to receive(:from_io).and_return(mime_type)
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