Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
paperclip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
paperclip
Commits
9ecd52ea
Commit
9ecd52ea
authored
Apr 26, 2015
by
Stephen Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix some style guide issues
parent
2a0274d1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
15 additions
and
12 deletions
+15
-12
lib/paperclip/content_type_detector.rb
+11
-9
lib/paperclip/file_command_content_type_detector.rb
+1
-1
spec/paperclip/content_type_detector_spec.rb
+3
-2
No files found.
lib/paperclip/content_type_detector.rb
View file @
9ecd52ea
...
@@ -54,24 +54,26 @@ module Paperclip
...
@@ -54,24 +54,26 @@ module Paperclip
end
end
def
calculated_type_matches
def
calculated_type_matches
possible_types
.
select
{
|
content_type
|
content_type
==
type_from_file_contents
}
possible_types
.
select
do
|
content_type
|
content_type
==
type_from_file_contents
end
end
end
def
type_from_file_contents
def
type_from_file_contents
type
=
begin
type_from_mime_magic
||
type_from_file_command
type_from_mime_magic
||
type_from_file_command
rescue
Errno
::
ENOENT
=>
e
rescue
Errno
::
ENOENT
=>
e
Paperclip
.
log
(
"Error while determining content type:
#{
e
}
"
)
Paperclip
.
log
(
"Error while determining content type:
#{
e
}
"
)
SENSIBLE_DEFAULT
SENSIBLE_DEFAULT
end
end
end
def
type_from_file_command
def
type_from_file_command
@type_from_file_command
||=
FileCommandContentTypeDetector
.
new
(
@filename
).
detect
@type_from_file_command
||=
FileCommandContentTypeDetector
.
new
(
@filename
).
detect
end
end
def
type_from_mime_magic
def
type_from_mime_magic
@type_from_mime_magic
||=
MimeMagic
.
by_magic
(
File
.
open
(
@filename
)).
try
(
:type
)
@type_from_mime_magic
||=
MimeMagic
.
by_magic
(
File
.
open
(
@filename
)).
try
(
:type
)
end
end
end
end
end
end
lib/paperclip/file_command_content_type_detector.rb
View file @
9ecd52ea
...
@@ -13,8 +13,8 @@ module Paperclip
...
@@ -13,8 +13,8 @@ module Paperclip
private
private
def
type_from_file_command
def
type_from_file_command
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
type
=
begin
type
=
begin
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
Paperclip
.
run
(
"file"
,
"-b --mime :file"
,
file:
@filename
)
Paperclip
.
run
(
"file"
,
"-b --mime :file"
,
file:
@filename
)
rescue
Cocaine
::
CommandLineError
=>
e
rescue
Cocaine
::
CommandLineError
=>
e
Paperclip
.
log
(
"Error while determining content type:
#{
e
}
"
)
Paperclip
.
log
(
"Error while determining content type:
#{
e
}
"
)
...
...
spec/paperclip/content_type_detector_spec.rb
View file @
9ecd52ea
...
@@ -4,7 +4,7 @@ describe Paperclip::ContentTypeDetector do
...
@@ -4,7 +4,7 @@ describe Paperclip::ContentTypeDetector do
it
'returns a meaningful content type for open xml spreadsheets'
do
it
'returns a meaningful content type for open xml spreadsheets'
do
file
=
File
.
new
(
fixture_file
(
"empty.xlsx"
))
file
=
File
.
new
(
fixture_file
(
"empty.xlsx"
))
assert_equal
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
,
assert_equal
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
,
Paperclip
::
ContentTypeDetector
.
new
(
file
.
path
).
detect
Paperclip
::
ContentTypeDetector
.
new
(
file
.
path
).
detect
end
end
it
'gives a sensible default when the name is empty'
do
it
'gives a sensible default when the name is empty'
do
...
@@ -19,7 +19,8 @@ describe Paperclip::ContentTypeDetector do
...
@@ -19,7 +19,8 @@ describe Paperclip::ContentTypeDetector do
it
'returns content type of file if it is an acceptable type'
do
it
'returns content type of file if it is an acceptable type'
do
MIME
::
Types
.
stubs
(
:type_for
).
returns
([
MIME
::
Type
.
new
(
'application/mp4'
),
MIME
::
Type
.
new
(
'video/mp4'
),
MIME
::
Type
.
new
(
'audio/mp4'
)])
MIME
::
Types
.
stubs
(
:type_for
).
returns
([
MIME
::
Type
.
new
(
'application/mp4'
),
MIME
::
Type
.
new
(
'video/mp4'
),
MIME
::
Type
.
new
(
'audio/mp4'
)])
Paperclip
::
ContentTypeDetector
.
any_instance
.
stubs
(
:type_from_file_contents
).
returns
(
"video/mp4"
)
Paperclip
::
ContentTypeDetector
.
any_instance
.
stubs
(
:type_from_file_contents
).
returns
(
"video/mp4"
)
@filename
=
"my_file.mp4"
@filename
=
"my_file.mp4"
assert_equal
"video/mp4"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
assert_equal
"video/mp4"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
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