Class: RackDAV::FileResource
- Includes:
- WEBrick::HTTPUtils
- Defined in:
- lib/rack_dav/file_resource.rb
Instance Attribute Summary
Attributes inherited from Resource
Instance Method Summary collapse
-
#children ⇒ Object
If this is a collection, return the child resources.
-
#collection? ⇒ Boolean
Is this resource a collection?.
-
#content_length ⇒ Object
Return the size in bytes for this resource.
-
#content_type ⇒ Object
Return the mime type of this resource.
-
#copy(dest) ⇒ Object
HTTP COPY request.
-
#creation_date ⇒ Object
Return the creation time.
-
#delete ⇒ Object
HTTP DELETE request.
-
#etag ⇒ Object
Return an Etag, an unique hash value for this resource.
-
#exist? ⇒ Boolean
Does this recource exist?.
-
#get(request, response) ⇒ Object
HTTP GET request.
-
#last_modified ⇒ Object
Return the time of last modification.
-
#last_modified=(time) ⇒ Object
Set the time of last modification.
-
#make_collection ⇒ Object
HTTP MKCOL request.
-
#move(dest) ⇒ Object
HTTP MOVE request.
-
#post(request, response) ⇒ Object
HTTP POST request.
-
#put(request, response) ⇒ Object
HTTP PUT request.
-
#resource_type ⇒ Object
Return the resource type.
-
#write(io) ⇒ Object
Write to this resource from given IO.
Methods inherited from Resource
#==, #child, #descendants, #display_name, #get_property, #initialize, #lockable?, #name, #parent, #property_names, #remove_property, #set_property
Constructor Details
This class inherits a constructor from RackDAV::Resource
Instance Method Details
#children ⇒ Object
If this is a collection, return the child resources.
9 10 11 12 13 |
# File 'lib/rack_dav/file_resource.rb', line 9 def children Dir[file_path + '/*'].map do |path| child File.basename(path) end end |
#collection? ⇒ Boolean
Is this resource a collection?
16 17 18 |
# File 'lib/rack_dav/file_resource.rb', line 16 def collection? File.directory?(file_path) end |
#content_length ⇒ Object
Return the size in bytes for this resource.
64 65 66 |
# File 'lib/rack_dav/file_resource.rb', line 64 def content_length stat.size end |
#content_type ⇒ Object
Return the mime type of this resource.
55 56 57 58 59 60 61 |
# File 'lib/rack_dav/file_resource.rb', line 55 def content_type if stat.directory? "text/html" else mime_type(file_path, DefaultMimeTypes) end end |
#copy(dest) ⇒ Object
HTTP COPY request.
Copy this resource to given destination resource.
115 116 117 118 119 120 121 122 123 |
# File 'lib/rack_dav/file_resource.rb', line 115 def copy(dest) if stat.directory? dest.make_collection else open(file_path, "rb") do |file| dest.write(file) end end end |
#creation_date ⇒ Object
Return the creation time.
26 27 28 |
# File 'lib/rack_dav/file_resource.rb', line 26 def creation_date stat.ctime end |
#delete ⇒ Object
HTTP DELETE request.
Delete this resource.
104 105 106 107 108 109 110 |
# File 'lib/rack_dav/file_resource.rb', line 104 def delete if stat.directory? Dir.rmdir(file_path) else File.unlink(file_path) end end |
#etag ⇒ Object
Return an Etag, an unique hash value for this resource.
41 42 43 |
# File 'lib/rack_dav/file_resource.rb', line 41 def etag sprintf('%x-%x-%x', stat.ino, stat.size, stat.mtime.to_i) end |
#exist? ⇒ Boolean
Does this recource exist?
21 22 23 |
# File 'lib/rack_dav/file_resource.rb', line 21 def exist? File.exist?(file_path) end |
#get(request, response) ⇒ Object
HTTP GET request.
Write the content of the resource to the response.body.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rack_dav/file_resource.rb', line 71 def get(request, response) 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 ⇒ Object
Return the time of last modification.
31 32 33 |
# File 'lib/rack_dav/file_resource.rb', line 31 def last_modified stat.mtime end |
#last_modified=(time) ⇒ Object
Set the time of last modification.
36 37 38 |
# File 'lib/rack_dav/file_resource.rb', line 36 def last_modified=(time) File.utime(Time.now, time, file_path) end |
#make_collection ⇒ Object
HTTP MKCOL request.
Create this resource as collection.
136 137 138 |
# File 'lib/rack_dav/file_resource.rb', line 136 def make_collection Dir.mkdir(file_path) end |
#move(dest) ⇒ Object
HTTP MOVE request.
Move this resource to given destination resource.
128 129 130 131 |
# File 'lib/rack_dav/file_resource.rb', line 128 def move(dest) copy(dest) delete end |
#post(request, response) ⇒ Object
HTTP POST request.
Usually forbidden.
97 98 99 |
# File 'lib/rack_dav/file_resource.rb', line 97 def post(request, response) raise HTTPStatus::Forbidden end |
#put(request, response) ⇒ Object
HTTP PUT request.
Save the content of the request.body.
86 87 88 89 90 91 92 |
# File 'lib/rack_dav/file_resource.rb', line 86 def put(request, response) 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 ⇒ Object
Return the resource type.
If this is a collection, return a collection element
48 49 50 51 52 |
# File 'lib/rack_dav/file_resource.rb', line 48 def resource_type if collection? Nokogiri::XML::fragment('<D:collection xmlns:D="DAV:"/>').children.first end end |
#write(io) ⇒ Object
Write to this resource from given IO.
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/rack_dav/file_resource.rb', line 141 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 |