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

[#40228] implemented static open

Eric Schuberta8df00e61956
diff --git a/lib/api/utilities/endpoints/modify.rb b/lib/api/utilities/endpoints/modify.rb
index 33e62ffbd5e..29e424012e9 100644
--- a/lib/api/utilities/endpoints/modify.rb
+++ b/lib/api/utilities/endpoints/modify.rb
@@ -72,6 +72,7 @@ module API
:error_in_dependent,
dependent_class: result.model_name.human,
related_id: result.id,
+ # TODO: Make it more robust, as not every model has a 'name' attribute (e.g. fall back to collection index?)
related_subject: result.name,
error: full_message
)
diff --git a/modules/storages/app/contracts/storages/file_links/create_contract.rb b/modules/storages/app/contracts/storages/file_links/create_contract.rb
index 07dd17d1969..7c5275b267a 100644
--- a/modules/storages/app/contracts/storages/file_links/create_contract.rb
+++ b/modules/storages/app/contracts/storages/file_links/create_contract.rb
@@ -1,6 +1,6 @@
#-- copyright
# OpenProject is an open source project management software.
-# Copyright (C) 2012-2021 the OpenProject GmbH
+# Copyright (C) 2012-2022 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.
@@ -27,4 +27,11 @@
#++
class Storages::FileLinks::CreateContract < BaseContract
+ validate :validate_storage_url
+
+ private
+
+ def validate_storage_url
+ errors.add(:storage_id, :invalid) if model.storage_id.blank?
+ end
end
diff --git a/modules/storages/app/models/storages/file_link.rb b/modules/storages/app/models/storages/file_link.rb
index 305a8b36d0f..aea9202fc4a 100644
--- a/modules/storages/app/models/storages/file_link.rb
+++ b/modules/storages/app/models/storages/file_link.rb
@@ -37,4 +37,8 @@ class Storages::FileLink < ApplicationRecord
.references(:projects)
.merge(Project.allowed_to(user, :view_file_links))
}
+
+ def name
+ origin_name
+ end
end
diff --git a/modules/storages/app/services/storages/file_links/create_service.rb b/modules/storages/app/services/storages/file_links/create_service.rb
index c03d0e91035..60d2a457795 100644
--- a/modules/storages/app/services/storages/file_links/create_service.rb
+++ b/modules/storages/app/services/storages/file_links/create_service.rb
@@ -1,8 +1,6 @@
-#-- encoding: UTF-8
-
#-- copyright
# OpenProject is an open source project management software.
-# Copyright (C) 2012-2021 the OpenProject GmbH
+# Copyright (C) 2012-2022 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.
diff --git a/modules/storages/app/services/storages/file_links/set_attributes_service.rb b/modules/storages/app/services/storages/file_links/set_attributes_service.rb
index 1031abdbeb0..b9a76dc75a5 100644
--- a/modules/storages/app/services/storages/file_links/set_attributes_service.rb
+++ b/modules/storages/app/services/storages/file_links/set_attributes_service.rb
@@ -1,8 +1,6 @@
-#-- encoding: UTF-8
-
#-- copyright
# OpenProject is an open source project management software.
-# Copyright (C) 2012-2021 the OpenProject GmbH
+# Copyright (C) 2012-2022 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.
diff --git a/modules/storages/app/services/storages/storage_url_service.rb b/modules/storages/app/services/storages/storage_url_service.rb
new file mode 100644
index 00000000000..1caec3f16e9
--- /dev/null
+++ b/modules/storages/app/services/storages/storage_url_service.rb
@@ -0,0 +1,45 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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
+ class StorageUrlService
+ attr_reader :file_link
+
+ ACTION_TYPES = %w[open].freeze
+
+ def initialize(file_link)
+ @file_link = file_link
+ end
+
+ def call(action)
+ return ServiceResult.new(success: false, message: 'invalid action type') unless ACTION_TYPES.include?(action)
+
+ ServiceResult.new(success: true, result: "#{@file_link.storage.host}f/#{@file_link.origin_id}")
+ end
+ end
+end
diff --git a/modules/storages/lib/api/v3/file_links/file_link_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
index 3f39f89b036..09de1058d12 100644
--- a/modules/storages/lib/api/v3/file_links/file_link_representer.rb
+++ b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
@@ -76,6 +76,13 @@ module API
}
end
+ link :openLocation do
+ {
+ href: ::Storages::StorageUrlService.new(represented).call('open').result,
+ method: :get
+ }
+ end
+
link :staticOpenLocation do
{
href: api_v3_paths.file_link_open(represented.container_id, represented.id),
@@ -83,10 +90,17 @@ module API
}
end
- # TODO: add download and open location
-
associated_resource :storage
+ associated_resource :storageUrl,
+ skip_render: ->(*) { true },
+ getter: ->(*) {},
+ setter: ->(fragment:, **) {
+ # TODO: canonize url correctly
+ canonical_url = fragment['href']
+ represented.storage = ::Storages::Storage.find_by(host: canonical_url)
+ }
+
associated_resource :container,
v3_path: :work_package,
representer: ::API::V3::WorkPackages::WorkPackageRepresenter
diff --git a/modules/storages/lib/api/v3/file_links/file_links_open_api.rb b/modules/storages/lib/api/v3/file_links/file_links_open_api.rb
index ab885311102..a04eaca115f 100644
--- a/modules/storages/lib/api/v3/file_links/file_links_open_api.rb
+++ b/modules/storages/lib/api/v3/file_links/file_links_open_api.rb
@@ -32,7 +32,14 @@ module API
class FileLinksOpenAPI < ::API::OpenProjectAPI
resources :open do
get do
- raise ::API::Errors::NotImplemented
+ call = ::Storages::StorageUrlService.new(@file_link).call('open')
+ raise StandardError.new 'Cannot create opening url for linked file.' if call.failure?
+
+ url = call.result
+ status 303
+ header 'Location', url
+ content_type 'text/plain'
+ body "The requested resource can be viewed at #{url}"
end
end
end
diff --git a/modules/storages/lib/api/v3/file_links/parse_create_params_service.rb b/modules/storages/lib/api/v3/file_links/parse_create_params_service.rb
index 4e04bff74b8..fc3d1db59ab 100644
--- a/modules/storages/lib/api/v3/file_links/parse_create_params_service.rb
+++ b/modules/storages/lib/api/v3/file_links/parse_create_params_service.rb
@@ -1,6 +1,6 @@
#-- copyright
# OpenProject is an open source project management software.
-# Copyright (C) 2012-2021 the OpenProject GmbH
+# Copyright (C) 2012-2022 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.
diff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
index 6e28698a0f0..5d98312f6ac 100644
--- a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
@@ -96,7 +96,7 @@ describe 'API v3 file links resource', type: :request do
},
_links: {
storageUrl: {
- href: "https://nextcloud.deathstar.rocks/"
+ href: storage.host
}
}
},
@@ -112,7 +112,7 @@ describe 'API v3 file links resource', type: :request do
},
_links: {
storageUrl: {
- href: "https://nextcloud.deathstar.rocks/"
+ href: storage.host
}
}
}
@@ -126,14 +126,56 @@ describe 'API v3 file links resource', type: :request do
post path, params.to_json
end
- it 'returns ok created' do
- expect(response.status).to eq(201)
- end
-
it_behaves_like 'API V3 collection response', 2, 2, 'FileLink' do
let(:elements) { Storages::FileLink.all.order(id: :asc) }
let(:expected_status_code) { 201 }
end
+
+ context 'if the request body contains an invalid storage host' do
+ let(:params) do
+ {
+ _type: "Collection",
+ _embedded: {
+ elements: [
+ {
+ originData: {
+ id: 5503,
+ name: "logo.png",
+ mimeType: "image/png",
+ createdAt: "2021-12-19T09:42:10.170Z",
+ lastModifiedAt: "2021-12-20T14:00:13.987Z",
+ createdByName: "Luke Skywalker",
+ lastModifiedByName: "Anakin Skywalker"
+ },
+ _links: {
+ storageUrl: {
+ href: 'https://invalid.host.org/'
+ }
+ }
+ },
+ {
+ originData: {
+ id: 5503,
+ name: "logo2.png",
+ mimeType: "image/png",
+ createdAt: "2021-12-19T09:42:10.170Z",
+ lastModifiedAt: "2021-12-20T14:00:13.987Z",
+ createdByName: "Luke Skywalker",
+ lastModifiedByName: "Anakin Skywalker"
+ },
+ _links: {
+ storageUrl: {
+ href: storage.host
+ }
+ }
+ }
+ ]
+ }
+ }
+ end
+
+ it_behaves_like 'invalid request body'
+ end
end
describe 'GET /api/v3/work_packages/:work_package_id/file_links/:file_link_id' do
@@ -212,8 +254,20 @@ describe 'API v3 file links resource', type: :request do
get path
end
- it 'returns not implemented' do
- expect(subject.status).to be 501
+ it 'is successful' do
+ expect(subject.status).to be 303
+ end
+
+ context 'if user has no view permissions' do
+ let(:permissions) { %i(view_work_packages) }
+
+ it_behaves_like 'not found'
+ end
+
+ context 'if no storage with that id exists' do
+ let(:path) { api_v3_paths.file_link(work_package.id, 1337) }
+
+ it_behaves_like 'not found'
end
end
end