Commit b920ee4d by Jon Yurek

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

parent e54c8363
......@@ -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'
......
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
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, destination.path)
else
FileUtils.cp(src.path(@style), dest.path)
FileUtils.cp(src.path(@style), destination.path)
end
dest
destination
end
end
end
......
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
......
module Paperclip
class IdentityAdapter
class IdentityAdapter < AbstractAdapter
def new(adapter)
adapter
end
......
module Paperclip
class NilAdapter
class NilAdapter < AbstractAdapter
def initialize(target)
end
......
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)
destination.write(data)
end
dest.rewind
dest
destination.rewind
destination
end
end
......
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
......
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