Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Added more properties to WebDAV response parsing for Nextcloud connection

Andreas Pfohl72005cf2160a
diff --git a/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb b/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb
index f91f25d0ce9..e4b804b7782 100644
--- a/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb
+++ b/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb
@@ -4,17 +4,16 @@ module API::V3::Utilities::StorageInteraction
@uri = base_uri
@token = token
@token_refresh = token_refresh
+ @base_path = "/remote.php/dav/files/#{@token.origin_user_id}/"
end
def files
http = Net::HTTP.new(@uri.host, @uri.port)
http.use_ssl = @uri.scheme == 'https'
- path = "/remote.php/dav/files/#{@token.origin_user_id}/"
-
result = @token_refresh.call do
response = http.propfind(
- path,
+ @base_path,
nil,
{
'Depth' => '1',
@@ -29,31 +28,58 @@ module API::V3::Utilities::StorageInteraction
end
end
- parse_response(result, path)
+ parse_response(result)
end
private
- def parse_response(response, base)
+ def parse_response(response)
return response unless response.success
- document = Nokogiri::XML(response.result)
- elements = document.xpath('//d:href')
+ Nokogiri::XML(response.result)
+ .xpath('//d:response')
+ .drop(1) # drop current directory
+ .map { |file_element| storage_file(file_element) }
+ end
- elements.drop(1).map do |e|
- normalized_name = e.children.first.to_s.delete_prefix(base)
+ def storage_file(file_element)
+ name = name(file_element)
- ::Storages::StorageFile.new(
- nil,
- CGI.unescape(normalized_name),
- 'text/plain',
- nil,
- nil,
- nil,
- nil,
- "/#{normalized_name}"
- )
- end
+ ::Storages::StorageFile.new(
+ nil,
+ CGI.unescape(name),
+ mime_type(file_element),
+ nil,
+ last_modified_at(file_element),
+ nil,
+ nil,
+ "/#{name}"
+ )
+ end
+
+ def name(response)
+ response
+ .xpath('d:href')
+ .first
+ .inner_text
+ .delete_prefix(@base_path)
+ .delete_suffix('/')
+ end
+
+ def mime_type(response)
+ response
+ .xpath('.//d:getcontenttype')
+ .first
+ .inner_text
+ rescue NoMethodError
+ 'application/x-op-directory'
+ end
+
+ def last_modified_at(response)
+ response
+ .xpath('.//d:getlastmodified')
+ .map { |e| DateTime.parse(e) }
+ .first
end
end
end