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
5be67595
Commit
5be67595
authored
Jun 29, 2011
by
Alex Godin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
working implementation of the mime-types gem
parent
1ff1cc73
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
23 additions
and
16 deletions
+23
-16
Gemfile
+1
-0
Gemfile.lock
+1
-0
lib/paperclip/upfile.rb
+18
-14
test/helper.rb
+1
-0
test/upfile_test.rb
+2
-2
No files found.
Gemfile
View file @
5be67595
...
@@ -10,3 +10,4 @@ gem "appraisal"
...
@@ -10,3 +10,4 @@ gem "appraisal"
gem
"
fog
"
gem
"
fog
"
gem
"
bundler
"
gem
"
bundler
"
gem
"
cocaine
"
gem
"
cocaine
"
gem
"
mime-types
"
Gemfile.lock
View file @
5be67595
...
@@ -69,6 +69,7 @@ DEPENDENCIES
...
@@ -69,6 +69,7 @@ DEPENDENCIES
bundler
bundler
cocaine
cocaine
fog
fog
mime-types
mocha
mocha
rake
rake
shoulda
shoulda
...
...
lib/paperclip/upfile.rb
View file @
5be67595
...
@@ -6,24 +6,28 @@ module Paperclip
...
@@ -6,24 +6,28 @@ module Paperclip
# Infer the MIME-type of the file from the extension.
# Infer the MIME-type of the file from the extension.
def
content_type
def
content_type
type
=
(
self
.
original_filename
.
match
(
/\.(\w+)$/
)[
1
]
rescue
"octet-stream"
).
downcase
types
=
MIME
::
Types
.
type_for
(
self
.
original_filename
)
case
type
if
types
.
length
==
0
when
%r"jp(e|g|eg)"
then
"image/jpeg"
type_from_file_command
when
%r"tiff?"
then
"image/tiff"
elsif
types
.
length
==
1
when
%r"png"
,
"gif"
,
"bmp"
then
"image/
#{
type
}
"
types
.
first
.
content_type
when
%r"svg"
then
"image/svg+xml"
when
"txt"
then
"text/plain"
when
%r"html?"
then
"text/html"
when
"js"
then
"application/js"
when
"csv"
,
"xml"
,
"css"
then
"text/
#{
type
}
"
else
else
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
iterate_over_array_to_find_best_option
(
types
)
content_type
=
(
Paperclip
.
run
(
"file"
,
"-b --mime-type :file"
,
:file
=>
self
.
path
).
split
(
':'
).
last
.
strip
rescue
"application/x-
#{
type
}
"
)
content_type
=
"application/x-
#{
type
}
"
if
content_type
.
match
(
/\(.*?\)/
)
content_type
end
end
end
end
def
iterate_over_array_to_find_best_option
(
types
)
types
.
reject
{
|
type
|
type
.
content_type
.
match
(
/\/x-/
)
}.
first
end
def
type_from_file_command
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
type
=
(
self
.
original_filename
.
match
(
/\.(\w+)$/
)[
1
]
rescue
"octet-stream"
).
downcase
mime_type
=
(
Paperclip
.
run
(
"file"
,
"-b --mime-type :file"
,
:file
=>
self
.
path
).
split
(
':'
).
last
.
strip
rescue
"application/x-
#{
type
}
"
)
mime_type
=
"application/x-
#{
type
}
"
if
mime_type
.
match
(
/\(.*?\)/
)
mime_type
end
# Returns the file's normal name.
# Returns the file's normal name.
def
original_filename
def
original_filename
File
.
basename
(
self
.
path
)
File
.
basename
(
self
.
path
)
...
...
test/helper.rb
View file @
5be67595
...
@@ -8,6 +8,7 @@ require 'mocha'
...
@@ -8,6 +8,7 @@ require 'mocha'
require
'active_record'
require
'active_record'
require
'active_record/version'
require
'active_record/version'
require
'active_support'
require
'active_support'
require
'mime/types'
puts
"Testing against version
#{
ActiveRecord
::
VERSION
::
STRING
}
"
puts
"Testing against version
#{
ActiveRecord
::
VERSION
::
STRING
}
"
...
...
test/upfile_test.rb
View file @
5be67595
...
@@ -10,9 +10,9 @@ class UpfileTest < Test::Unit::TestCase
...
@@ -10,9 +10,9 @@ class UpfileTest < Test::Unit::TestCase
%w(txt)
=>
'text/plain'
,
%w(txt)
=>
'text/plain'
,
%w(htm html)
=>
'text/html'
,
%w(htm html)
=>
'text/html'
,
%w(csv)
=>
'text/csv'
,
%w(csv)
=>
'text/csv'
,
%w(xml)
=>
'
text
/xml'
,
%w(xml)
=>
'
application
/xml'
,
%w(css)
=>
'text/css'
,
%w(css)
=>
'text/css'
,
%w(js)
=>
'application/j
s
'
,
%w(js)
=>
'application/j
avascript
'
,
%w(foo)
=>
'application/x-foo'
%w(foo)
=>
'application/x-foo'
}.
each
do
|
extensions
,
content_type
|
}.
each
do
|
extensions
,
content_type
|
extensions
.
each
do
|
extension
|
extensions
.
each
do
|
extension
|
...
...
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