def save(path = nil, options = {})
opts = trait[:options].merge(options)
unless path
unless opts[:default_upload_dir]
raise StandardError.new('Unable to save file, no dirname given')
end
unless @filename
raise StandardError.new('Unable to save file, no filename given')
end
dn = opts[:default_upload_dir]
if dn.respond_to?(:call)
dn = dn.call
end
path = File.join(dn, @filename)
end
path = File.expand_path(path)
if File.exists?(path) and !opts[:allow_overwrite]
raise StandardError.new('Unable to overwrite existing file')
end
unless File.readable?(@tempfile.path)
raise StandardError.new('Unable to read temporary file')
end
unless (File.exists?(path) and File.writable?(path)) \
or (File.exists?(File.dirname(path)) \
and File.writable?(File.dirname(path)))
raise StandardError.new(
"Unable to save file to #{path}. Path is not writable"
)
end
if IO.respond_to?(:copy_stream)
IO.copy_stream(@tempfile, path)
else
require 'fileutils'
File.open(@tempfile.path, 'rb') do |src|
File.open(path, 'wb') do |dest|
FileUtils.copy_stream(src, dest)
end
end
end
@realfile = File.new(path)
unlink_tempfile if opts[:unlink_tempfile]
end