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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add prepare service and contract
Oliver Günther287111c574c0
diff --git a/app/contracts/attachments/create_contract.rb b/app/contracts/attachments/create_contract.rbindex d8a23e7c227..f7eed10e0b4 100644--- a/app/contracts/attachments/create_contract.rb+++ b/app/contracts/attachments/create_contract.rb@@ -37,6 +37,7 @@ module Attachmentsattribute :descriptionattribute :content_typeattribute :container+ attribute :container_typeattribute :authorvalidate :validate_attachments_addablediff --git a/app/contracts/attachments/prepare_upload_contract.rb b/app/contracts/attachments/prepare_upload_contract.rbnew file mode 100644index 00000000000..456c2eb0ab0--- /dev/null+++ b/app/contracts/attachments/prepare_upload_contract.rb@@ -0,0 +1,43 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.+#++++module Attachments+ class PrepareUploadContract < CreateContract+ validate :validate_direct_uploads_active+ # prepared uploads require a filesize to be present+ validates :filesize, presence: true++ private++ def validate_direct_uploads_active+ errors.add :base, :not_available unless OpenProject::Configuration.direct_uploads?+ end+ end+enddiff --git a/app/models/attachment.rb b/app/models/attachment.rbindex 15f32c7810e..5d24495e59f 100644--- a/app/models/attachment.rb+++ b/app/models/attachment.rb@@ -47,8 +47,8 @@ class Attachment < ApplicationRecordacts_as_journalizedacts_as_event title: -> { file.name },url: (Proc.new do |o|- { controller: '/attachments', action: 'download', id: o.id, filename: o.filename }- end)+ { controller: '/attachments', action: 'download', id: o.id, filename: o.filename }+ end)mount_uploader :file, OpenProject::Configuration.file_uploader@@ -255,37 +255,6 @@ class Attachment < ApplicationRecordwhere(digest: "", downloads: -1)end- def self.create_pending_direct_upload(file_name:, author:, container: nil, content_type: nil, file_size: 0)- a = create(- container: container,- author: author,- content_type: content_type.presence || "application/octet-stream",- filesize: file_size,- digest: "",- downloads: -1- )-- # We need to do it like this because `file` is an uploader which expects a File (not a string)- # to upload usually. But in this case the data has already been uploaded and we just point to it.- a[:file] = pending_direct_upload_filename(file_name)-- a.save!- a.reload # necessary so that the fog file uploader path is correct-- a- end-- class << self- private-- # The name has to be in the same format as what Carrierwave will produce later on. If they are different,- # Carrierwave will alter the name (both local and remote) whenever the attachment is saved with the remote- # file loaded.- def pending_direct_upload_filename(file_name)- CarrierWave::SanitizedFile.new(nil).send(:sanitize, file_name)- end- end-def pending_direct_upload?digest == "" && downloads == -1enddiff --git a/app/services/attachments/create_service.rb b/app/services/attachments/create_service.rbindex 772b5ef3f2d..33f00f1586c 100644--- a/app/services/attachments/create_service.rb+++ b/app/services/attachments/create_service.rb@@ -68,7 +68,6 @@ class Attachments::CreateService < ::BaseServices::Createdef error_wrapped_callyieldrescue StandardError => e- binding.prylog_attachment_saving_error(e)message =diff --git a/app/services/attachments/prepare_upload_service.rb b/app/services/attachments/prepare_upload_service.rbnew file mode 100644index 00000000000..0f7a9546d65--- /dev/null+++ b/app/services/attachments/prepare_upload_service.rb@@ -0,0 +1,64 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.+#++++class Attachments::PrepareUploadService < ::BaseServices::Create+ def instance(params)+ binding.pry+ super(params).tap do |attachment|+ # We need to do it like this because `file` is an uploader which expects a File (not a string)+ # to upload usually. But in this case the data has already been uploaded and we just point to it.+ attachment[:file] = pending_direct_upload_filename(params[:filename])++ attachment.extend(OpenProject::ChangedBySystem)+ attachment.change_by_system do+ attachment.downloads = -1+ attachment.content_type = params[:content_type] || 'application/octet-stream'+ end+ end+ end++ def persist(call)+ attachment = call.result++ if attachment.save+ attachment.reload # necessary so that the fog file uploader path is correct+ ServiceResult.new success: true, result: attachment+ else+ ServiceResult.new success: false, result: attachment+ end+ end++ private++ # The name has to be in the same format as what Carrierwave will produce later on. If they are different,+ # Carrierwave will alter the name (both local and remote) whenever the attachment is saved with the remote+ # file loaded.+ def pending_direct_upload_filename(filename)+ CarrierWave::SanitizedFile.new(nil).send(:sanitize, filename)+ end+enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 0d039307f17..d5f11428190 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -617,6 +617,7 @@ en:invalid_url: 'is not a valid URL.'invalid_url_scheme: 'is not a supported protocol (allowed: %{allowed_schemes}).'less_than_or_equal_to: "must be less than or equal to %{count}."+ not_available: "is not available due to a system configuration."not_deletable: "cannot be deleted."not_current_user: "is not the current user."not_a_date: "is not a valid date."diff --git a/lib/api/v3/attachments/attachments_by_container_api.rb b/lib/api/v3/attachments/attachments_by_container_api.rbindex 089be45c488..c9b76afede7 100644--- a/lib/api/v3/attachments/attachments_by_container_api.rb+++ b/lib/api/v3/attachments/attachments_by_container_api.rb@@ -47,26 +47,6 @@ module APIrequest.env['REQUEST_METHOD'] == 'POST'end- def parse_and_prepare- metadata = nil-- unless metadata- raise ::API::Errors::InvalidRequestBody.new(I18n.t('api_v3.errors.multipart_body_error'))- end-- create_attachment metadata- end-- def create_attachment(metadata)- Attachment.create_pending_direct_upload(- file_name: metadata.file_name,- container: container,- author: current_user,- content_type: metadata.content_type,- file_size: metadata.file_size- )- end-### Additionally to what would be checked by the contract,# we need to restrict permissions in some use cases of the mounts of this endpoint.@@ -74,12 +54,6 @@ module APIauthorize_any(permissions, projects: container.project) unless permissions.empty?end- def require_direct_uploads- unless OpenProject::Configuration.direct_uploads?- raise API::Errors::NotFound, message: "Only available if direct uploads are enabled."- end- end-def parse_multipart(request)request.params.tap do |params|params[:metadata] = JSON.parse(params[:metadata]) if params.key?(:metadata)@@ -113,14 +87,14 @@ module APIdef self.prepare(permissions = [])-> do- require_direct_uploadsrestrict_permissions permissionsinstance_exec &::API::V3::Utilities::Endpoints::Create.new(model: ::Attachment,parse_representer: AttachmentParsingRepresenter,- process_service: nil, # TODO prepare service- params_getter: ->(request) { request.params },+ process_service: ::Attachments::PrepareUploadService,+ process_contract: ::Attachments::PrepareUploadContract,+ params_getter: method(:parse_multipart),params_modifier: ->(params) doparams.merge(container: container)end)