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

[#44875] Implement model and API representer for upload link

Andreas Pfohlb2a35cce31f9
diff --git a/modules/storages/app/models/storages/storage_file.rb b/modules/storages/app/models/storages/storage_file.rb
index 24e258d6deb..91c62198b6c 100644
--- a/modules/storages/app/models/storages/storage_file.rb
+++ b/modules/storages/app/models/storages/storage_file.rb
@@ -1,3 +1,31 @@
+#-- 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.
+#++
+
class Storages::StorageFile
attr_reader :id, :name, :size, :mime_type, :created_at, :last_modified_at, :created_by_name, :last_modified_by_name, :location
diff --git a/modules/storages/app/models/storages/upload_link.rb b/modules/storages/app/models/storages/upload_link.rb
new file mode 100644
index 00000000000..e57687727a0
--- /dev/null
+++ b/modules/storages/app/models/storages/upload_link.rb
@@ -0,0 +1,36 @@
+#-- 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.
+#++
+
+class Storages::UploadLink
+ attr_reader :destination, :finalize
+
+ def initialize(destination = '', finalize = nil)
+ @destination = destination
+ @finalize = finalize
+ end
+end
diff --git a/modules/storages/lib/api/v3/storage_files/storage_file_representer.rb b/modules/storages/lib/api/v3/storage_files/storage_file_representer.rb
index adbb49e5bf3..5f544777e16 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_file_representer.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_file_representer.rb
@@ -45,7 +45,7 @@ module API::V3::StorageFiles
property :location
def _type
- Storages::StorageFile.class.to_s
+ Storages::StorageFile.name.split('::').last
end
end
end
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
new file mode 100644
index 00000000000..edbc5f9a7c0
--- /dev/null
+++ b/modules/storages/lib/api/v3/storage_files/storage_upload_link_representer.rb
@@ -0,0 +1,55 @@
+#-- 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 API::V3::StorageFiles
+ class StorageUploadLinkRepresenter < ::API::Decorators::Single
+ link :self do
+ { href: "#{::API::V3::URN_PREFIX}storages:upload_link:no_link_provided" }
+ end
+
+ link :destination do
+ {
+ href: represented.destination,
+ method: :post
+ }
+ end
+
+ link :finalize do
+ next if represented.finalize.nil?
+
+ {
+ href: represented.finalize,
+ method: :post
+ }
+ end
+
+ def _type
+ Storages::UploadLink.name.split('::').last
+ end
+ end
+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
new file mode 100644
index 00000000000..c75d9db2d49
--- /dev/null
+++ b/modules/storages/spec/lib/api/v3/storage_files/storage_upload_link_representer_spec.rb
@@ -0,0 +1,93 @@
+#-- 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.
+#++
+
+require 'spec_helper'
+
+describe API::V3::StorageFiles::StorageUploadLinkRepresenter, 'rendering' do
+ include API::V3::Utilities::PathHelper
+
+ 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
+ let(:representer) { described_class.new(upload_link, current_user: user) }
+
+ subject { representer.to_json }
+
+ describe 'links' do
+ it { is_expected.to have_json_type(Object).at_path('_links') }
+
+ describe 'to self' do
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'self' }
+ let(:href) { "#{::API::V3::URN_PREFIX}storages:upload_link:no_link_provided" }
+ end
+ end
+
+ describe 'without finalize link' do
+ describe 'to destination' do
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'destination' }
+ let(:href) { destination }
+ end
+ end
+
+ describe 'not to finalize' do
+ it_behaves_like 'has no link' do
+ let(:link) { 'finalize' }
+ 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