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
f407412f
Commit
f407412f
authored
Jun 15, 2012
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds a class for managing content type detection
parent
0eae7dbe
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
79 additions
and
30 deletions
+79
-30
lib/paperclip.rb
+1
-0
lib/paperclip/content_type_detector.rb
+46
-0
lib/paperclip/io_adapters/abstract_adapter.rb
+0
-18
lib/paperclip/io_adapters/file_adapter.rb
+1
-12
test/content_type_detector_test.rb
+31
-0
No files found.
lib/paperclip.rb
View file @
f407412f
...
...
@@ -40,6 +40,7 @@ require 'paperclip/attachment'
require
'paperclip/attachment_options'
require
'paperclip/storage'
require
'paperclip/callbacks'
require
'paperclip/content_type_detector'
require
'paperclip/glue'
require
'paperclip/errors'
require
'paperclip/missing_attachment_styles'
...
...
lib/paperclip/content_type_detector.rb
0 → 100644
View file @
f407412f
module
Paperclip
class
ContentTypeDetector
def
initialize
(
filename
)
@filename
=
filename
end
def
detect
if
!
match?
type_from_file_command
elsif
!
multiple?
possible_types
.
first
else
best_type_match
end
.
to_s
end
private
def
possible_types
@possible_types
||=
MIME
::
Types
.
type_for
(
@filename
)
end
def
match?
possible_types
.
length
>
0
end
def
multiple?
possible_types
.
length
>
1
end
def
best_type_match
official_types
=
possible_types
.
reject
{
|
type
|
type
.
content_type
.
match
(
/\/x-/
)
}
(
official_types
.
first
||
possible_types
.
first
).
content_type
end
def
type_from_file_command
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
type
=
Paperclip
.
run
(
"file"
,
"-b --mime :file"
,
:file
=>
@filename
)
if
type
.
match
(
/\(.*?\)/
)
type
=
"application/octet-stream"
end
type
.
split
(
/[:;\s]+/
)[
0
]
end
end
end
lib/paperclip/io_adapters/abstract_adapter.rb
View file @
f407412f
...
...
@@ -23,23 +23,5 @@ module Paperclip
FileUtils
.
cp
(
src
.
path
,
destination
.
path
)
destination
end
def
best_content_type_option
(
types
)
best
=
types
.
reject
{
|
type
|
type
.
content_type
.
match
(
/\/x-/
)
}
if
best
.
size
==
0
types
.
first
.
content_type
else
best
.
first
.
content_type
end
end
def
type_from_file_command
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
type
=
(
File
.
extname
(
self
.
path
.
to_s
)).
downcase
type
=
"octet-stream"
if
type
.
empty?
mime_type
=
Paperclip
.
run
(
"file"
,
"-b --mime :file"
,
:file
=>
self
.
path
).
split
(
/[:;\s]+/
)[
0
]
mime_type
=
"application/x-
#{
type
}
"
if
mime_type
.
match
(
/\(.*?\)/
)
mime_type
end
end
end
lib/paperclip/io_adapters/file_adapter.rb
View file @
f407412f
...
...
@@ -11,20 +11,9 @@ module Paperclip
@original_filename
=
@target
.
original_filename
if
@target
.
respond_to?
(
:original_filename
)
@original_filename
||=
File
.
basename
(
@target
.
path
)
@tempfile
=
copy_to_tempfile
(
@target
)
@content_type
=
calculate_content_type
@content_type
=
ContentTypeDetector
.
new
(
@target
.
path
).
detect
@size
=
File
.
size
(
@target
)
end
def
calculate_content_type
types
=
MIME
::
Types
.
type_for
(
original_filename
)
if
types
.
length
==
0
type_from_file_command
elsif
types
.
length
==
1
types
.
first
.
content_type
else
best_content_type_option
(
types
)
end
end
end
end
...
...
test/content_type_detector_test.rb
0 → 100644
View file @
f407412f
require
'./test/helper'
class
ContentTypeDetectorTest
<
Test
::
Unit
::
TestCase
context
'given a name'
do
should
'return a content type based on that name'
do
@filename
=
"/path/to/something.jpg"
assert_equal
"image/jpeg"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
should
'return a content type based on the content of the file'
do
tempfile
=
Tempfile
.
new
(
"something"
)
tempfile
.
write
(
"This is a file."
)
tempfile
.
rewind
assert_equal
"text/plain"
,
Paperclip
::
ContentTypeDetector
.
new
(
tempfile
.
path
).
detect
end
should
'return a sensible default if something goes wrong'
do
@filename
=
"/path/to/something"
assert_equal
"application/octet-stream"
,
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
should
'let errors raise if something blows up'
do
Paperclip
.
stubs
(
:run
).
raises
(
Cocaine
::
CommandLineError
.
new
)
@filename
=
"/path/to/something"
assert_raises
(
Cocaine
::
CommandLineError
)
do
Paperclip
::
ContentTypeDetector
.
new
(
@filename
).
detect
end
end
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