From fa4207dfce84e937ae0a2233ef493fefc187f91d Mon Sep 17 00:00:00 2001 From: Daniel Munn Date: Sat, 9 Jun 2012 11:13:18 +0100 Subject: [PATCH] Updating Dav to list second level (folders / other), doesn't function in webdav client until 3rd level object functions --- lib/redmine_dmsf/webdav.rb | 2 + lib/redmine_dmsf/webdav/base_resource.rb | 32 +++++++------ lib/redmine_dmsf/webdav/index_resource.rb | 41 +++++++++++++++-- lib/redmine_dmsf/webdav/project_resource.rb | 50 ++++++++++++++++++--- lib/redmine_dmsf/webdav/resource_proxy.rb | 12 ++--- 5 files changed, 108 insertions(+), 29 deletions(-) diff --git a/lib/redmine_dmsf/webdav.rb b/lib/redmine_dmsf/webdav.rb index 26598dac..05984b5b 100644 --- a/lib/redmine_dmsf/webdav.rb +++ b/lib/redmine_dmsf/webdav.rb @@ -3,3 +3,5 @@ require 'redmine_dmsf/webdav/resource_proxy' require 'redmine_dmsf/webdav/base_resource' require 'redmine_dmsf/webdav/index_resource' +require 'redmine_dmsf/webdav/project_resource' +require 'redmine_dmsf/webdav/dmsf_resource' diff --git a/lib/redmine_dmsf/webdav/base_resource.rb b/lib/redmine_dmsf/webdav/base_resource.rb index bdfb3215..94a6748c 100644 --- a/lib/redmine_dmsf/webdav/base_resource.rb +++ b/lib/redmine_dmsf/webdav/base_resource.rb @@ -1,42 +1,34 @@ module RedmineDmsf module Webdav - class BaseResource < DAV4Rack::Resource DIR_FILE = "%s%s%s%s" def initialize(public_path, path, request, response, options) super(public_path, path, request, response, options) - pinfo = path.split('/').drop(1) - if pinfo.length > 0 - @project = Project.find(pinfo.first) - end end def accessor=(klass) @__proxy = klass end - def name - nil - end - def long_name nil end + def special_type + nil + end def html_display @response.body = "" - #@response.body << "

HELLO WORLD

