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

[#47121] Add breadcrumb information to storage files endpoint

Eric Schubertcad632f3fc91
diff --git a/docs/api/apiv3/components/schemas/storage_files_model.yml b/docs/api/apiv3/components/schemas/storage_files_model.yml
index b0294ed4dd6..457675d1819 100644
--- a/docs/api/apiv3/components/schemas/storage_files_model.yml
+++ b/docs/api/apiv3/components/schemas/storage_files_model.yml
@@ -5,6 +5,7 @@ required:
- _type
- files
- parent
+ - ancestors
- _links
properties:
_type:
@@ -20,6 +21,21 @@ properties:
allOf:
- $ref: './storage_file_model.yml'
- description: File of the currently selected parent directory.
+ ancestors:
+ type: array
+ items:
+ type: object
+ description: List of ancestors of the parent directory. Can be empty, if parent directory was root directory.
+ required:
+ - name
+ - location
+ properties:
+ name:
+ type: string
+ description: The name of the ancestor file in storage
+ location:
+ type: string
+ description: Location identification for file in storage
_links:
type: object
required:
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
index 88cea3ac198..4d11c8dda0d 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
@@ -86,6 +86,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
end.to_xml
end
+
# rubocop:enable Metrics/AbcSize
def error(response)
@@ -117,17 +118,41 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
storage_file(file_element)
end
- ::Storages::StorageFiles.new(files, parent)
+ ::Storages::StorageFiles.new(files, parent, ancestors(parent.location))
+ end
+ end
+
+ def ancestors(parent_location)
+ path = parent_location.split('/')
+ return [] if path.count == 0
+
+ path.take(path.count - 1).reduce([]) do |list, item|
+ last = list.last
+ prefix = last.nil? || last.location[-1] != '/' ? '/' : ''
+ location = "#{last&.location}#{prefix}#{item}"
+ list.append(::Storages::StorageFile.new(nil,
+ name(location),
+ nil,
+ nil,
+ nil,
+ nil,
+ nil,
+ nil,
+ location,
+ nil))
end
end
+ def name(location)
+ location == '/' ? location : CGI.unescape(location.split('/').last)
+ end
+
def storage_file(file_element)
- location = name(file_element)
- name = location == '/' ? location : CGI.unescape(location.split('/').last)
+ location = location(file_element)
::Storages::StorageFile.new(
id(file_element),
- name,
+ name(location),
size(file_element),
mime_type(file_element),
nil,
@@ -147,7 +172,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
.first
end
- def name(element)
+ def location(element)
texts = element
.xpath('d:href')
.map(&:inner_text)
diff --git a/modules/storages/app/models/storages/storage_files.rb b/modules/storages/app/models/storages/storage_files.rb
index 24e7a91b607..cf090ccbb9f 100644
--- a/modules/storages/app/models/storages/storage_files.rb
+++ b/modules/storages/app/models/storages/storage_files.rb
@@ -27,10 +27,11 @@
#++
class Storages::StorageFiles
- attr_reader :files, :parent
+ attr_reader :files, :parent, :ancestors
- def initialize(files, parent = nil)
+ def initialize(files, parent, ancestors)
@files = files
@parent = parent
+ @ancestors = ancestors
end
end
diff --git a/modules/storages/lib/api/v3/storage_files/storage_files_representer.rb b/modules/storages/lib/api/v3/storage_files/storage_files_representer.rb
index 019f6acc8e9..e460382a9b2 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_files_representer.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_files_representer.rb
@@ -42,6 +42,12 @@ module API::V3::StorageFiles
getter: ->(*) { API::V3::StorageFiles::StorageFileRepresenter.new(represented.parent, current_user:) },
exec_context: :decorator
+ collection :ancestors,
+ getter: ->(*) do
+ represented.ancestors.map { |file| API::V3::StorageFiles::StorageFileRepresenter.new(file, current_user:) }
+ end,
+ exec_context: :decorator
+
def _type
'StorageFiles'
end
diff --git a/modules/storages/spec/common/peripherals/storage_requests_spec.rb b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
index 71024a547ad..936d5209997 100644
--- a/modules/storages/spec/common/peripherals/storage_requests_spec.rb
+++ b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
@@ -205,6 +205,7 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
result = query.call(nil)
expect(result).to be_success
expect(result.result.files.size).to eq(4)
+ expect(result.result.ancestors.size).to eq(0)
expect(result.result.parent).not_to be_nil
end,
on_failure: ->(error) do
@@ -320,6 +321,8 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
on_success: ->(query) {
result = query.call(parent)
expect(result.result.files[2].location).to eq('/Photos/Birds/README.md')
+ expect(result.result.ancestors[0].location).to eq('/')
+ expect(result.result.ancestors[1].location).to eq('/Photos')
},
on_failure: ->(error) { raise "Files query could not be created: #{error}" }
)
diff --git a/modules/storages/spec/lib/api/v3/storage_files/storage_files_representer_spec.rb b/modules/storages/spec/lib/api/v3/storage_files/storage_files_representer_spec.rb
index ddc7479bf93..5b31f1dcb8a 100644
--- a/modules/storages/spec/lib/api/v3/storage_files/storage_files_representer_spec.rb
+++ b/modules/storages/spec/lib/api/v3/storage_files/storage_files_representer_spec.rb
@@ -36,14 +36,14 @@ describe API::V3::StorageFiles::StorageFilesRepresenter do
let(:parent) do
Storages::StorageFile.new(
23,
- '/',
+ 'Documents',
2048,
'application/x-op-directory',
created_at,
last_modified_at,
'admin',
'admin',
- '/',
+ '/Documents',
%i[readable writeable]
)
end
@@ -58,13 +58,28 @@ describe API::V3::StorageFiles::StorageFilesRepresenter do
last_modified_at,
'admin',
'admin',
- '/readme.md',
+ '/Documents/readme.md',
+ %i[readable writeable]
+ )
+ end
+
+ let(:ancestor) do
+ Storages::StorageFile.new(
+ 47,
+ '/',
+ 4096,
+ 'application/x-op-directory',
+ created_at,
+ last_modified_at,
+ 'admin',
+ 'admin',
+ '/',
%i[readable writeable]
)
end
let(:files) do
- Storages::StorageFiles.new([file], parent)
+ Storages::StorageFiles.new([file], parent, [ancestor])
end
let(:representer) { described_class.new(files, current_user: user) }
@@ -81,6 +96,11 @@ describe API::V3::StorageFiles::StorageFilesRepresenter do
let(:element_decorator) { API::V3::StorageFiles::StorageFileRepresenter }
end
+ it_behaves_like 'collection', :ancestors do
+ let(:value) { files.ancestors }
+ let(:element_decorator) { API::V3::StorageFiles::StorageFileRepresenter }
+ end
+
it_behaves_like 'property', :parent do
let(:value) { API::V3::StorageFiles::StorageFileRepresenter.new(files.parent, current_user: user) }
end
diff --git a/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb b/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb
index a9b36f827e6..71985d1b6d5 100644
--- a/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb
@@ -71,7 +71,8 @@ describe 'API v3 storage files', content_type: :json, webmock: true do
'Obi-Wan Kenobi', 'Obi-Wan Kenobi', '/', %i[readable writeable])
],
Storages::StorageFile.new(32, '/', 4096 * 2, 'application/x-op-directory', DateTime.now, DateTime.now,
- 'Obi-Wan Kenobi', 'Obi-Wan Kenobi', '/', %i[readable writeable])
+ 'Obi-Wan Kenobi', 'Obi-Wan Kenobi', '/', %i[readable writeable]),
+ []
)
end
@@ -97,6 +98,8 @@ describe 'API v3 storage files', content_type: :json, webmock: true do
it { is_expected.to be_json_eql(response.parent.id.to_json).at_path('parent/id') }
it { is_expected.to be_json_eql(response.parent.name.to_json).at_path('parent/name') }
+
+ it { is_expected.to be_json_eql(response.ancestors.to_json).at_path('ancestors') }
end
describe 'with files query creation failed' do