Basic functionality

This commit is contained in:
karel.picman@lbcfree.net 2020-06-18 16:29:31 +02:00
parent 6062d69d27
commit 6542db909c
4 changed files with 90 additions and 26 deletions

View File

@ -34,7 +34,7 @@ module RedmineDmsf
raise NotFound if Setting.plugin_redmine_dmsf['dmsf_webdav'].blank? raise NotFound if Setting.plugin_redmine_dmsf['dmsf_webdav'].blank?
@project = nil @project = nil
@public_path = "#{options[:root_uri_path]}#{path}" @public_path = "#{options[:root_uri_path]}#{path}"
super(path, request, response, options) super path, request, response, options
end end
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>"
@ -81,16 +81,17 @@ module RedmineDmsf
new_path = @path new_path = @path
new_path = new_path + '/' unless new_path[-1,1] == '/' new_path = new_path + '/' unless new_path[-1,1] == '/'
new_path = '/' + new_path unless new_path[0,1] == '/' new_path = '/' + new_path unless new_path[0,1] == '/'
@__proxy.class.new("#{new_path}#{name}", request, response, @options.merge(user: @user)) @__proxy.class.new "#{new_path}#{name}", request, response, @options.merge(user: @user)
end end
def child_project(p) def child_project(p)
project_display_name = ProjectResource.create_project_name(p) project_display_name = ProjectResource.create_project_name(p)
new_path = @path new_path = @path
#new_path = +'/'
new_path = new_path + '/' unless new_path[-1,1] == '/' new_path = new_path + '/' unless new_path[-1,1] == '/'
new_path = '/' + new_path unless new_path[0,1] == '/' new_path = '/' + new_path unless new_path[0,1] == '/'
new_path += project_display_name new_path += project_display_name
@__proxy.class.new(new_path, request, response, @options.merge(user: @user)) @__proxy.class.new new_path, request, response, @options.merge(user: @user)
end end
def parent def parent
@ -110,26 +111,35 @@ module RedmineDmsf
protected protected
def basename def basename
File.basename(@path) File.basename @path
end end
# Return instance of Project based on the path # Return instance of Project based on the path
def project def project
unless @project unless @project
pinfo = @path.split('/').drop(1) i = 1
if pinfo.length > 0 while true
if Setting.plugin_redmine_dmsf['dmsf_webdav_use_project_names'] pinfo = @path.split('/').drop(i)
if pinfo.first =~ /(\d+)$/ if pinfo.length > 0
@project = Project.find_by(id: $1) if Setting.plugin_redmine_dmsf['dmsf_webdav_use_project_names']
Rails.logger.error("No project found on path '#{@path}'") unless @project if pinfo.first =~ /(\d+)$/
end prj = Project.visible.find_by(id: $1)
else Rails.logger.error("No project found on path '#{@path}'") unless prj
begin end
@project = Project.find(pinfo.first) else
rescue => e begin
Rails.logger.error e.message scope = Project.visible.where(identifier: pinfo.first)
scope = scope.where(parent_id: @project.id) if @project
prj = scope.first
rescue => e
Rails.logger.error e.message
end
end end
end end
break unless prj
i = i + 1
@project = prj
prj = nil
end end
end end
@project @project
@ -137,6 +147,7 @@ module RedmineDmsf
# Make it easy to find the path without project in it. # Make it easy to find the path without project in it.
def projectless_path def projectless_path
# TODO:
'/' + @path.split('/').drop(2).join('/') '/' + @path.split('/').drop(2).join('/')
end end

View File

