Updating Dav to list second level (folders / other), doesn't function in webdav client until 3rd level object functions

This commit is contained in:
Daniel Munn 2012-06-09 11:13:18 +01:00
parent bf6c4e8f53
commit fa4207dfce
5 changed files with 108 additions and 29 deletions

View File

@ -3,3 +3,5 @@ require 'redmine_dmsf/webdav/resource_proxy'
require 'redmine_dmsf/webdav/base_resource' require 'redmine_dmsf/webdav/base_resource'
require 'redmine_dmsf/webdav/index_resource' require 'redmine_dmsf/webdav/index_resource'
require 'redmine_dmsf/webdav/project_resource'
require 'redmine_dmsf/webdav/dmsf_resource'

View File

@ -1,42 +1,34 @@
module RedmineDmsf module RedmineDmsf
module Webdav module Webdav
class BaseResource < DAV4Rack::Resource class BaseResource < DAV4Rack::Resource
DIR_FILE = "<tr><td class=\"name\"><a href=\"%s\">%s</a></td><td class=\"size\">%s</td><td class=\"type\">%s</td><td class=\"mtime\">%s</td></tr>" DIR_FILE = "<tr><td class=\"name\"><a href=\"%s\">%s</a></td><td class=\"size\">%s</td><td class=\"type\">%s</td><td class=\"mtime\">%s</td></tr>"
def initialize(public_path, path, request, response, options) def initialize(public_path, path, request, response, options)
super(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 end
def accessor=(klass) def accessor=(klass)
@__proxy = klass @__proxy = klass
end end
def name
nil
end
def long_name def long_name
nil nil
end end
def special_type
nil
end
def html_display def html_display
@response.body = "" @response.body = ""
#@response.body << "<h1>HELLO WORLD</H1>"
Confict unless collection? 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" 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"
page = index_page % [ path.empty? ? "/" : path, path.empty? ? "/" : path , entities ] @response.body << index_page % [ path.empty? ? "/" : path, path.empty? ? "/" : path , entities ]
page.each_line{|l| @response.body << l}
end end
def child(name) def child(name, options = nil)
@__proxy.child(name) @__proxy.child(name)
end end
@ -68,6 +60,18 @@ table { width:100%%; }
</body></html> </body></html>
PAGE PAGE
end 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 end
end end

View File

@ -2,11 +2,18 @@ module RedmineDmsf
module Webdav module Webdav
class IndexResource < BaseResource class IndexResource < BaseResource
def initialize(public_path, path, request, response, options)
super(public_path, path, request, response, options)
end
def children def children
projects = Project.visible.find(:all, :order => 'lft') if @Projects.nil? || @Projects.empty?
return nill if projects.nil? || projects.empty? @Projects = Project.visible.find(:all, :order => 'lft')
projects.delete_if { |node| node.module_enabled?('dmsf').nil? } @Projects.delete_if { |node| node.module_enabled?('dmsf').nil? }
projects.map do |p| end
return nil if @Projects.nil? || @Projects.empty?
@Projects.map do |p|
child p.identifier child p.identifier
end end
end end
@ -15,15 +22,41 @@ module RedmineDmsf
true true
end end
def creation_date
Time.now
end
def last_modified
Time.now
end
def last_modified=
MethodNotAllowed
end
#Index resource ALWAYS exists #Index resource ALWAYS exists
def exist? def exist?
true true
end 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) def get(request, response)
html_display html_display
response['Content-Length'] = response.body.bytesize.to_s
OK OK
end end
end end
end end
end end

View File

@ -2,32 +2,70 @@ module RedmineDmsf
module Webdav module Webdav
class ProjectResource < BaseResource 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 def name
@project.name self.Project.name unless self.Project.nil?
end end
def exist? def exist?
!@Project.nil? !self.Project.nil?
end end
def collection? def collection?
exist? exist?
end end
def creation_date
self.Project.created_on unless self.Project.nil?
end
def last_modified 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 end
def name def name
@project.identifier unless @project.nil? self.Project.identifier unless self.Project.nil?
end end
def long_name def long_name
@project.name unless @project.nil? self.Project.name unless self.Project.nil?
end end
def content_type 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
end end

View File

@ -1,11 +1,7 @@
require 'webrick/httputils'
module RedmineDmsf module RedmineDmsf
module Webdav module Webdav
class ResourceProxy < DAV4Rack::Resource class ResourceProxy < DAV4Rack::Resource
include WEBrick::HTTPUtils
def initialize(public_path, path, request, response, options) def initialize(public_path, path, request, response, options)
super(public_path, path, request, response, options) super(public_path, path, request, response, options)
pinfo = path.split('/').drop(1) pinfo = path.split('/').drop(1)
@ -14,6 +10,7 @@ module RedmineDmsf
elsif (pinfo.length == 1) #This is first level, and as such, project path elsif (pinfo.length == 1) #This is first level, and as such, project path
@resource_c = ProjectResource.new(public_path, path, request, response, options) @resource_c = ProjectResource.new(public_path, path, request, response, options)
else #We made it all the way to DMSF Data else #We made it all the way to DMSF Data
@resource_c = DmsfResource.new(public_path, path, request, response, options)
end end
@resource_c.accessor= self unless @resource_c.nil? @resource_c.accessor= self unless @resource_c.nil?
@ -21,7 +18,8 @@ module RedmineDmsf
def authenticate(username, password) def authenticate(username, password)
User.current = User.try_to_login(username, password) || nil User.current = User.try_to_login(username, password) || nil
!User.current.nil? return !User.current.anonymous? unless User.current.nil?
return false
end end
def children def children
@ -71,6 +69,10 @@ module RedmineDmsf
def long_name def long_name
@resource_c.long_name @resource_c.long_name
end end
def special_type
@resource_c.special_type
end
end end
end end
end end