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
b920ee4d
Commit
b920ee4d
authored
Jun 08, 2012
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move some IOAdapters code to superclass, fix bug with tildes in filenames
parent
e54c8363
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
56 additions
and
60 deletions
+56
-60
lib/paperclip.rb
+1
-0
lib/paperclip/io_adapters/abstract_adapter.rb
+42
-0
lib/paperclip/io_adapters/attachment_adapter.rb
+5
-9
lib/paperclip/io_adapters/file_adapter.rb
+1
-29
lib/paperclip/io_adapters/identity_adapter.rb
+1
-1
lib/paperclip/io_adapters/nil_adapter.rb
+1
-1
lib/paperclip/io_adapters/stringio_adapter.rb
+4
-8
lib/paperclip/io_adapters/uploaded_file_adapter.rb
+1
-12
No files found.
lib/paperclip.rb
View file @
b920ee4d
...
...
@@ -214,6 +214,7 @@ end
# This stuff needs to be run after Paperclip is defined.
require
'paperclip/io_adapters/registry'
require
'paperclip/io_adapters/abstract_adapter'
require
'paperclip/io_adapters/identity_adapter'
require
'paperclip/io_adapters/file_adapter'
require
'paperclip/io_adapters/stringio_adapter'
...
...
lib/paperclip/io_adapters/abstract_adapter.rb
0 → 100644
View file @
b920ee4d
module
Paperclip
class
AbstractAdapter
ILLEGAL_FILENAME_CHARACTERS
=
/^~/
private
def
destination
if
@destination
.
nil?
extension
=
File
.
extname
(
original_filename
)
basename
=
File
.
basename
(
original_filename
,
extension
)
basename
=
basename
.
gsub
(
ILLEGAL_FILENAME_CHARACTERS
,
'_'
)
dest
=
Tempfile
.
new
([
basename
,
extension
])
dest
.
binmode
@destination
=
dest
end
@destination
end
def
copy_to_tempfile
(
src
)
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
=
(
self
.
original_filename
.
match
(
/\.(\w+)$/
)[
1
]
rescue
"octet-stream"
).
downcase
mime_type
=
(
Paperclip
.
run
(
"file"
,
"-b --mime :file"
,
:file
=>
self
.
path
).
split
(
/[:;\s]+/
)[
0
]
rescue
"application/x-
#{
type
}
"
)
mime_type
=
"application/x-
#{
type
}
"
if
mime_type
.
match
(
/\(.*?\)/
)
mime_type
end
end
end
lib/paperclip/io_adapters/attachment_adapter.rb
View file @
b920ee4d
module
Paperclip
class
AttachmentAdapter
class
AttachmentAdapter
<
AbstractAdapter
def
initialize
(
target
)
@target
,
@style
=
case
target
when
Paperclip
::
Attachment
...
...
@@ -51,23 +51,19 @@ module Paperclip
private
def
cache_current_values
@tempfile
=
copy_to_tempfile
(
@target
)
@original_filename
=
@target
.
original_filename
@content_type
=
@target
.
content_type
@tempfile
=
copy_to_tempfile
(
@target
)
@size
=
@tempfile
.
size
||
@target
.
size
end
def
copy_to_tempfile
(
src
)
extension
=
File
.
extname
(
src
.
original_filename
)
basename
=
File
.
basename
(
src
.
original_filename
,
extension
)
dest
=
Tempfile
.
new
([
basename
,
extension
])
dest
.
binmode
if
src
.
respond_to?
:copy_to_local_file
src
.
copy_to_local_file
(
@style
,
dest
.
path
)
src
.
copy_to_local_file
(
@style
,
dest
ination
.
path
)
else
FileUtils
.
cp
(
src
.
path
(
@style
),
dest
.
path
)
FileUtils
.
cp
(
src
.
path
(
@style
),
dest
ination
.
path
)
end
dest
dest
ination
end
end
end
...
...
lib/paperclip/io_adapters/file_adapter.rb
View file @
b920ee4d
module
Paperclip
class
FileAdapter
class
FileAdapter
<
AbstractAdapter
def
initialize
(
target
)
@target
=
target
@tempfile
=
copy_to_tempfile
(
@target
)
...
...
@@ -52,34 +52,6 @@ module Paperclip
def
path
@tempfile
.
path
end
private
def
copy_to_tempfile
(
src
)
extension
=
File
.
extname
(
original_filename
)
basename
=
File
.
basename
(
original_filename
,
extension
)
dest
=
Tempfile
.
new
([
basename
,
extension
])
dest
.
binmode
FileUtils
.
cp
(
src
.
path
,
dest
.
path
)
dest
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
=
(
self
.
original_filename
.
match
(
/\.(\w+)$/
)[
1
]
rescue
"octet-stream"
).
downcase
mime_type
=
(
Paperclip
.
run
(
"file"
,
"-b --mime :file"
,
:file
=>
self
.
path
).
split
(
/[:;\s]+/
)[
0
]
rescue
"application/x-
#{
type
}
"
)
mime_type
=
"application/x-
#{
type
}
"
if
mime_type
.
match
(
/\(.*?\)/
)
mime_type
end
end
end
...
...
lib/paperclip/io_adapters/identity_adapter.rb
View file @
b920ee4d
module
Paperclip
class
IdentityAdapter
class
IdentityAdapter
<
AbstractAdapter
def
new
(
adapter
)
adapter
end
...
...
lib/paperclip/io_adapters/nil_adapter.rb
View file @
b920ee4d
module
Paperclip
class
NilAdapter
class
NilAdapter
<
AbstractAdapter
def
initialize
(
target
)
end
...
...
lib/paperclip/io_adapters/stringio_adapter.rb
View file @
b920ee4d
module
Paperclip
class
StringioAdapter
class
StringioAdapter
<
AbstractAdapter
def
initialize
(
target
)
@target
=
target
@tempfile
=
copy_to_tempfile
(
@target
)
...
...
@@ -47,15 +47,11 @@ module Paperclip
private
def
copy_to_tempfile
(
src
)
extension
=
File
.
extname
(
original_filename
)
basename
=
File
.
basename
(
original_filename
,
extension
)
dest
=
Tempfile
.
new
([
basename
,
extension
])
dest
.
binmode
while
data
=
src
.
read
(
16
*
1024
)
dest
.
write
(
data
)
dest
ination
.
write
(
data
)
end
dest
.
rewind
dest
dest
ination
.
rewind
dest
ination
end
end
...
...
lib/paperclip/io_adapters/uploaded_file_adapter.rb
View file @
b920ee4d
module
Paperclip
class
UploadedFileAdapter
class
UploadedFileAdapter
<
AbstractAdapter
def
initialize
(
target
)
@target
=
target
...
...
@@ -46,17 +46,6 @@ module Paperclip
def
path
@tempfile
.
path
end
private
def
copy_to_tempfile
(
src
)
extension
=
File
.
extname
(
original_filename
)
basename
=
File
.
basename
(
original_filename
,
extension
)
dest
=
Tempfile
.
new
([
basename
,
extension
])
dest
.
binmode
FileUtils
.
cp
(
src
.
path
,
dest
.
path
)
dest
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