@ -31,12 +31,13 @@ module RedmineDmsf
def initialize(path, request, response, options) def initialize(path, request, response, options)
@folder = nil @folder = nil
@file = nil @file = nil
@subproject = nil
super path, request, response, options super path, request, response, options
end end
# Here we make sure our folder and file methods are not aliased - it should shave a few cycles off of processing # Here we make sure our folder and file methods are not aliased - it should shave a few cycles off of processing
def setup def setup
@skip_alias |= [ :folder, :file ] @skip_alias |= [ :folder, :file, :subproject ]
end end
# Gather collection of objects that denote current entities child entities # Gather collection of objects that denote current entities child entities
@ -46,13 +47,29 @@ module RedmineDmsf
def children def children
unless @children unless @children
@children = [] @children = []
if collection? if folder
# Folders
folder.dmsf_folders.visible.pluck(:title).each do |title| folder.dmsf_folders.visible.pluck(:title).each do |title|
@children.push child(title) @children.push child(title)
end end
# Files
folder.dmsf_files.visible.pluck(:name).each do |name| folder.dmsf_files.visible.pluck(:name).each do |name|
@children.push child(name) @children.push child(name)
end end
elsif subproject
# Projects
subproject.children.visible.select(:id, :identifier, :name).has_module(:dmsf).where(
Project.allowed_to_condition(User.current, :view_dmsf_folders)).find_each do |p|
@children << child_project(p)
end
# Folders
subproject.dmsf_folders.visible.pluck(:title).each do |title|
@children.push child(title)
end
# Files
subproject.dmsf_files.visible.pluck(:name).each do |name|
@children.push child(name)
end
end end
end end
@children @children
@ -61,17 +78,17 @@ module RedmineDmsf
# Does the object exist? # Does the object exist?
# If it is either a folder or a file, then it exists # If it is either a folder or a file, then it exists
def exist? def exist?
project && project.module_enabled?('dmsf') && (folder || file) && project && project.module_enabled?('dmsf') && (folder || file || subproject) &&
(User.current.admin? || User.current.allowed_to?(:view_dmsf_folders, project)) (User.current.admin? || User.current.allowed_to?(:view_dmsf_folders, project))
end end
def really_exist? def really_exist?
project && project.module_enabled?('dmsf') && (folder || file) project && project.module_enabled?('dmsf') && (folder || file || subproject)
end end
# Is this entity a folder? # Is this entity a folder?
def collection? def collection?
!folder.nil? folder || subproject
end end
# Check if current entity is a folder and return DmsfFolder object if found (nil if not) # Check if current entity is a folder and return DmsfFolder object if found (nil if not)
@ -86,12 +103,27 @@ module RedmineDmsf
# Check if the current entity exists as a file (DmsfFile), and returns corresponding object if found (nil otherwise) # Check if the current entity exists as a file (DmsfFile), and returns corresponding object if found (nil otherwise)
def file def file
unless @file unless @file
return nil unless project # Again if entity project is nil, it cannot exist in context of this object @file = DmsfFile.find_file_by_name(project, parent.folder, basename) if project
@file = DmsfFile.find_file_by_name(project, parent.folder, basename)
end end
@file @file
end end
def subproject
unless @subproject
@subproject = Project.visible.where(parent_id: parent_project.id, name: basename).first if parent_project
end
@subproject
end
def parent_project
unless @parent_project
if /\/(.+)\/#{project.identifier}\/?$/.match(@path)
@parent_project = Project.visible.where(identifier: $1).first
end
end
@parent_project
end
# Return the content type of file # Return the content type of file
# will return inode/directory for any collections, and appropriate for File entities # will return inode/directory for any collections, and appropriate for File entities
def content_type def content_type
@ -99,6 +131,8 @@ module RedmineDmsf
'inode/directory' 'inode/directory'
elsif file && file.last_revision elsif file && file.last_revision
file.last_revision.detect_content_type file.last_revision.detect_content_type
elsif subproject
'inode/directory'
else else
NotFound NotFound
end end
@ -109,6 +143,8 @@ module RedmineDmsf
folder.created_at folder.created_at
elsif file elsif file
file.created_at file.created_at
elsif subproject
subproject.created_on
else else
NotFound NotFound
end end
@ -119,6 +155,8 @@ module RedmineDmsf
folder.updated_at folder.updated_at
elsif file && file.last_revision elsif file && file.last_revision
file.last_revision.updated_at file.last_revision.updated_at
elsif subproject
subproject.updated_on
else else
NotFound NotFound
end end
@ -135,7 +173,11 @@ module RedmineDmsf
end end
def special_type def special_type
l(:field_folder) if folder if folder
l(:field_folder)
elsif subproject
l(:field_project)
end
end end
# Process incoming GET request # Process incoming GET request

View File

@ -32,9 +32,9 @@ module RedmineDmsf
def children def children
unless @projects unless @projects
@projects = [] @projects = []
Project.select(:id, :identifier, :name).has_module(:dmsf).where( Project.visible.select(:id, :identifier, :name).has_module(:dmsf).where(
Project.allowed_to_condition( Project.allowed_to_condition(
User.current, :view_dmsf_folders)).order('lft').find_each do |p| User.current, :view_dmsf_folders)).find_each do |p|
@projects << child_project(p) @projects << child_project(p)
end end
end end

View File

@ -34,9 +34,20 @@ module RedmineDmsf
unless @children unless @children
@children = [] @children = []
if project if project
# Sub-projects
# project.children.select(:id, :identifier, :name).has_module(:dmsf).where(
# Project.allowed_to_condition(
# User.current, :view_dmsf_folders)).order('lft').pluck(:name).each do |name|
# @children.push child(name)
project.children.visible.select(:id, :identifier, :name).has_module(:dmsf).where(
Project.allowed_to_condition(User.current, :view_dmsf_folders)).find_each do |p|
@children << child_project(p)
end
# Folders
project.dmsf_folders.visible.pluck(:title).each do |title| project.dmsf_folders.visible.pluck(:title).each do |title|
@children.push child(title) @children.push child(title)
end end
# Files
project.dmsf_files.visible.pluck(:name).each do |name| project.dmsf_files.visible.pluck(:name).each do |name|
@children.push child(name) @children.push child(name)
end end