Commit b920ee4d by Jon Yurek

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

parent e54c8363
...@@ -214,6 +214,7 @@ end ...@@ -214,6 +214,7 @@ end
# This stuff needs to be run after Paperclip is defined. # This stuff needs to be run after Paperclip is defined.
require 'paperclip/io_adapters/registry' require 'paperclip/io_adapters/registry'
require 'paperclip/io_adapters/abstract_adapter'
require 'paperclip/io_adapters/identity_adapter' require 'paperclip/io_adapters/identity_adapter'
require 'paperclip/io_adapters/file_adapter' require 'paperclip/io_adapters/file_adapter'
require 'paperclip/io_adapters/stringio_adapter' require 'paperclip/io_adapters/stringio_adapter'
......
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
module Paperclip module Paperclip
class AttachmentAdapter class AttachmentAdapter < AbstractAdapter
def initialize(target) def initialize(target)
@target, @style = case target @target, @style = case target
when Paperclip::Attachment when Paperclip::Attachment
...@@ -51,23 +51,19 @@ module Paperclip ...@@ -51,23 +51,19 @@ module Paperclip
private private
def cache_current_values def cache_current_values
@tempfile = copy_to_tempfile(@target)
@original_filename = @target.original_filename @original_filename = @target.original_filename
@content_type = @target.content_type @content_type = @target.content_type
@tempfile = copy_to_tempfile(@target)
@size = @tempfile.size || @target.size @size = @tempfile.size || @target.size
end end
def copy_to_tempfile(src) 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 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 else
FileUtils.cp(src.path(@style), dest.path) FileUtils.cp(src.path(@style), destination.path)
end end
dest destination
end end
end end
end end
......
module Paperclip module Paperclip
class FileAdapter class FileAdapter < AbstractAdapter
def initialize(target) def initialize(target)
@target = target @target = target
@tempfile = copy_to_tempfile(@target) @tempfile = copy_to_tempfile(@target)
...@@ -52,34 +52,6 @@ module Paperclip ...@@ -52,34 +52,6 @@ module Paperclip
def path def path
@tempfile.path @tempfile.path
end 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
end end
......
module Paperclip module Paperclip
class IdentityAdapter class IdentityAdapter < AbstractAdapter
def new(adapter) def new(adapter)
adapter adapter
end end
......
module Paperclip module Paperclip
class NilAdapter class NilAdapter < AbstractAdapter
def initialize(target) def initialize(target)
end end
......
module Paperclip module Paperclip
class StringioAdapter class StringioAdapter < AbstractAdapter
def initialize(target) def initialize(target)
@target = target @target = target
@tempfile = copy_to_tempfile(@target) @tempfile = copy_to_tempfile(@target)
...@@ -47,15 +47,11 @@ module Paperclip ...@@ -47,15 +47,11 @@ module Paperclip
private private
def copy_to_tempfile(src) 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) while data = src.read(16*1024)
dest.write(data) destination.write(data)
end end
dest.rewind destination.rewind
dest destination
end end
end end
......
module Paperclip module Paperclip
class UploadedFileAdapter class UploadedFileAdapter < AbstractAdapter
def initialize(target) def initialize(target)
@target = target @target = target
...@@ -46,17 +46,6 @@ module Paperclip ...@@ -46,17 +46,6 @@ module Paperclip
def path def path
@tempfile.path @tempfile.path
end 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
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment