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

work in review comments

ulferts3ba3e8b74bd0
diff --git a/app/services/attachments/finish_direct_upload_service.rb b/app/services/attachments/finish_direct_upload_service.rb
index 5eb3a15ed9d..545a9e10e54 100644
--- a/app/services/attachments/finish_direct_upload_service.rb
+++ b/app/services/attachments/finish_direct_upload_service.rb
@@ -59,8 +59,11 @@ module Attachments
end
def persist(call)
- super.tap do
- attachment.save!
+ super.tap do |service_result|
+ unless attachment.save
+ service_result.errors = attachment.errors
+ service_result.success = false
+ end
end
end
@@ -83,9 +86,7 @@ module Attachments
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
+ attachment.file = local_file
end
end
@@ -133,10 +134,7 @@ module Attachments
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
+ attachment&.diskfile
end
end
end
diff --git a/app/workers/attachments/finish_direct_upload_job.rb b/app/workers/attachments/finish_direct_upload_job.rb
index 0438b1b61e3..0c295ac4d34 100644
--- a/app/workers/attachments/finish_direct_upload_job.rb
+++ b/app/workers/attachments/finish_direct_upload_job.rb
@@ -32,6 +32,11 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
def perform(attachment_id, allowlist: true)
attachment = Attachment.pending_direct_upload.find_by(id: attachment_id)
+ unless attachment
+ log_not_found(attachment_id)
+ return
+ end
+
Attachments::FinishDirectUploadService
.new(user: attachment.author, model: attachment, contract_options: derive_contract_options(allowlist))
.call
@@ -49,16 +54,20 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
OpenProject.logger.error(
<<~MSG
Failed to finish attachment upload for:
- * user: #{attachment.author}
- * container: #{attachment.container}
+ * user: #{attachment.author_id} - #{attachment.author.name}
+ * container: #{attachment.container_id} - #{attachment.container}
* attachment file name: #{attachment.filename}
Errors:
- #{errors.join("\n")}
+ #{errors.join("\n ")}
MSG
)
end
+ def log_not_found(attachment_id)
+ OpenProject.logger.error("Attachment #{attachment_id} not found")
+ end
+
def derive_contract_options(allowlist)
case allowlist
when false
diff --git a/spec/workers/attachments/finish_direct_upload_job_integration_spec.rb b/spec/workers/attachments/finish_direct_upload_job_integration_spec.rb
index 2df7fb0b761..e6b0aef64e8 100644
--- a/spec/workers/attachments/finish_direct_upload_job_integration_spec.rb
+++ b/spec/workers/attachments/finish_direct_upload_job_integration_spec.rb
@@ -136,6 +136,18 @@ RSpec.describe Attachments::FinishDirectUploadJob, "integration", type: :job do
it_behaves_like "adding a journal to the attachment in the name of the attachment's author"
end
+ context "for an attachment that has been removed in the meantime" do
+ let!(:container) { create(:work_package) }
+
+ it "logs the error and resolves the job as done" do
+ allow(OpenProject.logger).to receive(:error)
+
+ job.perform(pending_attachment.id + 1)
+
+ expect(OpenProject.logger).to have_received(:error)
+ end
+ end
+
context "with an incompatible attachment allowlist",
with_settings: { attachment_whitelist: %w[image/png] } do
let!(:container) { create(:work_package) }