" Confict unless collection? - entities = children.map{|child| DIR_FILE % [child.public_path.html_safe, child.long_name || child.name, "-", child.content_type, child.last_modified]} * "\n" - page = index_page % [ path.empty? ? "/" : path, path.empty? ? "/" : path , entities ] - page.each_line{|l| @response.body << l} + entities = children.map{|child| DIR_FILE % [child.public_path.html_safe, child.long_name || child.name, "-", child.special_type || child.content_type, child.last_modified]} * "\n" + @response.body << index_page % [ path.empty? ? "/" : path, path.empty? ? "/" : path , entities ] end - def child(name) + def child(name, options = nil) @__proxy.child(name) end @@ -68,6 +60,18 @@ table { width:100%%; } PAGE end + + protected + def Project + return @Project unless @Project.nil? + pinfo = @path.split('/').drop(1) + if pinfo.length > 0 + begin + @project = Project.find(pinfo.first) + rescue + end + end + end end end end diff --git a/lib/redmine_dmsf/webdav/index_resource.rb b/lib/redmine_dmsf/webdav/index_resource.rb index 2042cdd5..6e578b78 100644 --- a/lib/redmine_dmsf/webdav/index_resource.rb +++ b/lib/redmine_dmsf/webdav/index_resource.rb @@ -2,11 +2,18 @@ module RedmineDmsf module Webdav class IndexResource < BaseResource + def initialize(public_path, path, request, response, options) + super(public_path, path, request, response, options) + end + + def children - projects = Project.visible.find(:all, :order => 'lft') - return nill if projects.nil? || projects.empty? - projects.delete_if { |node| node.module_enabled?('dmsf').nil? } - projects.map do |p| + if @Projects.nil? || @Projects.empty? + @Projects = Project.visible.find(:all, :order => 'lft') + @Projects.delete_if { |node| node.module_enabled?('dmsf').nil? } + end + return nil if @Projects.nil? || @Projects.empty? + @Projects.map do |p| child p.identifier end end @@ -15,15 +22,41 @@ module RedmineDmsf true end + def creation_date + Time.now + end + + def last_modified + Time.now + end + + def last_modified= + MethodNotAllowed + end + #Index resource ALWAYS exists def exist? true end + def etag + sprintf('%x-%x-%x', children.count, 4096, Time.now.to_i) + end + + def content_type + "text/html" + end + + def content_length + 4096 + end + def get(request, response) html_display + response['Content-Length'] = response.body.bytesize.to_s OK end + end end end diff --git a/lib/redmine_dmsf/webdav/project_resource.rb b/lib/redmine_dmsf/webdav/project_resource.rb index 582bdd69..440fc514 100644 --- a/lib/redmine_dmsf/webdav/project_resource.rb +++ b/lib/redmine_dmsf/webdav/project_resource.rb @@ -2,32 +2,70 @@ module RedmineDmsf module Webdav class ProjectResource < BaseResource + def initialize(public_path, path, request, response, options) + super(public_path, path, request, response, options) + end + + def children + #caching for repeat usage + return @children unless @children.nil? + DmsfFolder.project_root_folders(self.Project).map do |p| + child p.title, p + end + DmsfFile.project_root_files(self.Project).map do |p| + child p.display_name, p + end + end + + def name - @project.name + self.Project.name unless self.Project.nil? end def exist? - !@Project.nil? + !self.Project.nil? end def collection? exist? end + def creation_date + self.Project.created_on unless self.Project.nil? + end + def last_modified - @project.updated_on unless @project.nil? + self.Project.updated_on unless self.Project.nil? + end + + def etag + sprintf('%x-%x-%x', 0, 4096, last_modified.to_i) end def name - @project.identifier unless @project.nil? + self.Project.identifier unless self.Project.nil? end def long_name - @project.name unless @project.nil? + self.Project.name unless self.Project.nil? end def content_type - "Project" #l(:field_project) + "inode/directory" #l(:field_project) + end + + def special_type + "Project" + end + + def content_length + 4096 + end + + def get(request, response) + html_display + response['Content-Length'] = response.body.bytesize.to_s + OK end end diff --git a/lib/redmine_dmsf/webdav/resource_proxy.rb b/lib/redmine_dmsf/webdav/resource_proxy.rb index 39bdda50..739a431a 100644 --- a/lib/redmine_dmsf/webdav/resource_proxy.rb +++ b/lib/redmine_dmsf/webdav/resource_proxy.rb @@ -1,11 +1,7 @@ -require 'webrick/httputils' - module RedmineDmsf module Webdav class ResourceProxy < DAV4Rack::Resource - include WEBrick::HTTPUtils - def initialize(public_path, path, request, response, options) super(public_path, path, request, response, options) pinfo = path.split('/').drop(1) @@ -14,6 +10,7 @@ module RedmineDmsf elsif (pinfo.length == 1) #This is first level, and as such, project path @resource_c = ProjectResource.new(public_path, path, request, response, options) else #We made it all the way to DMSF Data + @resource_c = DmsfResource.new(public_path, path, request, response, options) end @resource_c.accessor= self unless @resource_c.nil? @@ -21,7 +18,8 @@ module RedmineDmsf def authenticate(username, password) User.current = User.try_to_login(username, password) || nil - !User.current.nil? + return !User.current.anonymous? unless User.current.nil? + return false end def children @@ -71,6 +69,10 @@ module RedmineDmsf def long_name @resource_c.long_name end + + def special_type + @resource_c.special_type + end end end end