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

[#44876] Implement API endpoint for creating upload link

Andreas Pfohl02d38aab0c21
diff --git a/modules/storages/app/common/storages/peripherals/storage_requests.rb b/modules/storages/app/common/storages/peripherals/storage_requests.rb
index 6c87d14c4a4..bb5bd439438 100644
--- a/modules/storages/app/common/storages/peripherals/storage_requests.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_requests.rb
@@ -45,6 +45,24 @@ module Storages::Peripherals
.map { |query| query.method(:query).to_proc }
end
+ # ToDo: Remove if implemented
+ # rubocop:disable Lint/UnusedMethodArgument
+ # rubocop:disable Lint/UnusedBlockArgument
+ def upload_link_query(user:, finalize_url:)
+ query_stub = ->(payload) do
+ ServiceResult.success(
+ result: Storages::UploadLink.new(
+ "https://example.com/upload/xyz123",
+ nil
+ )
+ )
+ end
+
+ ServiceResult.success(result: query_stub)
+ end
+ # rubocop:enable Lint/UnusedBlockArgument
+ # rubocop:enable Lint/UnusedMethodArgument
+
private
def storage_queries(user)
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 a346fd7a169..e715cf0c9c6 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
@@ -54,16 +54,44 @@ module API::V3::StorageFiles
files_query
.call(params[:parent])
.map do |files|
- API::V3::StorageFiles::StorageFileCollectionRepresenter.new(
- files,
- self_link: api_v3_paths.storage_files(@storage.id),
- current_user:
- )
- end
- .match(
- on_success: ->(representer) { representer },
- on_failure: ->(error) { raise_error(error) }
- )
+ API::V3::StorageFiles::StorageFileCollectionRepresenter.new(
+ files,
+ self_link: api_v3_paths.storage_files(@storage.id),
+ current_user:
+ )
+ end
+ .match(
+ on_success: ->(representer) { representer },
+ on_failure: ->(error) { raise_error(error) }
+ )
+ },
+ on_failure: ->(error) { raise_error(error) }
+ )
+ end
+
+ # RequestBody:
+ # {
+ # "fileName": "ape.png",
+ # "parent": "/Pictures"
+ # }
+ post :prepare_upload do
+ raise ::API::Errors::NotFound unless OpenProject::FeatureDecisions.storage_file_upload_active?
+
+ Storages::Peripherals::StorageRequests
+ .new(storage: @storage)
+ .upload_link_query(
+ user: current_user,
+ finalize_url: nil # ToDo: api_v3_paths.finalize_upload(@storage.id)
+ )
+ .match(
+ on_success: ->(upload_link_query) {
+ upload_link_query
+ .call(request_body)
+ .map { |link| API::V3::StorageFiles::StorageUploadLinkRepresenter.new(link, current_user:) }
+ .match(
+ on_success: ->(representer) { representer },
+ on_failure: ->(error) { raise_error(error) }
+ )
},
on_failure: ->(error) { raise_error(error) }
)
diff --git a/modules/storages/lib/api/v3/storage_files/storage_upload_link_representer.rb b/modules/storages/lib/api/v3/storage_files/storage_upload_link_representer.rb
index edbc5f9a7c0..82323e8c9e0 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_upload_link_representer.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_upload_link_representer.rb
@@ -35,7 +35,8 @@ module API::V3::StorageFiles
link :destination do
{
href: represented.destination,
- method: :post
+ method: :post,
+ title: 'Upload File'
}
end
@@ -43,8 +44,10 @@ module API::V3::StorageFiles
next if represented.finalize.nil?
{
- href: represented.finalize,
- method: :post
+ href: represented.finalize.destination,
+ method: :post,
+ title: 'Conclude file upload',
+ payload: represented.finalize.payload
}
end
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 4d27b93b836..6ce0f74292b 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -121,6 +121,10 @@ module OpenProject::Storages
"#{root}/storages/#{storage_id}/files"
end
+ add_api_path :prepare_upload do |storage_id|
+ "#{root}/storages/#{storage_id}/files/prepare_upload"
+ end
+
add_api_path :file_links do |work_package_id|
"#{work_package(work_package_id)}/file_links"
end
diff --git a/modules/storages/spec/lib/api/v3/storage_files/storage_upload_link_representer_spec.rb b/modules/storages/spec/lib/api/v3/storage_files/storage_upload_link_representer_spec.rb
index c75d9db2d49..133f1cb7d4e 100644
--- a/modules/storages/spec/lib/api/v3/storage_files/storage_upload_link_representer_spec.rb
+++ b/modules/storages/spec/lib/api/v3/storage_files/storage_upload_link_representer_spec.rb
@@ -34,7 +34,6 @@ describe API::V3::StorageFiles::StorageUploadLinkRepresenter, 'rendering' do
let(:user) { build_stubbed(:user) }
let(:token) { 'xyz123' }
let(:destination) { "https://example.com/upload/#{token}" }
- let(:finalize) { "https://example.com/upload/finalize/#{token}" }
let(:upload_link) do
Storages::UploadLink.new("https://example.com/upload/#{token}")
end
@@ -54,9 +53,10 @@ describe API::V3::StorageFiles::StorageUploadLinkRepresenter, 'rendering' do
describe 'without finalize link' do
describe 'to destination' do
- it_behaves_like 'has an untitled link' do
+ it_behaves_like 'has a titled link' do
let(:link) { 'destination' }
let(:href) { destination }
+ let(:title) { 'Upload File' }
end
end
@@ -66,28 +66,5 @@ describe API::V3::StorageFiles::StorageUploadLinkRepresenter, 'rendering' do
end
end
end
-
- describe 'with finalize link' do
- let(:upload_link) do
- Storages::UploadLink.new(
- "https://example.com/upload/#{token}",
- finalize
- )
- end
-
- describe 'to destination' do
- it_behaves_like 'has an untitled link' do
- let(:link) { 'destination' }
- let(:href) { destination }
- end
- end
-
- describe 'to finalize' do
- it_behaves_like 'has an untitled link' do
- let(:link) { 'finalize' }
- let(:href) { finalize }
- end
- end
- end
end
end
diff --git a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
index 375d1bd0078..53a917e76e1 100644
--- a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
@@ -278,4 +278,69 @@ describe 'API v3 storages resource', content_type: :json do
end
end
end
+
+ describe 'POST /api/v3/storages/:storage_id/files/prepare_upload', with_flag: { storage_file_upload: true } do
+ let(:path) { api_v3_paths.prepare_upload(storage.id) }
+ let(:upload_link) { Storages::UploadLink.new('https://example.com/upload/xyz123') }
+ let(:body) { { fileName: "ape.png", parent: "/Pictures" }.to_json }
+
+ subject(:last_response) do
+ post(path, body)
+ end
+
+ describe 'with successful response' do
+ before do
+ storage_requests = instance_double(Storages::Peripherals::StorageRequests)
+ uplaod_link_query = Proc.new { ServiceResult.success(result: upload_link) }
+ allow(storage_requests).to receive(:upload_link_query).and_return(ServiceResult.success(result: uplaod_link_query))
+ allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)
+ end
+
+ subject { last_response.body }
+
+ it { is_expected.to be_json_eql(Storages::UploadLink.name.split('::').last.to_json).at_path('_type') }
+
+ it do
+ expect(subject)
+ .to(be_json_eql("#{::API::V3::URN_PREFIX}storages:upload_link:no_link_provided".to_json)
+ .at_path('_links/self/href'))
+ end
+
+ it { is_expected.to be_json_eql(upload_link.destination.to_json).at_path('_links/destination/href') }
+ it { is_expected.to be_json_eql("post".to_json).at_path('_links/destination/method') }
+ it { is_expected.to be_json_eql("Upload File".to_json).at_path('_links/destination/title') }
+ end
+
+ describe 'with files query creation failed' do
+ let(:storage_requests) { instance_double(Storages::Peripherals::StorageRequests) }
+
+ before do
+ allow(Storages::Peripherals::StorageRequests).to receive(:new).and_return(storage_requests)
+ end
+
+ describe 'due to authorization failure' do
+ before do
+ allow(storage_requests).to receive(:upload_link_query).and_return(ServiceResult.failure(result: :not_authorized))
+ end
+
+ it { expect(last_response.status).to be(500) }
+ end
+
+ describe 'due to internal error' do
+ before do
+ allow(storage_requests).to receive(:upload_link_query).and_return(ServiceResult.failure(result: :error))
+ end
+
+ it { expect(last_response.status).to be(500) }
+ end
+
+ describe 'due to not found' do
+ before do
+ allow(storage_requests).to receive(:upload_link_query).and_return(ServiceResult.failure(result: :not_found))
+ end
+
+ it { expect(last_response.status).to be(404) }
+ end
+ end
+ end
end