Class: DAV4Rack::FileResource
- Includes:
- WEBrick::HTTPUtils
- Defined in:
- lib/dav4rack/file_resource.rb
Constant Summary
Constants included from HTTPStatus
Instance Attribute Summary
Attributes inherited from Resource
#options, #path, #public_path, #request, #response, #user
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, overwrite = false) ⇒ 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(*args) ⇒ Object
HTTP MOVE request.
-
#post(request, response) ⇒ Object
HTTP POST request.
-
#put(request, response) ⇒ Object
HTTP PUT request.
-
#write(io) ⇒ Object
Write to this resource from given IO.
Methods inherited from Resource
#==, #child, #descendants, #display_name, #get_property, #initialize, #lock, #lock_check, method_missing, #method_missing, #name, #parent, #parent_exists?, #property_names, #remove_property, #resource_type, #set_property, #unlock
Constructor Details
This class inherits a constructor from DAV4Rack::Resource
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class DAV4Rack::Resource
Instance Method Details
#children ⇒ Object
If this is a collection, return the child resources.
10 11 12 13 14 |
# File 'lib/dav4rack/file_resource.rb', line 10 def children Dir[file_path + '/*'].map do |path| child File.basename(path) end end |
#collection? ⇒ Boolean
Is this resource a collection?
17 18 19 |
# File 'lib/dav4rack/file_resource.rb', line 17 def collection? File.directory?(file_path) end |
#content_length ⇒ Object
Return the size in bytes for this resource.
56 57 58 |
# File 'lib/dav4rack/file_resource.rb', line 56 def content_length stat.size end |
#content_type ⇒ Object
Return the mime type of this resource.
47 48 49 50 51 52 53 |
# File 'lib/dav4rack/file_resource.rb', line 47 def content_type if stat.directory? "text/html" else mime_type(file_path, DefaultMimeTypes) end end |
#copy(dest, overwrite = false) ⇒ Object
HTTP COPY request.
Copy this resource to given destination resource.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/dav4rack/file_resource.rb', line 108 def copy(dest, overwrite = false) if(dest.path == path) Conflict elsif(stat.directory?) dest.make_collection FileUtils.cp_r("#{file_path}/.", "#{dest.send(:file_path)}/") OK else exists = File.exists?(file_path) if(exists && !overwrite) PreconditionFailed else open(file_path, "rb") do |file| dest.write(file) end exists ? NoContent : Created end end end |
#creation_date ⇒ Object
Return the creation time.
27 28 29 |
# File 'lib/dav4rack/file_resource.rb', line 27 def creation_date stat.ctime end |
#delete ⇒ Object
HTTP DELETE request.
Delete this resource.
96 97 98 99 100 101 102 103 |
# File 'lib/dav4rack/file_resource.rb', line 96 def delete if stat.directory? FileUtils.rm_rf(file_path) else File.unlink(file_path) end NoContent end |
#etag ⇒ Object
Return an Etag, an unique hash value for this resource.
42 43 44 |
# File 'lib/dav4rack/file_resource.rb', line 42 def etag sprintf('%x-%x-%x', stat.ino, stat.size, stat.mtime.to_i) end |
#exist? ⇒ Boolean
Does this recource exist?
22 23 24 |
# File 'lib/dav4rack/file_resource.rb', line 22 def exist? File.exist?(file_path) end |
#get(request, response) ⇒ Object
HTTP GET request.
Write the content of the resource to the response.body.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dav4rack/file_resource.rb', line 63 def get(request, response) raise NotFound unless exist? if stat.directory? response.body = "" Rack::Directory.new(root).call(request.env)[2].each do |line| response.body << line end response['Content-Length'] = response.body.size.to_s else file = Rack::File.new(root) response.body = file end OK end |
#last_modified ⇒ Object
Return the time of last modification.
32 33 34 |
# File 'lib/dav4rack/file_resource.rb', line 32 def last_modified stat.mtime end |
#last_modified=(time) ⇒ Object
Set the time of last modification.
37 38 39 |
# File 'lib/dav4rack/file_resource.rb', line 37 def last_modified=(time) File.utime(Time.now, time, file_path) end |
#make_collection ⇒ Object
HTTP MKCOL request.
Create this resource as collection.
140 141 142 143 |
# File 'lib/dav4rack/file_resource.rb', line 140 def make_collection Dir.mkdir(file_path) Created end |
#move(*args) ⇒ Object
HTTP MOVE request.
Move this resource to given destination resource.
131 132 133 134 135 |
# File 'lib/dav4rack/file_resource.rb', line 131 def move(*args) copy(*args) delete OK end |
#post(request, response) ⇒ Object
HTTP POST request.
Usually forbidden.
89 90 91 |
# File 'lib/dav4rack/file_resource.rb', line 89 def post(request, response) raise HTTPStatus::Forbidden end |
#put(request, response) ⇒ Object
HTTP PUT request.
Save the content of the request.body.
81 82 83 84 |
# File 'lib/dav4rack/file_resource.rb', line 81 def put(request, response) write(request.body) Created end |
#write(io) ⇒ Object
Write to this resource from given IO.
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/dav4rack/file_resource.rb', line 146 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 |