class RackDAV::FileResource

Public Instance Methods

children() click to toggle source

If this is a collection, return the child resources.

# File lib/rack_dav/file_resource.rb, line 10
def children
  Dir[file_path + '/*'].map do |path|
    child File.basename(path)
  end
end
collection?() click to toggle source

Is this resource a collection?

# File lib/rack_dav/file_resource.rb, line 17
def collection?
  File.directory?(file_path)
end
content_length() click to toggle source

Return the size in bytes for this resource.

# File lib/rack_dav/file_resource.rb, line 65
def content_length
  stat.size
end
content_type() click to toggle source

Return the mime type of this resource.

# File lib/rack_dav/file_resource.rb, line 56
def content_type
  if stat.directory?
    "text/html"
  else
    mime_type(file_path, DefaultMimeTypes)
  end
end
copy(dest) click to toggle source

HTTP COPY request.

Copy this resource to given destination resource.

# File lib/rack_dav/file_resource.rb, line 120
def copy(dest)
  if stat.directory?
    dest.make_collection
  else
    open(file_path, "rb") do |file|
      dest.write(file)
    end

    list_custom_properties.each do |prop|
      dest.set_custom_property(prop, get_custom_property(prop))
    end
  end
end
creation_date() click to toggle source

Return the creation time.

# File lib/rack_dav/file_resource.rb, line 27
def creation_date
  stat.ctime
end
delete() click to toggle source

HTTP DELETE request.

Delete this resource.

# File lib/rack_dav/file_resource.rb, line 109
def delete
  if stat.directory?
    Dir.rmdir(file_path)
  else
    File.unlink(file_path)
  end
end
etag() click to toggle source

Return an Etag, an unique hash value for this resource.

# File lib/rack_dav/file_resource.rb, line 42
def etag
  sprintf('%x-%x-%x', stat.ino, stat.size, stat.mtime.to_i)
end
exist?() click to toggle source

Does this recource exist?

# File lib/rack_dav/file_resource.rb, line 22
def exist?
  File.exist?(file_path)
end
get() click to toggle source

HTTP GET request.

Write the content of the resource to the response.body.

# File lib/rack_dav/file_resource.rb, line 76
def get
  if stat.directory?
    content = ""
    Rack::Directory.new(root).call(@request.env)[2].each { |line| content << line }
    @response.body = [content]
    @response['Content-Length'] = (content.respond_to?(:bytesize) ? content.bytesize : content.size).to_s
  else
    file = File.open(file_path)
    @response.body = file
  end
end
last_modified() click to toggle source

Return the time of last modification.

# File lib/rack_dav/file_resource.rb, line 32
def last_modified
  stat.mtime
end
last_modified=(time) click to toggle source

Set the time of last modification.

# File lib/rack_dav/file_resource.rb, line 37
def last_modified=(time)
  File.utime(Time.now, time, file_path)
end
list_custom_properties() click to toggle source
# File lib/rack_dav/file_resource.rb, line 69
def list_custom_properties
  []
end
make_collection() click to toggle source

HTTP MKCOL request.

Create this resource as collection.

# File lib/rack_dav/file_resource.rb, line 145
def make_collection
  Dir.mkdir(file_path)
end
move(dest) click to toggle source

HTTP MOVE request.

Move this resource to given destination resource.

# File lib/rack_dav/file_resource.rb, line 137
def move(dest)
  copy(dest)
  delete
end
post() click to toggle source

HTTP POST request.

Usually forbidden.

# File lib/rack_dav/file_resource.rb, line 102
def post
  raise HTTPStatus::Forbidden
end
put() click to toggle source

HTTP PUT request.

Save the content of the request.body.

# File lib/rack_dav/file_resource.rb, line 91
def put
  if @request.env['HTTP_CONTENT_MD5']
    content_md5_pass?(@request.env) or raise HTTPStatus::BadRequest.new('Content-MD5 mismatch')
  end

  write(@request.body)
end
resource_type() click to toggle source

Return the resource type.

If this is a collection, return a collection element

# File lib/rack_dav/file_resource.rb, line 49
def resource_type
  if collection?
    Nokogiri::XML::fragment('<D:collection xmlns:D="DAV:"/>').children.first
  end
end
write(io) click to toggle source

Write to this resource from given IO.

# File lib/rack_dav/file_resource.rb, line 150
def write(io)
  tempfile = "#{file_path}.#{Process.pid}.#{object_id}"

  open(tempfile, "wb") do |file|
    while part = io.read(8192)
      file << part
    end
  end

  File.rename(tempfile, file_path)
ensure
  File.unlink(tempfile) rescue nil
end

Private Instance Methods

content_md5_pass?(env) click to toggle source
# File lib/rack_dav/file_resource.rb, line 179
def content_md5_pass?(env)
  expected = env['HTTP_CONTENT_MD5'] or return true

  body   = env['rack.input'].dup
  digest = Digest::MD5.new.digest(body.read)
  actual = [ digest ].pack('m').strip

  body.rewind

  expected == actual
end
file_path() click to toggle source
# File lib/rack_dav/file_resource.rb, line 171
def file_path
  root + '/' + path
end
root() click to toggle source
# File lib/rack_dav/file_resource.rb, line 167
def root
  @options[:root]
end
stat() click to toggle source
# File lib/rack_dav/file_resource.rb, line 175
def stat
  @stat ||= File.stat(file_path)
end