Class: Calligraphy::FileResource
Constant Summary
Constants included
from Utils
Utils::FALSE_VALUES, Utils::TRUE_VALUES
Instance Attribute Summary
Attributes inherited from Resource
#client_nonce, #contents, #full_request_path, #mount_point, #request_body, #request_path, #root_dir, #updated_at
Instance Method Summary
collapse
Methods included from Utils
#extract_lock_token, #is_false?, #is_true?, #join_paths, #map_array_of_hashes, #obj_exists_and_is_not_type?, #split_and_pop
Methods inherited from Resource
#readable?
Constructor Details
#initialize(resource: nil, req: nil, mount: nil, root_dir: Dir.pwd) ⇒ FileResource
Returns a new instance of FileResource.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/calligraphy/resource/file_resource.rb', line 7
def initialize(resource: nil, req: nil, mount: nil, root_dir: Dir.pwd)
super
@root_dir = root_dir || Dir.pwd
@src_path = join_paths @root_dir, @request_path
if exists?
@name = File.basename @src_path
init_pstore
set_file_stats
end
set_ancestors
end
|
Instance Method Details
#ancestor_exist? ⇒ Boolean
22
23
24
|
# File 'lib/calligraphy/resource/file_resource.rb', line 22
def ancestor_exist?
File.exist? @ancestor_path
end
|
#can_copy?(options) ⇒ Boolean
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/calligraphy/resource/file_resource.rb', line 26
def can_copy?(options)
copy_options = { can_copy: false, ancestor_exist: false, locked: false }
overwrite = is_true? options[:overwrite]
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
|
#collection? ⇒ Boolean
61
62
63
|
# File 'lib/calligraphy/resource/file_resource.rb', line 61
def collection?
File.directory? @src_path
end
|
#copy(options) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/calligraphy/resource/file_resource.rb', line 65
def copy(options)
destination = options[:destination].tap { |s| s.slice! @mount_point }
preserve_existing = is_false? options[:overwrite]
to_path = join_paths @root_dir, destination
to_path_exists = File.exist? to_path
if collection?
FileUtils.cp_r @src_path, to_path, preserve: preserve_existing
else
FileUtils.cp @src_path, to_path, preserve: preserve_existing
end
if 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
to_path_exists
end
|
#create_collection ⇒ Object
88
89
90
|
# File 'lib/calligraphy/resource/file_resource.rb', line 88
def create_collection
Dir.mkdir @src_path
end
|
#delete_collection ⇒ Object
92
93
94
95
|
# File 'lib/calligraphy/resource/file_resource.rb', line 92
def delete_collection
FileUtils.rm_r @src_path
FileUtils.rm_r @store_path if store_exist?
end
|
#etag ⇒ Object
97
98
99
|
# File 'lib/calligraphy/resource/file_resource.rb', line 97
def etag
[@updated_at.to_i, @stats[:inode], @stats[:size]].join('-').to_s
end
|
#exists? ⇒ Boolean
101
102
103
|
# File 'lib/calligraphy/resource/file_resource.rb', line 101
def exists?
File.exist? @src_path
end
|
#lock(nodes, depth = INFINITY) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/calligraphy/resource/file_resource.rb', line 105
def lock(nodes, depth=INFINITY)
properties = {}
nodes.each do |node|
next unless node.is_a? Nokogiri::XML::Element
properties[node.name.to_sym] = node
end
unless exists?
write ''
@name = File.basename @src_path
init_pstore
end
create_lock properties, depth
end
|
#lock_is_exclusive? ⇒ Boolean
122
123
124
|
# File 'lib/calligraphy/resource/file_resource.rb', line 122
def lock_is_exclusive?
lockscope == 'exclusive'
end
|
#lock_tokens ⇒ Object
126
127
128
129
|
# File 'lib/calligraphy/resource/file_resource.rb', line 126
def lock_tokens
get_lock_info
@lock_info&.each { |x| x }&.map { |k, v| k[:locktoken].children[0].text }
end
|
#locked? ⇒ Boolean
131
132
133
134
|
# File 'lib/calligraphy/resource/file_resource.rb', line 131
def locked?
get_lock_info
obj_exists_and_is_not_type? obj: @lock_info, type: []
end
|
#locked_to_user?(headers = nil) ⇒ Boolean
136
137
138
139
140
141
142
|
# File 'lib/calligraphy/resource/file_resource.rb', line 136
def locked_to_user?(=nil)
if locked?
!can_unlock?
else
locking_ancestor? @ancestor_path, @ancestors.dup,
end
end
|
#propfind(nodes) ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/calligraphy/resource/file_resource.rb', line 144
def propfind(nodes)
properties = { found: [], not_found: [] }
nodes.each do |node|
node.children.each do |prop|
next unless prop.is_a? Nokogiri::XML::Element
value = get_property prop
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
end
properties
end
|
#proppatch(nodes) ⇒ Object
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/calligraphy/resource/file_resource.rb', line 168
def proppatch(nodes)
actions = { set: [], remove: [] }
@store.transaction do
@store[:properties] = {} unless @store[:properties].is_a? Hash
nodes.each do |node|
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
get_custom_property nil
actions
end
|
#read ⇒ Object
217
218
219
|
# File 'lib/calligraphy/resource/file_resource.rb', line 217
def read
@contents ||= File.read @src_path if readable?
end
|
#refresh_lock ⇒ Object
221
222
223
224
225
226
227
228
229
230
231
|
# File 'lib/calligraphy/resource/file_resource.rb', line 221
def refresh_lock
if locked?
@store.transaction do
@store[:lockdiscovery][-1][:timeout] = timeout_node
end
get_lock_info
else
refresh_ancestor_locks @ancestor_path, @ancestors.dup
end
end
|
#unlock(token) ⇒ Object
233
234
235
236
237
238
239
240
|
# File 'lib/calligraphy/resource/file_resource.rb', line 233
def unlock(token)
if lock_tokens.include? token
remove_lock token
:no_content
else
:forbidden
end
end
|
#write(contents = @request_body.to_s) ⇒ Object
242
243
244
245
246
247
248
|
# File 'lib/calligraphy/resource/file_resource.rb', line 242
def write(contents=@request_body.to_s)
@contents = contents
File.open(@src_path, 'w') do |file|
file.write @contents
end
end
|