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
[67642] Avoid error when attachment is not uploaded yet
Christophe Bliarda69859f0ec73
diff --git a/app/workers/attachments/extract_fulltext_job.rb b/app/workers/attachments/extract_fulltext_job.rbindex d02fa7ac8ef..6260ff5e1da 100644--- a/app/workers/attachments/extract_fulltext_job.rb+++ b/app/workers/attachments/extract_fulltext_job.rb@@ -53,17 +53,17 @@ module Attachmentsdef initcarrierwave_uploader = @attachment.file- @file = carrierwave_uploader.local_file@filename = carrierwave_uploader.file.filenameif @attachment.readable?+ @file = carrierwave_uploader.local_fileresolver = Plaintext::Resolver.new(@file, @attachment.content_type)@text = resolver.textendrescue StandardError => e- Rails.logger.error(- "Failed to extract plaintext from file #{@attachment&.id} (On domain #{Setting.host_name}): #{e}: #{e.message}"- )+ log_error("Failed to extract plaintext for attachment ##{@attachment&.id}", e)+ update # Still update tsv values+ raiseenddef update@@ -76,9 +76,8 @@ module Attachments@language,OpenProject::FullTextSearch.normalize_filename(@filename)])rescue StandardError => e- Rails.logger.error(- "Failed to update TSV values for attachment #{@attachment&.id} (On domain #{Setting.host_name}): #{e.message[0..499]}[...]"- )+ log_error("Failed to update TSV values for attachment ##{@attachment&.id}", e)+ raiseenddef find_attachment(id)@@ -92,5 +91,10 @@ module Attachmentsdef delete_file?remote_file? && @fileend++ def log_error(message, cause)+ message += " (on domain #{Setting.host_name})"+ Rails.logger.error("#{message}: #{cause.message.truncate(500, omission: '[...]')}")+ endendenddiff --git a/config/initializers/content_security_policy.rb b/config/initializers/content_security_policy.rbindex 9969096f567..ef82c74a711 100644--- a/config/initializers/content_security_policy.rb+++ b/config/initializers/content_security_policy.rb@@ -73,7 +73,7 @@ Rails.application.config.after_initialize do# Allow connections to S3 for BIMif OpenProject::Configuration.fog_directory.present?connect_src += [- "#{OpenProject::Configuration.fog_directory}.s3-#{OpenProject::Configuration.fog_credentials[:region]}.amazonaws.com"+ OpenProject::Configuration.fog_s3_upload_host]enddiff --git a/lib_static/open_project/configuration/helpers.rb b/lib_static/open_project/configuration/helpers.rbindex 2979850f43f..4afdc706020 100644--- a/lib_static/open_project/configuration/helpers.rb+++ b/lib_static/open_project/configuration/helpers.rb@@ -131,13 +131,17 @@ module OpenProjectenddef fog_credentials- (Hash(self["fog"])["credentials"] || {}).map { |key, value| [key.to_sym, value] }.to_h+ (Hash(self["fog"])["credentials"] || {}).symbolize_keysenddef fog_directoryHash(self["fog"])["directory"]end+ def fog_s3_upload_host+ fog_credentials[:endpoint].presence || "#{fog_directory}.s3-#{fog_credentials[:region]}.amazonaws.com"+ end+def file_uploaderavailable_file_uploaders[OpenProject::Configuration.attachments_storage.to_sym]enddiff --git a/spec/.rubocop.yml b/spec/.rubocop.ymlnew file mode 100644index 00000000000..39ed3485427--- /dev/null+++ b/spec/.rubocop.yml@@ -0,0 +1,4 @@+inherit_from: ../.rubocop.yml++Style/RescueModifier:+ Enabled: falsediff --git a/spec/workers/extract_fulltext_job_spec.rb b/spec/workers/attachments/extract_fulltext_job_spec.rbsimilarity index 75%rename from spec/workers/extract_fulltext_job_spec.rbrename to spec/workers/attachments/extract_fulltext_job_spec.rbindex f4abe191d1e..cd34f6ec327 100644--- a/spec/workers/extract_fulltext_job_spec.rb+++ b/spec/workers/attachments/extract_fulltext_job_spec.rb@@ -32,8 +32,6 @@ require "spec_helper"RSpec.describe Attachments::ExtractFulltextJob, type: :job dosubject(:extracted_attributes) do- perform_enqueued_jobs-attachment.reloadAttachment.connection.select_one <<~SQL.squish@@ -51,10 +49,6 @@ RSpec.describe Attachments::ExtractFulltextJob, type: :job dolet(:text) { "lorem ipsum" }let(:attachment) docreate(:attachment).tap do |attachment|- expect(Attachments::ExtractFulltextJob)- .to have_been_enqueued- .with(attachment.id)-allow(Attachment).to receive(:find_by).with(id: attachment.id)@@ -62,17 +56,33 @@ RSpec.describe Attachments::ExtractFulltextJob, type: :job doendend+ it "is enqueued on Attachment creation" do+ attachment = create(:attachment)+ expect(described_class)+ .to have_been_enqueued+ .with(attachment.id)+ end++ it "is not enqueued on Attachment update" do+ attachment = create(:attachment)+ clear_enqueued_jobs+ attachment.touch+ expect(described_class)+ .not_to have_been_enqueued+ end+context "with successful text extraction" dobefore doallow_any_instance_of(Plaintext::Resolver).to receive(:text).and_return(text)end- context "attachment is readable" do+ context "when the attachment is readable" dobefore doallow(attachment).to receive(:readable?).and_return(true)endit "updates the attachment's DB record with fulltext, fulltext_tsv, and file_tsv" do+ perform_enqueued_jobsexpect(extracted_attributes["fulltext"]).to eq textexpect(extracted_attributes["fulltext_tsv"].size).to be > 0expect(extracted_attributes["file_tsv"].size).to be > 0@@ -83,6 +93,7 @@ RSpec.describe Attachments::ExtractFulltextJob, type: :job do# include_examples 'no fulltext but file name saved as TSV'it "updates the attachment's DB record with file_tsv" do+ perform_enqueued_jobsexpect(extracted_attributes["fulltext"]).to be_blankexpect(extracted_attributes["fulltext_tsv"]).to be_blankexpect(extracted_attributes["file_tsv"].size).to be > 0@@ -91,39 +102,45 @@ RSpec.describe Attachments::ExtractFulltextJob, type: :job doendend- shared_examples "only file name indexed" do- it "updates the attachment's DB record with file_tsv" do- expect(extracted_attributes["fulltext"]).to be_blank- expect(extracted_attributes["fulltext_tsv"]).to be_blank- expect(extracted_attributes["file_tsv"].size).to be > 0- end- end-context "with the file not readable" dobefore doallow(attachment).to receive(:readable?).and_return(false)end- include_examples "only file name indexed"+ it "updates the attachment's DB record with file_tsv" do+ perform_enqueued_jobs+ expect(extracted_attributes["fulltext"]).to be_blank+ expect(extracted_attributes["fulltext_tsv"]).to be_blank+ expect(extracted_attributes["file_tsv"].size).to be > 0+ endend- context "with exception in extraction" do+ context "with exception during extraction" dolet(:exception_message) { "boom-internal-error" }let(:logger) { Rails.logger }before do- allow_any_instance_of(Plaintext::Resolver).to receive(:text).and_raise(exception_message)+ allow_any_instance_of(Plaintext::Resolver).to receive(:text).and_raise(exception_message) # rubocop:disable RSpec/AnyInstanceallow(logger).to receive(:error)allow(attachment).to receive(:readable?).and_return(true)end- it "logs the error" do- extracted_attributes+ it "raises the exception to update Job's internal state" do+ expect { perform_enqueued_jobs }.to raise_error(exception_message)+ end++ it "logs the exception" do+ perform_enqueued_jobs rescue nilexpect(logger).to have_received(:error).with(/boom-internal-error/)end- include_examples "only file name indexed"+ it "updates the attachment's DB record with file_tsv from the filename" do+ perform_enqueued_jobs rescue nil+ expect(extracted_attributes["fulltext"]).to be_blank+ expect(extracted_attributes["fulltext_tsv"]).to be_blank+ expect(extracted_attributes["file_tsv"].size).to be > 0+ endendend