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
f1f446c2
Commit
f1f446c2
authored
Mar 01, 2013
by
Zach Millman
Committed by
Jon Yurek
Jul 30, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add documentation of content-type detection strategy
Also refactored to make the strategy hierarchy clearer
parent
d42adb8a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
16 deletions
+35
-16
lib/paperclip/content_type_detector.rb
+35
-16
No files found.
lib/paperclip/content_type_detector.rb
View file @
f1f446c2
module
Paperclip
module
Paperclip
class
ContentTypeDetector
class
ContentTypeDetector
# The content-type detection strategy is as follows:
#
# 1. Blank/Empty files: If there's no filename or the file is empty, provide a sensible default
# (application/octet-stream or inode/x-empty)
#
# 2. Uploaded file: Use the uploaded file's content type if it is in the list of mime-types
# for the file's extension
#
# 3. Standard types: Return the first standard (without an x- prefix) entry in the list of
# mime-types
#
# 4. Experimental types: If there were no standard types in the mime-types list, try to return
# the first experimental one
#
# 5. Unrecognized extension: Use the file's content type or a sensible default if there are
# no entries in mime-types for the extension
#
EMPTY_TYPE
=
"inode/x-empty"
EMPTY_TYPE
=
"inode/x-empty"
SENSIBLE_DEFAULT
=
"application/octet-stream"
SENSIBLE_DEFAULT
=
"application/octet-stream"
...
@@ -7,17 +25,16 @@ module Paperclip
...
@@ -7,17 +25,16 @@ module Paperclip
@filename
=
filename
@filename
=
filename
end
end
# Returns a String describing the file's content type
def
detect
def
detect
if
blank_name?
if
blank_name?
SENSIBLE_DEFAULT
SENSIBLE_DEFAULT
elsif
empty_file?
elsif
empty_file?
EMPTY_TYPE
EMPTY_TYPE
elsif
!
match?
elsif
best_from_possible_types
type_from_file_command
best_from_possible_types
.
content_type
elsif
!
multiple?
possible_types
.
first
else
else
best_type_match
type_from_file_command
||
SENSIBLE_DEFAULT
end
.
to_s
end
.
to_s
end
end
...
@@ -30,23 +47,25 @@ module Paperclip
...
@@ -30,23 +47,25 @@ module Paperclip
def
blank_name?
def
blank_name?
@filename
.
nil?
||
@filename
.
empty?
@filename
.
nil?
||
@filename
.
empty?
end
end
def
empty?
File
.
exists?
(
@filename
)
&&
File
.
size
(
@filename
)
==
0
end
def
possible_types
def
possible_types
@possible_types
||=
MIME
::
Types
.
type_for
(
@filename
)
@possible_types
||=
MIME
::
Types
.
type_for
(
@filename
)
end
end
def
match?
def
official_types
possible_types
.
length
>
0
@official_types
||=
possible_types
.
reject
{
|
type
|
type
.
content_type
.
match
(
/\/x-/
)
}
end
end
def
multiple?
def
types_matching_file
possible_types
.
length
>
1
possible_types
.
select
{
|
type
|
type
.
content_type
==
type_from_file_command
}
end
end
def
best_type_match
def
best_from_possible_types
types_matching_file
=
possible_types
.
select
{
|
type
|
type
.
content_type
==
type_from_file_command
}
@best_from_possible_types
||=
(
types_matching_file
.
first
||
official_types
.
first
||
possible_types
.
first
)
official_types
=
possible_types
.
reject
{
|
type
|
type
.
content_type
.
match
(
/\/x-/
)
}
(
types_matching_file
.
first
||
official_types
.
first
||
possible_types
.
first
).
content_type
end
end
def
type_from_file_command
def
type_from_file_command
...
...
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