lib/tori/backend/filesystem.rb in tori-0.6.5 vs lib/tori/backend/filesystem.rb in tori-0.6.6
- old
+ new
@@ -1,27 +1,35 @@
module Tori
module Backend
class FileSystem
+ ResourceError = Class.new(StandardError)
+
attr_accessor :root
def initialize(root)
@root = root
FileUtils.mkdir_p @root.to_s
end
def write(filename, resource, opts = nil)
pathname = path(filename)
FileUtils.mkdir_p pathname.dirname
+ if resource.nil? && opts && opts[:body]
+ resource = opts[:body]
+ end
+
case resource
when String
::File.open(pathname, 'wb'){ |f| f.write resource }
when Pathname
# see also https://bugs.ruby-lang.org/issues/11199
::File.open(resource) { |src|
::File.open(pathname, 'wb'){ |dst|
::IO.copy_stream src, dst
}
}
+ when NilClass
+ raise ResourceError, "null resource"
else
::File.open(pathname, 'wb') do |dst|
::IO.copy_stream resource, dst
end
end