Commit 83d8ec5a by Jon Yurek

Translate bad filename characters

parent 86dc49bd
...@@ -2,6 +2,8 @@ require 'active_support/core_ext/module/delegation' ...@@ -2,6 +2,8 @@ require 'active_support/core_ext/module/delegation'
module Paperclip module Paperclip
class AbstractAdapter class AbstractAdapter
OS_RESTRICTED_CHARACTERS = %r{[/:]}
attr_reader :content_type, :original_filename, :size attr_reader :content_type, :original_filename, :size
delegate :close, :closed?, :eof?, :path, :rewind, :unlink, :to => :@tempfile delegate :close, :closed?, :eof?, :path, :rewind, :unlink, :to => :@tempfile
...@@ -17,6 +19,10 @@ module Paperclip ...@@ -17,6 +19,10 @@ module Paperclip
"#{self.class}: #{self.original_filename}" "#{self.class}: #{self.original_filename}"
end end
def original_filename=(new_filename)
@original_filename = new_filename.gsub(OS_RESTRICTED_CHARACTERS, "_")
end
private private
def destination def destination
......
...@@ -6,7 +6,8 @@ module Paperclip ...@@ -6,7 +6,8 @@ module Paperclip
@tempfile = copy_to_tempfile(@target) @tempfile = copy_to_tempfile(@target)
end end
attr_writer :original_filename, :content_type attr_writer :content_type
private private
def cache_current_values def cache_current_values
......
...@@ -9,7 +9,8 @@ module Paperclip ...@@ -9,7 +9,8 @@ module Paperclip
@tempfile = copy_to_tempfile(@content) @tempfile = copy_to_tempfile(@content)
end end
attr_writer :original_filename, :content_type attr_writer :content_type
private private
def download_content def download_content
......
...@@ -14,6 +14,7 @@ require 'active_support/core_ext' ...@@ -14,6 +14,7 @@ require 'active_support/core_ext'
require 'mime/types' require 'mime/types'
require 'pathname' require 'pathname'
require 'ostruct' require 'ostruct'
require 'pry'
puts "Testing against version #{ActiveRecord::VERSION::STRING}" puts "Testing against version #{ActiveRecord::VERSION::STRING}"
......
...@@ -2,7 +2,7 @@ require './test/helper' ...@@ -2,7 +2,7 @@ require './test/helper'
class AbstractAdapterTest < Test::Unit::TestCase class AbstractAdapterTest < Test::Unit::TestCase
class TestAdapter < Paperclip::AbstractAdapter class TestAdapter < Paperclip::AbstractAdapter
attr_accessor :original_file_name, :tempfile attr_accessor :tempfile
def content_type def content_type
Paperclip::ContentTypeDetector.new(path).detect Paperclip::ContentTypeDetector.new(path).detect
...@@ -40,4 +40,11 @@ class AbstractAdapterTest < Test::Unit::TestCase ...@@ -40,4 +40,11 @@ class AbstractAdapterTest < Test::Unit::TestCase
end end
end end
end end
should 'get rid of slashes and colons in filenames' do
@adapter = TestAdapter.new
@adapter.original_filename = "awesome/file:name.png"
assert_equal "awesome_file_name.png", @adapter.original_filename
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