Class: Bcms::WebDAV::Resource
- Inherits:
-
DAV4Rack::Resource
- Object
- DAV4Rack::Resource
- Bcms::WebDAV::Resource
- Includes:
- DAV4Rack::HTTPStatus
- Defined in:
- lib/bcms_webdav/resource.rb
Overview
A virtual resource representing a CMS
* Section
* File
* Image
Class Method Summary collapse
-
.normalize_path(webdav_path) ⇒ Object
Converts WebDAV paths into CMS paths.
Instance Method Summary collapse
- #authenticate(username, password) ⇒ Object
- #children ⇒ Object
- #collection? ⇒ Boolean
- #content_length ⇒ Object
- #content_type ⇒ Object
- #creation_date ⇒ Object
- #etag ⇒ Object
-
#exist? ⇒ Boolean
This should always be called by DAV4Rack controller before any other primary operation (get, put) on a resource.
- #find_section_for(path) ⇒ Object
- #get(request, response) ⇒ Object
- #have_file ⇒ Object
- #have_page ⇒ Object
- #have_section ⇒ Object
- #last_modified ⇒ Object
-
#put(request, response) ⇒ Object
Handle uploading file.
Class Method Details
.normalize_path(webdav_path) ⇒ Object
Converts WebDAV paths into CMS paths. Both have slightly different rules.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/bcms_webdav/resource.rb', line 13 def self.normalize_path(webdav_path) path = webdav_path if path.end_with?("/") path.gsub!(/\/$/, '') end unless path.start_with?("/") path = path.insert(0, "/") end path = "/" if path == '' path end |
Instance Method Details
#authenticate(username, password) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bcms_webdav/resource.rb', line 27 def authenticate(username, password) log "Authenticating user '#{username}'" user = User.authenticate(username, password) unless user Rails.logger.error "Failed authentication attempt by user '#{username}'" return false end user.able_to?(:administrate) end |
#children ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/bcms_webdav/resource.rb', line 77 def children if exist? child_nodes = @section.child_nodes child_resources = [] child_nodes.each do |node| child_resources << child_node(node) end return child_resources else [] end end |
#collection? ⇒ Boolean
98 99 100 |
# File 'lib/bcms_webdav/resource.rb', line 98 def collection? have_section ? true : false end |
#content_length ⇒ Object
110 111 112 |
# File 'lib/bcms_webdav/resource.rb', line 110 def content_length have_file ? @resource.file_size : 0 end |
#content_type ⇒ Object
106 107 108 |
# File 'lib/bcms_webdav/resource.rb', line 106 def content_type have_file ? @resource.file_type : "text/html" end |
#creation_date ⇒ Object
90 91 92 |
# File 'lib/bcms_webdav/resource.rb', line 90 def creation_date @resource.created_at end |
#etag ⇒ Object
102 103 104 |
# File 'lib/bcms_webdav/resource.rb', line 102 def etag sprintf('%x-%x-%x', @resource.id, creation_date.to_i, last_modified.to_i) if exist? end |
#exist? ⇒ Boolean
This should always be called by DAV4Rack controller before any other primary operation (get, put) on a resource.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/bcms_webdav/resource.rb', line 53 def exist? path_to_find = Resource.normalize_path(path) @section = Section.with_path(path_to_find).first if have_section log_exists('section', path_to_find) @resource = @section if have_section end @page = Page.with_path(path_to_find).first if have_page log_exists('page', path_to_find) @resource = @page end @file = Attachment.find_by_file_path(path) if have_file log_exists('file', path_to_find) @resource = @file end have_section || have_page || have_file end |
#find_section_for(path) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/bcms_webdav/resource.rb', line 144 def find_section_for(path) log "Looking up section for path '#{path}" path_obj = Path.new(path) section_path = path_obj.path_without_filename path_to_find = Resource.normalize_path(section_path) log "Section.path = #{path_to_find}" Section.with_path(path_to_find).first end |
#get(request, response) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/bcms_webdav/resource.rb', line 114 def get(request, response) log "GET request for #{request.path}" if have_file file_location = @resource.full_file_location log "For attachment '#{@resource}' file location is '#{file_location}" file = Bcms::WebDAV::File.new(file_location) log "Sending file '#{file.path}'" response.body = file end end |
#have_file ⇒ Object
46 47 48 |
# File 'lib/bcms_webdav/resource.rb', line 46 def have_file @file != nil end |
#have_page ⇒ Object
42 43 44 |
# File 'lib/bcms_webdav/resource.rb', line 42 def have_page @page != nil end |
#have_section ⇒ Object
38 39 40 |
# File 'lib/bcms_webdav/resource.rb', line 38 def have_section @section != nil end |
#last_modified ⇒ Object
94 95 96 |
# File 'lib/bcms_webdav/resource.rb', line 94 def last_modified @resource.created_at if exist? end |
#put(request, response) ⇒ Object
Handle uploading file.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/bcms_webdav/resource.rb', line 126 def put(request, response) temp_file = request.body add_rails_like_methods(temp_file) section = find_section_for(path) file_block = FileBlock.new(:name=>path, :attachment_file=>request.body, :attachment_section => section, :attachment_file_path=>path, :publish_on_save=>true) unless file_block.save log "Couldn't save file." file_block.errors.each do |error| log error end return end OK end |