Rubocop FileResource class and update references
This commit is contained in:
@@ -1,10 +1,14 @@
|
|||||||
AllCops:
|
AllCops:
|
||||||
TargetRubyVersion: 2.3
|
TargetRubyVersion: 2.3
|
||||||
Exclude:
|
Exclude:
|
||||||
- 'spec/dummy/**/*'
|
- 'spec/**/*'
|
||||||
|
Metrics/BlockLength:
|
||||||
|
Exclude:
|
||||||
|
- 'Rakefile'
|
||||||
Metrics/ClassLength:
|
Metrics/ClassLength:
|
||||||
Exclude:
|
Exclude:
|
||||||
- 'lib/calligraphy/resource/resource.rb'
|
- 'lib/calligraphy/resource/resource.rb'
|
||||||
|
- 'lib/calligraphy/resource/file_resource.rb'
|
||||||
Metrics/AbcSize:
|
Metrics/AbcSize:
|
||||||
Exclude:
|
Exclude:
|
||||||
- 'lib/calligraphy/rails/mapper.rb'
|
- 'lib/calligraphy/rails/mapper.rb'
|
||||||
|
|||||||
@@ -1,13 +1,22 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'pstore'
|
require 'pstore'
|
||||||
|
|
||||||
module Calligraphy
|
module Calligraphy
|
||||||
|
# Resource that's handles writing and deleting directories and files to disk.
|
||||||
class FileResource < Resource
|
class FileResource < Resource
|
||||||
|
DAV_PROPERTY_METHODS = %w[
|
||||||
|
creationdate displayname getcontentlanguage getcontentlength
|
||||||
|
getcontenttype getetag getlastmodified lockdiscovery resourcetype
|
||||||
|
supportedlock
|
||||||
|
].freeze
|
||||||
|
|
||||||
include Calligraphy::Utils
|
include Calligraphy::Utils
|
||||||
|
|
||||||
|
#:nodoc:
|
||||||
def initialize(resource: nil, req: nil, mount: nil, root_dir: Dir.pwd)
|
def initialize(resource: nil, req: nil, mount: nil, root_dir: Dir.pwd)
|
||||||
super
|
super
|
||||||
|
|
||||||
@root_dir = root_dir || Dir.pwd
|
|
||||||
@src_path = join_paths @root_dir, @request_path
|
@src_path = join_paths @root_dir, @request_path
|
||||||
|
|
||||||
if exists?
|
if exists?
|
||||||
@@ -19,91 +28,97 @@ module Calligraphy
|
|||||||
set_ancestors
|
set_ancestors
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Responsible for returning a boolean value indicating if an ancestor
|
||||||
|
# exists for the resource.
|
||||||
|
#
|
||||||
|
# Used in COPY and MKCOL requests.
|
||||||
def ancestor_exist?
|
def ancestor_exist?
|
||||||
File.exist? @ancestor_path
|
File.exist? @ancestor_path
|
||||||
end
|
end
|
||||||
|
|
||||||
def can_copy?(options)
|
# Responsible for returning a boolean value indicating if the resource
|
||||||
copy_options = { can_copy: false, ancestor_exist: false, locked: false }
|
# is a collection.
|
||||||
|
#
|
||||||
overwrite = true? options[:overwrite]
|
# Used in DELETE, MKCOL, MOVE, and PUT requests.
|
||||||
destination = options[:destination].tap { |s| s.slice! @mount_point }
|
|
||||||
copy_options[:ancestor_exist] = File.exist? parent_path(destination)
|
|
||||||
|
|
||||||
to_path = join_paths @root_dir, destination
|
|
||||||
to_path_exist = File.exist? to_path
|
|
||||||
|
|
||||||
copy_options[:locked] = if to_path_exist
|
|
||||||
if destination_locked? to_path
|
|
||||||
true
|
|
||||||
else
|
|
||||||
to_path_parent = split_and_pop(path: to_path).join '/'
|
|
||||||
common_ancestor = common_path_ancestors(to_path, @ancestors).first
|
|
||||||
to_path_ancestors = ancestors_from_path_to_ancestor to_path, common_ancestor
|
|
||||||
|
|
||||||
locking_ancestor? to_path_parent, to_path_ancestors
|
|
||||||
end
|
|
||||||
else
|
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
if copy_options[:ancestor_exist]
|
|
||||||
if !overwrite && to_path_exist
|
|
||||||
copy_options[:can_copy] = false
|
|
||||||
else
|
|
||||||
copy_options[:can_copy] = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
copy_options
|
|
||||||
end
|
|
||||||
|
|
||||||
def collection?
|
def collection?
|
||||||
File.directory? @src_path
|
File.directory? @src_path
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates a duplicate of the resource in `options[:destination]`.
|
# Responsible for returning a hash with keys indicating if the resource
|
||||||
def copy(options)
|
# can be copied, if an ancestor exists, or if the copy destinatin is
|
||||||
destination = options[:destination].tap { |s| s.slice! @mount_point }
|
# locked.
|
||||||
preserve_existing = false? options[:overwrite]
|
#
|
||||||
|
# Return hash should contain `can_copy`, `ancestor_exist`, and `locked`
|
||||||
|
# keys with boolean values.
|
||||||
|
#
|
||||||
|
# Used in COPY and MOVE (which inherits from COPY) requests.
|
||||||
|
def copy_options(options)
|
||||||
|
copy_options = { can_copy: false, ancestor_exist: false, locked: false }
|
||||||
|
|
||||||
|
destination = copy_destination options
|
||||||
to_path = join_paths @root_dir, destination
|
to_path = join_paths @root_dir, destination
|
||||||
to_path_exists = File.exist? to_path
|
to_path_exists = File.exist? to_path
|
||||||
|
|
||||||
if collection?
|
copy_options[:ancestor_exist] = File.exist? parent_path destination
|
||||||
FileUtils.cp_r @src_path, to_path, preserve: preserve_existing
|
copy_options[:locked] = can_copy_locked_option to_path, to_path_exists
|
||||||
else
|
copy_options = can_copy_option copy_options, options, to_path_exists
|
||||||
FileUtils.cp @src_path, to_path, preserve: preserve_existing
|
copy_options
|
||||||
end
|
end
|
||||||
|
|
||||||
if store_exist? && preserve_existing
|
# Responsible for creating a duplicate of the resource in
|
||||||
dest_store_path = collection? ? "#{to_path}/#{@name}" : to_path
|
# `options[:destination]` (see section 9.8 of RFC4918).
|
||||||
dest_store_path += ".pstore"
|
#
|
||||||
|
# Used in COPY and MOVE (which inherits from COPY) requests.
|
||||||
|
def copy(options)
|
||||||
|
destination = copy_destination options
|
||||||
|
to_path = join_paths @root_dir, destination
|
||||||
|
to_path_exists = File.exist? to_path
|
||||||
|
|
||||||
FileUtils.cp @store_path, dest_store_path, preserve: preserve_existing
|
preserve_existing = false? options[:overwrite]
|
||||||
end
|
|
||||||
|
copy_resource_to_path to_path, preserve_existing
|
||||||
|
copy_pstore_to_path to_path, preserve_existing
|
||||||
|
|
||||||
to_path_exists
|
to_path_exists
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Responsible for creating a new collection based on the resource (see
|
||||||
|
# section 9.3 of RFC4918).
|
||||||
|
#
|
||||||
|
# Used in MKCOL requests.
|
||||||
def create_collection
|
def create_collection
|
||||||
Dir.mkdir @src_path
|
Dir.mkdir @src_path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Responsible for deleting a resource collection (see section 9.6 of
|
||||||
|
# RFC4918).
|
||||||
|
#
|
||||||
|
# Used in DELETE and MOVE requests.
|
||||||
def delete_collection
|
def delete_collection
|
||||||
FileUtils.rm_r @src_path
|
FileUtils.rm_r @src_path
|
||||||
FileUtils.rm_r @store_path if store_exist?
|
FileUtils.rm_r @store_path if store_exist?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Responsible for returning unique identifier used to create an etag.
|
||||||
|
#
|
||||||
|
# Used in precondition validation, as well as GET, HEAD, and PROPFIND
|
||||||
|
# requests.
|
||||||
def etag
|
def etag
|
||||||
[@updated_at.to_i, @stats[:inode], @stats[:size]].join('-').to_s
|
[@updated_at.to_i, @stats[:inode], @stats[:size]].join('-').to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Responsible for indicating if the resource already exists.
|
||||||
|
#
|
||||||
|
# Used in DELETE, LOCK, MKCOL, and MOVE requests.
|
||||||
def exists?
|
def exists?
|
||||||
File.exist? @src_path
|
File.exist? @src_path
|
||||||
end
|
end
|
||||||
|
|
||||||
def lock(nodes, depth='infinity')
|
# Responsible for creating a lock on the resource (see section 9.10 of
|
||||||
|
# RFC4918).
|
||||||
|
#
|
||||||
|
# Used in LOCK requests.
|
||||||
|
def lock(nodes, depth = 'infinity')
|
||||||
properties = {}
|
properties = {}
|
||||||
|
|
||||||
nodes.each do |node|
|
nodes.each do |node|
|
||||||
@@ -111,30 +126,32 @@ module Calligraphy
|
|||||||
properties[node.name.to_sym] = node
|
properties[node.name.to_sym] = node
|
||||||
end
|
end
|
||||||
|
|
||||||
unless exists?
|
create_blank_file unless exists?
|
||||||
write ''
|
|
||||||
@name = File.basename @src_path
|
|
||||||
init_pstore
|
|
||||||
end
|
|
||||||
|
|
||||||
create_lock properties, depth
|
create_lock properties, depth
|
||||||
|
fetch_lock_info
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Responsible for indicating if a resource lock is exclusive.
|
||||||
|
#
|
||||||
|
# Used in LOCK requests.
|
||||||
def lock_is_exclusive?
|
def lock_is_exclusive?
|
||||||
lockscope == 'exclusive'
|
lockscope == 'exclusive'
|
||||||
end
|
end
|
||||||
|
|
||||||
def lock_tokens
|
# Responsible for indicating if a resource is current locked.
|
||||||
get_lock_info
|
#
|
||||||
@lock_info&.each { |x| x }&.map { |k, v| k[:locktoken].children[0].text }
|
# Used in LOCK requests.
|
||||||
end
|
|
||||||
|
|
||||||
def locked?
|
def locked?
|
||||||
get_lock_info
|
fetch_lock_info
|
||||||
|
|
||||||
obj_exists_and_is_not_type? obj: @lock_info, type: []
|
obj_exists_and_is_not_type? obj: @lock_info, type: []
|
||||||
end
|
end
|
||||||
|
|
||||||
def locked_to_user?(headers=nil)
|
# Responsible for indicating if a resource is locked to the current user.
|
||||||
|
#
|
||||||
|
# Used in DELETE, LOCK, MOVE, PROPPATCH, and PUT requests.
|
||||||
|
def locked_to_user?(headers = nil)
|
||||||
if locked?
|
if locked?
|
||||||
!can_unlock? headers
|
!can_unlock? headers
|
||||||
else
|
else
|
||||||
@@ -142,6 +159,10 @@ module Calligraphy
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Responsible for handling the retrieval of properties defined on the
|
||||||
|
# resource (see section 9.1 of RFC4918).
|
||||||
|
#
|
||||||
|
# Used in PROPFIND requests.
|
||||||
def propfind(nodes)
|
def propfind(nodes)
|
||||||
properties = { found: [], not_found: [] }
|
properties = { found: [], not_found: [] }
|
||||||
|
|
||||||
@@ -151,96 +172,73 @@ module Calligraphy
|
|||||||
|
|
||||||
value = get_property prop
|
value = get_property prop
|
||||||
|
|
||||||
if value.nil?
|
update_found_properties properties, prop, value
|
||||||
properties[:not_found].push prop
|
|
||||||
elsif value.is_a? Hash
|
|
||||||
value.each_key do |key|
|
|
||||||
properties[:found].push value[key]
|
|
||||||
end
|
|
||||||
else
|
|
||||||
properties[:found].push value
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
properties
|
properties
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Responsible for handling the addition and/or removal of properties
|
||||||
|
# defined on the resource through a PROPPATCH request (see section 9.2 of
|
||||||
|
# RFC4918).
|
||||||
|
#
|
||||||
|
# Used in PROPPATCH requests.
|
||||||
def proppatch(nodes)
|
def proppatch(nodes)
|
||||||
actions = { set: [], remove: [] }
|
actions = { set: [], remove: [] }
|
||||||
|
|
||||||
@store.transaction do
|
@store.transaction do
|
||||||
@store[:properties] = {} unless @store[:properties].is_a? Hash
|
@store[:properties] = {} unless @store[:properties].is_a? Hash
|
||||||
|
|
||||||
nodes.each do |node|
|
add_remove_properties nodes, actions
|
||||||
if node.name == 'set'
|
|
||||||
node.children.each do |prop|
|
|
||||||
prop.children.each do |property|
|
|
||||||
prop_sym = property.name.to_sym
|
|
||||||
node = Calligraphy::XML::Node.new property
|
|
||||||
|
|
||||||
if @store[:properties][prop_sym]
|
|
||||||
if @store[:properties][prop_sym].is_a? Array
|
|
||||||
unless matching_namespace? @store[:properties][prop_sym], node
|
|
||||||
@store[:properties][prop_sym].push node
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if !same_namespace? @store[:properties][prop_sym], node
|
|
||||||
@store[:properties][prop_sym] = [@store[:properties][prop_sym]]
|
|
||||||
@store[:properties][prop_sym].push node
|
|
||||||
else
|
|
||||||
@store[:properties][prop_sym] = node
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
@store[:properties][prop_sym] = node
|
|
||||||
end
|
|
||||||
|
|
||||||
actions[:set].push property
|
|
||||||
end
|
|
||||||
end
|
|
||||||
elsif node.name == 'remove'
|
|
||||||
node.children.each do |prop|
|
|
||||||
prop.children.each do |property|
|
|
||||||
@store[:properties].delete property.name.to_sym
|
|
||||||
|
|
||||||
actions[:remove].push property
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
get_custom_property nil
|
get_custom_property nil
|
||||||
actions
|
actions
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Responsible for setting and returning the contents of a resource
|
||||||
|
# if it is readable (see section 9.4 of RFC4918).
|
||||||
|
#
|
||||||
|
# Used in GET requests.
|
||||||
def read
|
def read
|
||||||
@contents ||= File.read @src_path if readable?
|
@contents ||= File.read @src_path if readable?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Responsible for refreshing locks (see section 9.10.2 of RFC4918).
|
||||||
|
#
|
||||||
|
# Used in LOCK requests.
|
||||||
def refresh_lock
|
def refresh_lock
|
||||||
if locked?
|
if locked?
|
||||||
@store.transaction do
|
@store.transaction do
|
||||||
@store[:lockdiscovery][-1][:timeout] = timeout_node
|
@store[:lockdiscovery][-1][:timeout] = timeout_node
|
||||||
end
|
end
|
||||||
|
|
||||||
get_lock_info
|
fetch_lock_info
|
||||||
else
|
else
|
||||||
refresh_ancestor_locks @ancestor_path, @ancestors.dup
|
refresh_ancestor_locks @ancestor_path, @ancestors.dup
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Responsible for unlocking a resource lock (see section 9.11 of RFC4918).
|
||||||
|
#
|
||||||
|
# Used in UNLOCK requests.
|
||||||
def unlock(token)
|
def unlock(token)
|
||||||
if lock_tokens.include? token
|
if lock_tokens.include? token
|
||||||
remove_lock token
|
remove_lock token
|
||||||
|
@lock_info = nil
|
||||||
|
|
||||||
:no_content
|
:no_content
|
||||||
else
|
else
|
||||||
:forbidden
|
:forbidden
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def write(contents=@request_body.to_s)
|
# Responsible for writing contents to a resource (see section 9.7 of
|
||||||
|
# RFC4918).
|
||||||
|
#
|
||||||
|
# Used in PUT requests.
|
||||||
|
def write(contents = @request_body.to_s)
|
||||||
@contents = contents
|
@contents = contents
|
||||||
|
|
||||||
File.open(@src_path, 'w') do |file|
|
File.open(@src_path, 'w') do |file|
|
||||||
@@ -262,7 +260,7 @@ module Calligraphy
|
|||||||
@stats = {
|
@stats = {
|
||||||
created_at: file_stats.ctime,
|
created_at: file_stats.ctime,
|
||||||
inode: file_stats.ino,
|
inode: file_stats.ino,
|
||||||
size: file_stats.size,
|
size: file_stats.size
|
||||||
}
|
}
|
||||||
@updated_at = file_stats.mtime
|
@updated_at = file_stats.mtime
|
||||||
end
|
end
|
||||||
@@ -276,6 +274,34 @@ module Calligraphy
|
|||||||
join_paths @root_dir, split_and_pop(path: path)
|
join_paths @root_dir, split_and_pop(path: path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def copy_destination(options)
|
||||||
|
options[:destination].tap { |s| s.slice! @mount_point }
|
||||||
|
end
|
||||||
|
|
||||||
|
def can_copy_locked_option(to_path, to_path_exists)
|
||||||
|
return false unless to_path_exists
|
||||||
|
return true if destination_locked? to_path
|
||||||
|
|
||||||
|
to_path_parent = split_and_pop(path: to_path).join '/'
|
||||||
|
common_ancestor = common_path_ancestors(to_path, @ancestors).first
|
||||||
|
to_path_ancestors = ancestors_from_path_to_ancestor(to_path,
|
||||||
|
common_ancestor)
|
||||||
|
|
||||||
|
locking_ancestor? to_path_parent, to_path_ancestors
|
||||||
|
end
|
||||||
|
|
||||||
|
def can_copy_option(copy_options, options, to_path_exists)
|
||||||
|
return copy_options unless copy_options[:ancestor_exist]
|
||||||
|
|
||||||
|
copy_options[:can_copy] = if false?(options[:overwrite]) && to_path_exists
|
||||||
|
false
|
||||||
|
else
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
copy_options
|
||||||
|
end
|
||||||
|
|
||||||
def destination_locked?(path)
|
def destination_locked?(path)
|
||||||
store = PStore.new "#{path}.pstore"
|
store = PStore.new "#{path}.pstore"
|
||||||
lock = store.transaction(true) { store[:lockdiscovery] }
|
lock = store.transaction(true) { store[:lockdiscovery] }
|
||||||
@@ -296,36 +322,51 @@ module Calligraphy
|
|||||||
path = split_and_pop path: path
|
path = split_and_pop path: path
|
||||||
ancestors = []
|
ancestors = []
|
||||||
|
|
||||||
until path.last == stop_at_ancestor
|
ancestors.push path.pop until path.last == stop_at_ancestor
|
||||||
ancestors.push path.pop
|
|
||||||
end
|
|
||||||
|
|
||||||
ancestors.push stop_at_ancestor
|
ancestors.push stop_at_ancestor
|
||||||
ancestors.reverse
|
ancestors.reverse
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def copy_resource_to_path(to_path, preserve_existing)
|
||||||
|
if collection?
|
||||||
|
FileUtils.cp_r @src_path, to_path, preserve: preserve_existing
|
||||||
|
else
|
||||||
|
FileUtils.cp @src_path, to_path, preserve: preserve_existing
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def copy_pstore_to_path(to_path, preserve_existing)
|
||||||
|
return unless store_exist? && preserve_existing
|
||||||
|
|
||||||
|
dest_store_path = collection? ? "#{to_path}/#{@name}" : to_path
|
||||||
|
dest_store_path += '.pstore'
|
||||||
|
|
||||||
|
FileUtils.cp @store_path, dest_store_path, preserve: preserve_existing
|
||||||
|
end
|
||||||
|
|
||||||
def store_exist?
|
def store_exist?
|
||||||
File.exist? @store_path
|
File.exist? @store_path
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_blank_file
|
||||||
|
write ''
|
||||||
|
@name = File.basename @src_path
|
||||||
|
init_pstore
|
||||||
|
end
|
||||||
|
|
||||||
def create_lock(properties, depth)
|
def create_lock(properties, depth)
|
||||||
@store.transaction do
|
@store.transaction do
|
||||||
@store[:lockcreator] = client_nonce
|
@store[:lockcreator] = client_nonce
|
||||||
@store[:lockdiscovery] = [] unless @store[:lockdiscovery].is_a? Array
|
|
||||||
@store[:lockdepth] = depth
|
@store[:lockdepth] = depth
|
||||||
|
@store[:lockdiscovery] = [] unless @store[:lockdiscovery].is_a? Array
|
||||||
|
|
||||||
activelock = {}
|
@store[:lockdiscovery].push({}.tap do |activelock|
|
||||||
activelock[:locktoken] = create_lock_token
|
activelock[:locktoken] = create_lock_token
|
||||||
activelock[:timeout] = timeout_node
|
activelock[:timeout] = timeout_node
|
||||||
|
|
||||||
properties.each_key do |prop|
|
add_lock_properties activelock, properties
|
||||||
activelock[prop] = Calligraphy::XML::Node.new properties[prop]
|
end)
|
||||||
end
|
|
||||||
|
|
||||||
@store[:lockdiscovery].push activelock
|
|
||||||
end
|
end
|
||||||
|
|
||||||
get_lock_info
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_lock_token
|
def create_lock_token
|
||||||
@@ -347,7 +388,13 @@ module Calligraphy
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_lock_info
|
def add_lock_properties(activelock, properties)
|
||||||
|
properties.each_key do |prop|
|
||||||
|
activelock[prop] = Calligraphy::XML::Node.new properties[prop]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def fetch_lock_info
|
||||||
return nil if @store.nil?
|
return nil if @store.nil?
|
||||||
|
|
||||||
@lock_info = @store.transaction(true) { @store[:lockdiscovery] }
|
@lock_info = @store.transaction(true) { @store[:lockdiscovery] }
|
||||||
@@ -358,94 +405,109 @@ module Calligraphy
|
|||||||
@lock_info[-1][:lockscope].children[0].name
|
@lock_info[-1][:lockscope].children[0].name
|
||||||
end
|
end
|
||||||
|
|
||||||
def can_unlock?(headers=nil)
|
def can_unlock?(headers = nil)
|
||||||
token = unless headers.nil?
|
token = unless headers.nil?
|
||||||
extract_lock_token(headers['If']) if headers['If']
|
extract_lock_token(headers['If']) if headers['If']
|
||||||
end
|
end
|
||||||
|
|
||||||
lock_tokens.include? token
|
lock_tokens.include? token
|
||||||
end
|
end
|
||||||
|
|
||||||
def locking_ancestor?(ancestor_path, ancestors, headers=nil)
|
def lock_tokens
|
||||||
|
fetch_lock_info
|
||||||
|
@lock_info&.each { |x| x }&.map { |k| k[:locktoken].children[0].text }
|
||||||
|
end
|
||||||
|
|
||||||
|
def locking_ancestor?(ancestor_path, ancestors, headers = nil)
|
||||||
|
ancestor_info = ancestor_lock_info headers
|
||||||
ancestor_store_path = "#{ancestor_path}/#{ancestors[-1]}.pstore"
|
ancestor_store_path = "#{ancestor_path}/#{ancestors[-1]}.pstore"
|
||||||
check_lock_creator = Calligraphy.enable_digest_authentication
|
|
||||||
blocking_lock = false
|
|
||||||
unlockable = true
|
|
||||||
|
|
||||||
ancestors.pop
|
ancestors.pop
|
||||||
|
|
||||||
if File.exist? ancestor_store_path
|
check_for_ancestor ancestor_info, ancestor_store_path
|
||||||
ancestor_store = PStore.new ancestor_store_path
|
|
||||||
|
|
||||||
ancestor_lock = nil
|
if ancestor_info[:blocking] || ancestors.empty?
|
||||||
ancestor_lock_creator = nil
|
assign_locking_ancestor ancestor_info
|
||||||
ancestor_lock_depth = nil
|
|
||||||
|
|
||||||
ancestor_store.transaction(true) do
|
return ancestor_info[:unlockable] ? false : true
|
||||||
ancestor_lock = ancestor_store[:lockdiscovery]
|
|
||||||
ancestor_lock_depth = ancestor_store[:lockdepth]
|
|
||||||
|
|
||||||
if check_lock_creator
|
|
||||||
ancestor_lock_creator = ancestor_store[:lockcreator]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
blocking_lock = obj_exists_and_is_not_type? obj: ancestor_lock, type: []
|
|
||||||
|
|
||||||
if blocking_lock
|
|
||||||
token = unless headers.nil?
|
|
||||||
extract_lock_token(headers['If']) if headers['If']
|
|
||||||
end
|
|
||||||
|
|
||||||
ancestor_lock_tokens = ancestor_lock
|
|
||||||
.each { |x| x }
|
|
||||||
.map { |k, v| k[:locktoken].children[0].text }
|
|
||||||
|
|
||||||
unlockable = ancestor_lock_tokens.include?(token) ||
|
|
||||||
(check_lock_creator && (ancestor_lock_creator == client_nonce))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if blocking_lock || ancestors.empty?
|
|
||||||
@locking_ancestor = {
|
|
||||||
depth: ancestor_lock_depth,
|
|
||||||
info: ancestor_lock
|
|
||||||
}
|
|
||||||
|
|
||||||
return unlockable ? false : true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
next_ancestor = split_and_pop(path: ancestor_path).join '/'
|
next_ancestor = split_and_pop(path: ancestor_path).join '/'
|
||||||
locking_ancestor? next_ancestor, ancestors, headers
|
locking_ancestor? next_ancestor, ancestors, ancestor_info[:headers]
|
||||||
|
end
|
||||||
|
|
||||||
|
def ancestor_lock_info(headers)
|
||||||
|
{
|
||||||
|
blocking: false,
|
||||||
|
check_creator: Calligraphy.enable_digest_authentication,
|
||||||
|
creator: nil,
|
||||||
|
depth: nil,
|
||||||
|
headers: headers || nil,
|
||||||
|
lock: nil,
|
||||||
|
unlockable: true
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_for_ancestor(ancestor_info, store_path)
|
||||||
|
return unless File.exist? store_path
|
||||||
|
ancestor_lock_from_store ancestor_info, store_path
|
||||||
|
|
||||||
|
ancestor_info[:blocking] = obj_exists_and_is_not_type?(
|
||||||
|
obj: ancestor_info[:lock],
|
||||||
|
type: []
|
||||||
|
)
|
||||||
|
|
||||||
|
blocking_lock_unlockable? ancestor_info if ancestor_info[:blocking]
|
||||||
|
end
|
||||||
|
|
||||||
|
def ancestor_lock_from_store(lock_info, store_path)
|
||||||
|
ancestor_store = PStore.new store_path
|
||||||
|
|
||||||
|
ancestor_store.transaction(true) do
|
||||||
|
lock_info[:lock] = ancestor_store[:lockdiscovery]
|
||||||
|
lock_info[:depth] = ancestor_store[:lockdepth]
|
||||||
|
|
||||||
|
if lock_info[:check_creator]
|
||||||
|
lock_info[:creator] = ancestor_store[:lockcreator]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def blocking_lock_unlockable?(lock_info)
|
||||||
|
headers = lock_info[:headers]
|
||||||
|
|
||||||
|
token = unless headers.nil?
|
||||||
|
extract_lock_token(headers['If']) if headers['If']
|
||||||
|
end
|
||||||
|
|
||||||
|
ancestor_tokens = lock_info[:lock]
|
||||||
|
|
||||||
|
lock_info[:unlockable] =
|
||||||
|
ancestor_tokens.include?(token) ||
|
||||||
|
(lock_info[:check_creator] && (lock_info[:creator] == client_nonce))
|
||||||
|
end
|
||||||
|
|
||||||
|
def ancestor_lock_tokens(lock_info)
|
||||||
|
lock_info[:lock].each { |x| x }.map { |k| k[:locktoken].children[0].text }
|
||||||
|
end
|
||||||
|
|
||||||
|
def assign_locking_ancestor(ancestor_info)
|
||||||
|
@locking_ancestor = {
|
||||||
|
depth: ancestor_info[:depth],
|
||||||
|
info: ancestor_info[:lock]
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_property(prop)
|
def get_property(prop)
|
||||||
case prop.name
|
case prop.name
|
||||||
when 'creationdate'
|
|
||||||
prop.content = creationdate
|
|
||||||
when 'displayname'
|
|
||||||
prop.content = displayname
|
|
||||||
when 'getcontentlanguage'
|
|
||||||
prop.content = getcontentlanguage
|
|
||||||
when 'getcontentlength'
|
|
||||||
prop.content = getcontentlength
|
|
||||||
when 'getcontenttype'
|
|
||||||
prop.content = getcontenttype
|
|
||||||
when 'getetag'
|
|
||||||
prop.content = getetag
|
|
||||||
when 'getlastmodified'
|
|
||||||
prop.content = getlastmodified
|
|
||||||
when 'lockdiscovery'
|
when 'lockdiscovery'
|
||||||
return get_lock_info
|
fetch_lock_info
|
||||||
when 'resourcetype'
|
when *DAV_PROPERTY_METHODS
|
||||||
prop.content = resourcetype
|
prop.content = send prop.name
|
||||||
when 'supportedlock'
|
prop
|
||||||
prop.content = supportedlock
|
|
||||||
else
|
else
|
||||||
return get_custom_property prop.name
|
get_custom_property prop.name
|
||||||
end
|
end
|
||||||
|
|
||||||
prop
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def creationdate
|
def creationdate
|
||||||
@@ -457,7 +519,7 @@ module Calligraphy
|
|||||||
end
|
end
|
||||||
|
|
||||||
def getcontentlanguage
|
def getcontentlanguage
|
||||||
get_custom_property(:contentlanguage)
|
get_custom_property :contentlanguage
|
||||||
end
|
end
|
||||||
|
|
||||||
def getcontentlength
|
def getcontentlength
|
||||||
@@ -465,7 +527,7 @@ module Calligraphy
|
|||||||
end
|
end
|
||||||
|
|
||||||
def getcontenttype
|
def getcontenttype
|
||||||
get_custom_property(:contenttype)
|
get_custom_property :contenttype
|
||||||
end
|
end
|
||||||
|
|
||||||
def getetag
|
def getetag
|
||||||
@@ -478,7 +540,7 @@ module Calligraphy
|
|||||||
end
|
end
|
||||||
|
|
||||||
def lockdiscovery
|
def lockdiscovery
|
||||||
get_lock_info
|
fetch_lock_info
|
||||||
end
|
end
|
||||||
|
|
||||||
def resourcetype
|
def resourcetype
|
||||||
@@ -497,12 +559,85 @@ module Calligraphy
|
|||||||
@store_properties[prop.to_sym] unless @store_properties.nil? || prop.nil?
|
@store_properties[prop.to_sym] unless @store_properties.nil? || prop.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
def matching_namespace?(node_arr, node)
|
def update_found_properties(properties, prop, value)
|
||||||
node_arr.select { |x| x.namespace.href == node.namespace.href }.length > 0
|
if value.nil?
|
||||||
|
properties[:not_found].push prop
|
||||||
|
elsif value.is_a? Hash
|
||||||
|
value.each_key do |key|
|
||||||
|
properties[:found].push value[key]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
properties[:found].push value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_remove_properties(nodes, actions)
|
||||||
|
nodes.each do |node|
|
||||||
|
if node.name == 'set'
|
||||||
|
add_properties node, actions
|
||||||
|
elsif node.name == 'remove'
|
||||||
|
remove_properties node, actions
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_properties(node, actions)
|
||||||
|
node.children.each do |prop|
|
||||||
|
prop.children.each do |property|
|
||||||
|
node = Calligraphy::XML::Node.new property
|
||||||
|
prop_sym = property.name.to_sym
|
||||||
|
|
||||||
|
store_property_node node, prop_sym
|
||||||
|
|
||||||
|
actions[:set].push property
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def store_property_node(node, prop)
|
||||||
|
# Property does not exist yet so we can just store the property node.
|
||||||
|
return @store[:properties][prop] = node unless @store[:properties][prop]
|
||||||
|
|
||||||
|
if @store[:properties][prop].is_a? Array
|
||||||
|
store_mismatch_namespace_property_node node, prop
|
||||||
|
elsif same_namespace? @store[:properties][prop], node
|
||||||
|
# If stored property and node have the same namespace, we can just
|
||||||
|
# overwrite the previously stored property node.
|
||||||
|
@store[:properties][prop] = node
|
||||||
|
else
|
||||||
|
# If stored property and node DO NOT have the same namespace, create
|
||||||
|
# an array for the stored property and push the new property node.
|
||||||
|
store_mismatch_namespace_property_nodes node, prop
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def store_mismatch_namespace_property_node(node, prop)
|
||||||
|
node_arr = @store[:properties][prop]
|
||||||
|
|
||||||
|
namespace_mismatch = node_arr.select do |x|
|
||||||
|
x.namespace.href == node.namespace.href
|
||||||
|
end.length.positive?
|
||||||
|
|
||||||
|
@store[:properties][prop].push node unless namespace_mismatch
|
||||||
end
|
end
|
||||||
|
|
||||||
def same_namespace?(node1, node2)
|
def same_namespace?(node1, node2)
|
||||||
node1.namespace.href == node2.namespace.href
|
node1.namespace&.href == node2.namespace&.href
|
||||||
|
end
|
||||||
|
|
||||||
|
def store_mismatch_namespace_property_nodes(node, prop)
|
||||||
|
@store[:properties][prop] = [@store[:properties][prop]]
|
||||||
|
@store[:properties][prop].push node
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_properties(node, actions)
|
||||||
|
node.children.each do |prop|
|
||||||
|
prop.children.each do |property|
|
||||||
|
@store[:properties].delete property.name.to_sym
|
||||||
|
|
||||||
|
actions[:remove].push property
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def refresh_ancestor_locks(ancestor_path, ancestors)
|
def refresh_ancestor_locks(ancestor_path, ancestors)
|
||||||
@@ -510,11 +645,7 @@ module Calligraphy
|
|||||||
ancestors.pop
|
ancestors.pop
|
||||||
|
|
||||||
if File.exist? ancestor_store_path
|
if File.exist? ancestor_store_path
|
||||||
ancestor_store = PStore.new ancestor_store_path
|
ancestor_lock = refresh_ancestor_lock ancestor_store_path
|
||||||
ancestor_lock = ancestor_store.transaction do
|
|
||||||
ancestor_store[:lockdiscovery][-1][:timeout] = timeout_node
|
|
||||||
ancestor_store[:lockdiscovery]
|
|
||||||
end
|
|
||||||
|
|
||||||
return map_array_of_hashes ancestor_lock
|
return map_array_of_hashes ancestor_lock
|
||||||
end
|
end
|
||||||
@@ -523,6 +654,15 @@ module Calligraphy
|
|||||||
refresh_ancestor_locks next_ancestor, ancestors
|
refresh_ancestor_locks next_ancestor, ancestors
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def refresh_ancestor_lock(ancestor_store_path)
|
||||||
|
ancestor_store = PStore.new ancestor_store_path
|
||||||
|
|
||||||
|
ancestor_store.transaction do
|
||||||
|
ancestor_store[:lockdiscovery][-1][:timeout] = timeout_node
|
||||||
|
ancestor_store[:lockdiscovery]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def remove_lock(token)
|
def remove_lock(token)
|
||||||
@store.transaction do
|
@store.transaction do
|
||||||
@store.delete :lockcreator
|
@store.delete :lockcreator
|
||||||
@@ -535,8 +675,6 @@ module Calligraphy
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@lock_info = nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -28,14 +28,6 @@ module Calligraphy
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
# Responsible for returning a boolean value indicating if the resource
|
|
||||||
# can be copied.
|
|
||||||
#
|
|
||||||
# Used in COPY and MOVE (which inherits from COPY) requests.
|
|
||||||
def can_copy?(_options)
|
|
||||||
raise NotImplementedError
|
|
||||||
end
|
|
||||||
|
|
||||||
# Responsible for returning a boolean value indicating if the resource
|
# Responsible for returning a boolean value indicating if the resource
|
||||||
# is a collection.
|
# is a collection.
|
||||||
#
|
#
|
||||||
@@ -44,6 +36,18 @@ module Calligraphy
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Responsible for returning a hash with keys indicating if the resource
|
||||||
|
# can be copied, if an ancestor exists, or if the copy destinatin is
|
||||||
|
# locked.
|
||||||
|
#
|
||||||
|
# Return hash should contain `can_copy`, `ancestor_exist`, and `locked`
|
||||||
|
# keys with boolean values.
|
||||||
|
#
|
||||||
|
# Used in COPY and MOVE (which inherits from COPY) requests.
|
||||||
|
def copy_options(_options)
|
||||||
|
raise NotImplementedError
|
||||||
|
end
|
||||||
|
|
||||||
# Responsible for creating a duplicate of the resource in
|
# Responsible for creating a duplicate of the resource in
|
||||||
# `options[:destination]` (see section 9.8 of RFC4918).
|
# `options[:destination]` (see section 9.8 of RFC4918).
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -8,14 +8,14 @@ module Calligraphy
|
|||||||
# Executes the WebDAV request for a particular resource.
|
# Executes the WebDAV request for a particular resource.
|
||||||
def execute
|
def execute
|
||||||
options = copy_move_options
|
options = copy_move_options
|
||||||
can_copy = @resource.can_copy? options
|
copy_options = @resource.copy_options options
|
||||||
|
|
||||||
unless can_copy[:can_copy]
|
unless copy_options[:can_copy]
|
||||||
return :precondition_failed if can_copy[:ancestor_exist]
|
return :precondition_failed if copy_options[:ancestor_exist]
|
||||||
return :conflict
|
return :conflict
|
||||||
end
|
end
|
||||||
|
|
||||||
return :locked if can_copy[:locked]
|
return :locked if copy_options[:locked]
|
||||||
|
|
||||||
overwritten = @resource.copy options
|
overwritten = @resource.copy options
|
||||||
overwritten ? :no_content : :created
|
overwritten ? :no_content : :created
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ RSpec.describe 'Resource' do
|
|||||||
getetag getlastmodified lockdiscovery resourcetype supportedlock
|
getetag getlastmodified lockdiscovery resourcetype supportedlock
|
||||||
)
|
)
|
||||||
resource_methods_with_inputs = %w(
|
resource_methods_with_inputs = %w(
|
||||||
can_copy? copy lock locked_to_user? propfind proppatch unlock write
|
copy copy_options lock locked_to_user? propfind proppatch unlock write
|
||||||
)
|
)
|
||||||
|
|
||||||
resource_methods_without_inputs.each do |method|
|
resource_methods_without_inputs.each do |method|
|
||||||
|
|||||||
Reference in New Issue
Block a user