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

[#47207] Add storage file endpoint

Eric Schubert5e1f7acee91d
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/files_query_helpers.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/files_query_helpers.rb
index d8607cc36d7..596db2caaa4 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/files_query_helpers.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/files_query_helpers.rb
@@ -34,8 +34,14 @@ module Storages::Peripherals::StorageInteraction
.files_query(user:)
end
- def execute_files_query(parent)
- ->(query) { query.call(parent) }
+ def file_query(storage, user)
+ Storages::Peripherals::StorageRequests
+ .new(storage:)
+ .file_query(user:)
+ end
+
+ def execute_files_query(data)
+ ->(query) { query.call(data) }
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb
new file mode 100644
index 00000000000..17b75b70fb0
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb
@@ -0,0 +1,99 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Storages::Peripherals::StorageInteraction::Nextcloud
+ class FileQuery < Storages::Peripherals::StorageInteraction::StorageQuery
+ include API::V3::Utilities::PathHelper
+ using Storages::Peripherals::ServiceResultRefinements
+
+ FILE_INFO_PATH = 'ocs/v1.php/apps/integration_openproject/fileinfo'.freeze
+
+ def initialize(base_uri:, token:, retry_proc:)
+ super()
+
+ @base_uri = base_uri
+ @token = token
+ @retry_proc = retry_proc
+ end
+
+ def query(file_id)
+ file_info(file_id) >>
+ method(:to_storage_file)
+ end
+
+ private
+
+ # rubocop:disable Metrics/AbcSize
+ def file_info(file_id)
+ @retry_proc.call(@token) do |token|
+ begin
+ service_result = ServiceResult.success(
+ result: RestClient::Request.execute(
+ method: :get,
+ url: api_v3_paths.join_uri_path(@base_uri, FILE_INFO_PATH, file_id),
+ headers: {
+ 'Authorization' => "Bearer #{token.access_token}",
+ 'Accept' => 'application/json',
+ 'Content-Type' => 'application/json'
+ }
+ )
+ )
+ rescue RestClient::Unauthorized => e
+ service_result = error(:not_authorized, 'Outbound request not authorized!', e.response)
+ rescue RestClient::NotFound => e
+ service_result = error(:not_found, 'Outbound request destination not found!', e.response)
+ rescue RestClient::ExceptionWithResponse => e
+ service_result = error(:error, 'Outbound request failed!', e.response)
+ rescue StandardError
+ service_result = error(:error, 'Outbound request failed!')
+ end
+
+ # rubocop:disable Style/OpenStructUse
+ service_result.map { |response| JSON.parse(response.body, object_class: OpenStruct) }
+ # rubocop:enable Style/OpenStructUse
+ end
+ end
+
+ # rubocop:enable Metrics/AbcSize
+
+ def to_storage_file(file_info_response)
+ data = file_info_response.ocs.data
+ storage_file = ::Storages::StorageFile.new(data.id,
+ data.name,
+ data.size,
+ data.mimetype,
+ Time.zone.at(data.ctime),
+ Time.zone.at(data.mtime),
+ data.owner_name,
+ data.modifier_name,
+ nil,
+ nil)
+ ServiceResult.success(result: storage_file)
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb
index 02e4eeee36f..7e98d772b93 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb
@@ -90,6 +90,21 @@ module Storages::Peripherals::StorageInteraction
end
end
+ def file_query
+ case @provider_type
+ when ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD
+ retry_with_refreshed_token do |token, with_refreshed_token_proc|
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::FileQuery.new(
+ base_uri: @uri,
+ token:,
+ retry_proc: with_refreshed_token_proc
+ )
+ end
+ else
+ raise ArgumentError
+ end
+ end
+
private
def retry_with_refreshed_token
diff --git a/modules/storages/app/common/storages/peripherals/storage_requests.rb b/modules/storages/app/common/storages/peripherals/storage_requests.rb
index 542e7922f6f..b737f7c3924 100644
--- a/modules/storages/app/common/storages/peripherals/storage_requests.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_requests.rb
@@ -45,6 +45,12 @@ module Storages::Peripherals
.map { |query| query.method(:query).to_proc }
end
+ def file_query(user:)
+ storage_queries(user)
+ .file_query
+ .map { |query| query.method(:query).to_proc }
+ end
+
def upload_link_query(user:)
storage_queries(user)
.upload_link_query
diff --git a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
index e6fa20cfa7a..753251eca2d 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
@@ -42,6 +42,16 @@ module API::V3::StorageFiles
)
end
+ route_param :file_id, type: String, desc: 'Storage file id' do
+ get do
+ (file_query(@storage, current_user) >> execute_files_query(params[:file_id]))
+ .match(
+ on_success: ->(storage_file) { API::V3::StorageFiles::StorageFileRepresenter.new(storage_file, current_user:) },
+ on_failure: ->(error) { raise_error(error) }
+ )
+ end
+ end
+
post :prepare_upload do
(upload_link_query(@storage, current_user) >> execute_upload_link_query(request_body))
.match(