lib/paperclip/io_adapters/abstract_adapter.rb in kt-paperclip-5.4.0 vs lib/paperclip/io_adapters/abstract_adapter.rb in kt-paperclip-6.2.0
- old
+ new
@@ -1,13 +1,13 @@
-require 'active_support/core_ext/module/delegation'
+require "active_support/core_ext/module/delegation"
module Paperclip
class AbstractAdapter
- OS_RESTRICTED_CHARACTERS = %r{[/:]}
+ OS_RESTRICTED_CHARACTERS = %r{[/:]}.freeze
- attr_reader :content_type, :original_filename, :size
- delegate :binmode, :binmode?, :close, :close!, :closed?, :eof?, :path, :readbyte, :rewind, :unlink, :to => :@tempfile
+ attr_reader :content_type, :original_filename, :size, :tempfile
+ delegate :binmode, :binmode?, :close, :close!, :closed?, :eof?, :path, :readbyte, :rewind, :unlink, to: :@tempfile
alias :length :size
def initialize(target, options = {})
@target = target
@options = options
@@ -27,15 +27,16 @@
def read(length = nil, buffer = nil)
@tempfile.read(length, buffer)
end
def inspect
- "#{self.class}: #{self.original_filename}"
+ "#{self.class}: #{original_filename}"
end
def original_filename=(new_filename)
return unless new_filename
+
@original_filename = new_filename.gsub(OS_RESTRICTED_CHARACTERS, "_")
end
def nil?
false
@@ -55,18 +56,19 @@
link_or_copy_file(src.path, destination.path)
destination
end
def link_or_copy_file(src, dest)
- Paperclip.log("Trying to link #{src} to #{dest}")
- FileUtils.ln(src, dest, force: true) # overwrite existing
- @destination.close
- @destination.open.binmode
- rescue Errno::EXDEV, Errno::EPERM, Errno::ENOENT, Errno::EEXIST => e
- Paperclip.log(
- "Link failed with #{e.message}; copying link #{src} to #{dest}"
- )
- FileUtils.cp(src, dest)
+ begin
+ Paperclip.log("Trying to link #{src} to #{dest}")
+ FileUtils.ln(src, dest, force: true) # overwrite existing
+ rescue Errno::EXDEV, Errno::EPERM, Errno::ENOENT, Errno::EEXIST => e
+ Paperclip.log(
+ "Link failed with #{e.message}; copying link #{src} to #{dest}"
+ )
+ FileUtils.cp(src, dest)
+ end
+
@destination.close
@destination.open.binmode
end
end
end