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, #propstat_relative_path, #public_path, #request, #response, #root_xml_attributes, #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) ⇒ 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.
-
#get_property(name) ⇒ Object
- name
-
String - Property name Returns the value of the given property.
-
#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.
-
#set_property(name, value) ⇒ Object
- name
- String - Property name value
-
New value Set the property to the given value.
-
#write(io) ⇒ Object
Write to this resource from given IO.
Methods inherited from Resource
#==, #allows_redirect?, #child, #descendants, #display_name, #index_page, #initialize, #is_ms_client?, #lock, #lock_check, method_missing, #method_missing, #name, #parent, #parent_exists?, #property_names, #remove_property, #resource_type, #unlock, #use_compat_mkcol_response?, #use_ms_compat_creationdate?
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) ⇒ Object
HTTP COPY request.
Copy this resource to given destination resource. Copy this resource to given destination resource.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/dav4rack/file_resource.rb', line 109 def copy(dest, overwrite) if(collection?) if(dest.exist?) if(dest.collection? && overwrite) FileUtils.cp_r(file_path, dest.send(:file_path)) Created else if(overwrite) FileUtils.rm(dest.send(:file_path)) FileUtils.cp_r(file_path, dest.send(:file_path)) NoContent else PreconditionFailed end end else FileUtils.cp_r(file_path, dest.send(:file_path)) Created end else if(dest.exist? && !overwrite) PreconditionFailed else if(File.directory?(File.dirname(dest.send(:file_path)))) new = !dest.exist? if(dest.collection? && dest.exist?) FileUtils.rm_rf(dest.send(:file_path)) end FileUtils.cp(file_path, dest.send(:file_path).sub(/\/$/, '')) new ? Created : NoContent else Conflict end 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.bytesize.to_s else file = Rack::File.new(root) response.body = file end OK end |
#get_property(name) ⇒ Object
- name
-
String - Property name
Returns the value of the given property
192 193 194 |
# File 'lib/dav4rack/file_resource.rb', line 192 def get_property(name) super || custom_props(name) 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.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/dav4rack/file_resource.rb', line 158 def make_collection if(request.body.read.to_s == '') if(File.directory?(file_path)) MethodNotAllowed else if(File.directory?(File.dirname(file_path))) Dir.mkdir(file_path) Created else Conflict end end else UnsupportedMediaType end end |
#move(*args) ⇒ Object
HTTP MOVE request.
Move this resource to given destination resource.
149 150 151 152 153 |
# File 'lib/dav4rack/file_resource.rb', line 149 def move(*args) result = copy(*args) delete if [Created, NoContent].include?(result) result 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 |
#set_property(name, value) ⇒ Object
- name
-
String - Property name
- value
-
New value
Set the property to the given value
199 200 201 |
# File 'lib/dav4rack/file_resource.rb', line 199 def set_property(name, value) super || set_custom_props(name,value) end |
#write(io) ⇒ Object
Write to this resource from given IO.
176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/dav4rack/file_resource.rb', line 176 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 |