Permalink
Browse files

Move some IOAdapters code to superclass, fix bug with tildes in filen…

…ames
  • Loading branch information...
1 parent e54c836 commit b920ee4d7408765245823a2884a049a6c6c62a36 @jyurek jyurek committed Jun 8, 2012
View
@@ -214,6 +214,7 @@ def attachment_definitions
# 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'
@@ -0,0 +1,42 @@
+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
@@ -1,5 +1,5 @@
module Paperclip
- class AttachmentAdapter
+ class AttachmentAdapter < AbstractAdapter
def initialize(target)
@target, @style = case target
when Paperclip::Attachment
@@ -51,23 +51,19 @@ def path
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, destination.path)
else
- FileUtils.cp(src.path(@style), dest.path)
+ FileUtils.cp(src.path(@style), destination.path)
end
- dest
+ destination
end
end
end
@@ -1,5 +1,5 @@
module Paperclip
- class FileAdapter
+ class FileAdapter < AbstractAdapter
def initialize(target)
@target = target
@tempfile = copy_to_tempfile(@target)
@@ -52,34 +52,6 @@ def eof?
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
@@ -1,5 +1,5 @@
module Paperclip
- class IdentityAdapter
+ class IdentityAdapter < AbstractAdapter
def new(adapter)
adapter
end
@@ -1,5 +1,5 @@
module Paperclip
- class NilAdapter
+ class NilAdapter < AbstractAdapter
def initialize(target)
end
@@ -1,5 +1,5 @@
module Paperclip
- class StringioAdapter
+ class StringioAdapter < AbstractAdapter
def initialize(target)
@target = target
@tempfile = copy_to_tempfile(@target)
@@ -47,15 +47,11 @@ def path
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)
+ destination.write(data)
end
- dest.rewind
- dest
+ destination.rewind
+ destination
end
end
@@ -1,5 +1,5 @@
module Paperclip
- class UploadedFileAdapter
+ class UploadedFileAdapter < AbstractAdapter
def initialize(target)
@target = target
@@ -46,17 +46,6 @@ def eof?
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

0 comments on commit b920ee4

Please sign in to comment.