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

use a BaseContracted service for finishing attachment upload

ulferts31573972b0e9
diff --git a/app/services/attachments/finish_direct_upload_service.rb b/app/services/attachments/finish_direct_upload_service.rb
new file mode 100644
index 00000000000..5eb3a15ed9d
--- /dev/null
+++ b/app/services/attachments/finish_direct_upload_service.rb
@@ -0,0 +1,142 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 Attachments
+ class FinishDirectUploadService < BaseServices::BaseContracted
+ def initialize(user:, model:, contract_class: nil, contract_options: {})
+ self.model = model
+ super(user:, contract_class:, contract_options:)
+ end
+
+ protected
+
+ alias_method :attachment, :model
+
+ def service_context(send_notifications:, &)
+ # Overwriting the call to in_context to place the semaphore on the container and not on the attachment.
+ # Since the service will write a journal on the container, there should not be another
+ # process that modifies the container while this service is running.
+ in_context(attachment.container, send_notifications:, &)
+ end
+
+ def validate_params(_params)
+ super.tap do |call|
+ validate_local_file_exists(call)
+ end
+ end
+
+ def before_perform(*)
+ super.tap do
+ set_attachment_parameters
+ end
+ end
+
+ def persist(call)
+ super.tap do
+ attachment.save!
+ end
+ end
+
+ def after_perform(service_call)
+ super.tap do
+ journalize_container
+ attachment_created_event
+ schedule_jobs
+ end
+ end
+
+ def validate_local_file_exists(call)
+ unless local_file
+ call.errors.add(:base, "File for attachment #{attachment.filename} was not uploaded.")
+ call.success = false
+ end
+ end
+
+ def set_attachment_parameters
+ attachment.extend(OpenProject::ChangedBySystem)
+ attachment.change_by_system do
+ attachment.status = :uploaded
+ attachment.set_file_size local_file
+ attachment.set_content_type local_file
+ attachment.set_digest local_file
+ end
+ end
+
+ def schedule_jobs
+ attachment.extract_fulltext
+ end
+
+ def journalize_container
+ journable = attachment.container
+
+ return unless journable&.class&.journaled?
+
+ # Touching the journable will lead to the journal created next having its own timestamp.
+ # That timestamp will not adequately reflect the time the attachment was uploaded. This job
+ # right here might be executed way later than the time the attachment was uploaded. Ideally,
+ # the journals would be created bearing the time stamps of the attachment's created_at.
+ # This remains a TODO.
+ # But with the timestamp update in place as it is, at least the collapsing of aggregated journals
+ # from days before with the newly uploaded attachment is prevented.
+ touch_journable(journable)
+
+ Journals::CreateService
+ .new(journable, attachment.author)
+ .call
+ end
+
+ def touch_journable(journable)
+ # Not using touch here on purpose,
+ # as to avoid changing lock versions on the journables for this change
+ attributes = journable.send(:timestamp_attributes_for_update_in_model)
+
+ timestamps = attributes.index_with { Time.current }
+ journable.update_columns(timestamps) if timestamps.any?
+ end
+
+ def attachment_created_event
+ OpenProject::Notifications.send(
+ OpenProject::Events::ATTACHMENT_CREATED,
+ attachment:
+ )
+ end
+
+ def default_contract_class
+ ::Attachments::CreateContract
+ end
+
+ def local_file
+ # An attachment is guaranteed to have a file.
+ # But if the attachment is nil the expression attachment&.file will be nil and attachment&.file.local_file
+ # will throw a NoMethodError: undefined method local_file' for nil:NilClass`.
+ attachment&.file&.local_file
+ end
+ end
+end
diff --git a/app/workers/attachments/finish_direct_upload_job.rb b/app/workers/attachments/finish_direct_upload_job.rb
index 18865d4cc3d..0438b1b61e3 100644
--- a/app/workers/attachments/finish_direct_upload_job.rb
+++ b/app/workers/attachments/finish_direct_upload_job.rb
@@ -29,115 +29,44 @@
class Attachments::FinishDirectUploadJob < ApplicationJob
queue_with_priority :high
- def perform(attachment_id, whitelist: true)
+ def perform(attachment_id, allowlist: true)
attachment = Attachment.pending_direct_upload.find_by(id: attachment_id)
- # An attachment is guaranteed to have a file.
- # But if the attachment is nil the expression attachment&.file will be nil and attachment&.file.local_file
- # will throw a NoMethodError: undefined method local_file' for nil:NilClass`.
- local_file = attachment && attachment.file.local_file
- if local_file.nil?
- return Rails.logger.error("File for attachment #{attachment_id} was not uploaded.")
- end
-
- User.execute_as(attachment.author) do
- attach_uploaded_file(attachment, local_file, whitelist)
+ Attachments::FinishDirectUploadService
+ .new(user: attachment.author, model: attachment, contract_options: derive_contract_options(allowlist))
+ .call
+ .on_failure do |call|
+ destroy_attachment_and_log_errors(attachment, call.errors)
end
end
private
- def attach_uploaded_file(attachment, local_file, whitelist)
- set_attributes_from_file(attachment, local_file)
- validate_attachment(attachment, whitelist)
- save_attachment(attachment)
- journalize_container(attachment)
- attachment_created_event(attachment)
- schedule_jobs(attachment)
- rescue StandardError => e
- ::OpenProject.logger.error e
+ def destroy_attachment_and_log_errors(attachment, errors)
attachment.destroy
- ensure
- FileUtils.rm_rf(local_file.path)
- end
-
- def set_attributes_from_file(attachment, local_file)
- attachment.extend(OpenProject::ChangedBySystem)
- attachment.change_by_system do
- attachment.status = :uploaded
- attachment.set_file_size local_file
- attachment.set_content_type local_file
- attachment.set_digest local_file
- end
- end
-
- def save_attachment(attachment)
- attachment.save! if attachment.changed?
- end
-
- def validate_attachment(attachment, whitelist)
- contract = create_contract attachment, whitelist
-
- unless contract.valid?
- errors = contract.errors.full_messages.join(", ")
- raise "Failed to validate attachment #{attachment.id}: #{errors}"
- end
- end
-
- def create_contract(attachment, whitelist)
- options = derive_contract_options(whitelist)
- ::Attachments::CreateContract.new attachment,
- attachment.author,
- options:
- end
-
- def schedule_jobs(attachment)
- attachment.extract_fulltext
+ errors = errors.full_messages
+
+ OpenProject.logger.error(
+ <<~MSG
+ Failed to finish attachment upload for:
+ * user: #{attachment.author}
+ * container: #{attachment.container}
+ * attachment file name: #{attachment.filename}
+
+ Errors:
+ #{errors.join("\n")}
+ MSG
+ )
end
- def derive_contract_options(whitelist)
- case whitelist
+ def derive_contract_options(allowlist)
+ case allowlist
when false
- { whitelist: [] }
+ { allowlist: [] }
when Array
- { whitelist: whitelist.map(&:to_s) }
+ { allowlist: allowlist.map(&:to_s) }
else
{}
end
end
-
- def journalize_container(attachment)
- journable = attachment.container
-
- return unless journable&.class&.journaled?
-
- # Touching the journable will lead to the journal created next having its own timestamp.
- # That timestamp will not adequately reflect the time the attachment was uploaded. This job
- # right here might be executed way later than the time the attachment was uploaded. Ideally,
- # the journals would be created bearing the time stamps of the attachment's created_at.
- # This remains a TODO.
- # But with the timestamp update in place as it is, at least the collapsing of aggregated journals
- # from days before with the newly uploaded attachment is prevented.
- touch_journable(journable)
-
- Journals::CreateService
- .new(journable, attachment.author)
- .call
- end
-
- def touch_journable(journable)
- # Not using touch here on purpose,
- # as to avoid changing lock versions on the journables for this change
- attributes = journable.send(:timestamp_attributes_for_update_in_model)
-
- timestamps = attributes.index_with { Time.now }
- journable.update_columns(timestamps) if timestamps.any?
- end
-
- def attachment_created_event(attachment)
- OpenProject::Notifications.send(
- OpenProject::Events::ATTACHMENT_CREATED,
- attachment:
- )
- end
end