Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
448bc31b
Commit
448bc31b
authored
Mar 24, 2021
by
Robert May
Committed by
Brett Walker
Mar 24, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create Gitlab::Utils::MimeType that wraps calls
to mimemagic gem. A different gem will replace this soon.
parent
726c581a
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
92 additions
and
5 deletions
+92
-5
app/controllers/projects/jobs_controller.rb
app/controllers/projects/jobs_controller.rb
+4
-2
app/uploaders/content_type_whitelist.rb
app/uploaders/content_type_whitelist.rb
+1
-1
changelogs/unreleased/remove-direct-mimemagic-dependency-minimal.yml
...unreleased/remove-direct-mimemagic-dependency-minimal.yml
+5
-0
lib/gitlab/utils/mime_type.rb
lib/gitlab/utils/mime_type.rb
+25
-0
spec/fixtures/rails_sample.bmp
spec/fixtures/rails_sample.bmp
+0
-0
spec/fixtures/rails_sample.png
spec/fixtures/rails_sample.png
+0
-0
spec/fixtures/rails_sample.tif
spec/fixtures/rails_sample.tif
+0
-0
spec/lib/gitlab/utils/mime_type_spec.rb
spec/lib/gitlab/utils/mime_type_spec.rb
+56
-0
spec/support/shared_contexts/upload_type_check_shared_context.rb
...pport/shared_contexts/upload_type_check_shared_context.rb
+1
-2
No files found.
app/controllers/projects/jobs_controller.rb
View file @
448bc31b
...
...
@@ -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
...
...
app/uploaders/content_type_whitelist.rb
View file @
448bc31b
...
...
@@ -43,7 +43,7 @@ module ContentTypeWhitelist
def
mime_magic_content_type
(
path
)
if
path
File
.
open
(
path
)
do
|
file
|
MimeMagic
.
by_magic
(
file
).
try
(
:typ
e
)
||
'invalid/invalid'
Gitlab
::
Utils
::
MimeType
.
from_io
(
fil
e
)
||
'invalid/invalid'
end
end
rescue
Errno
::
ENOENT
...
...
changelogs/unreleased/remove-direct-mimemagic-dependency-minimal.yml
0 → 100644
View file @
448bc31b
---
title
:
Refactor MimeMagic calls to new MimeType class
merge_request
:
57421
author
:
type
:
other
lib/gitlab/utils/mime_type.rb
0 → 100644
View file @
448bc31b
# 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
spec/fixtures/rails_sample.bmp
0 → 100644
View file @
448bc31b
348 KB
spec/fixtures/rails_sample.png
0 → 100644
View file @
448bc31b
83.8 KB
spec/fixtures/rails_sample.tif
0 → 100644
View file @
448bc31b
File added
spec/lib/gitlab/utils/mime_type_spec.rb
0 → 100644
View file @
448bc31b
# 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
spec/support/shared_contexts/upload_type_check_shared_context.rb
View file @
448bc31b
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment