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

[37014] Remove any special characters from target filename

Oliver Günther820d2b4178ec
diff --git a/app/workers/work_packages/exports/export_job.rb b/app/workers/work_packages/exports/export_job.rb
index 842b08f9982..f98f10ef079 100644
--- a/app/workers/work_packages/exports/export_job.rb
+++ b/app/workers/work_packages/exports/export_job.rb
@@ -1,3 +1,5 @@
+require 'active_storage/filename'
+
module WorkPackages
module Exports
class ExportJob < ::ApplicationJob
@@ -42,13 +44,20 @@ module WorkPackages
end
def store_from_tempfile(export, export_result)
- renamed_file_path = File.join(File.dirname(export_result.content.path), export_result.title)
+ renamed_file_path = target_file_name(export_result)
File.rename(export_result.content.path, renamed_file_path)
file = File.open(renamed_file_path)
store_attachment(export, file)
file.close
end
+ ##
+ # Create a target file name, replacing any invalid characters
+ def target_file_name(export_result)
+ target_name = ActiveStorage::Filename.new(export_result.title).sanitized
+ File.join(File.dirname(export_result.content.path), target_name)
+ end
+
def schedule_cleanup
::WorkPackages::Exports::CleanupOutdatedJob.perform_after_grace
end
diff --git a/spec/workers/work_packages/exports/export_job_integration_spec.rb b/spec/workers/work_packages/exports/export_job_integration_spec.rb
new file mode 100644
index 00000000000..b308d939737
--- /dev/null
+++ b/spec/workers/work_packages/exports/export_job_integration_spec.rb
@@ -0,0 +1,80 @@
+#-- 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.
+#++
+
+require 'spec_helper'
+
+describe WorkPackages::Exports::ExportJob, 'Integration' do
+ let(:project) { FactoryBot.create(:project) }
+ let(:user) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: %w[view_work_packages export_work_packages])
+ end
+ let(:export) do
+ FactoryBot.create(:work_packages_export)
+ end
+ let(:query) { FactoryBot.create(:query, name: 'Query report 04/2021 äöü', project: project) }
+ let(:query_attributes) { {} }
+
+ let(:job) { described_class.new(jobs_args) }
+ let(:jobs_args) do
+ {
+ export: export,
+ mime_type: mime_type,
+ user: user,
+ options: options,
+ query: query,
+ query_attributes: query_attributes
+ }
+ end
+ let(:options) { {} }
+ let(:mime_type) { :pdf }
+
+ subject(:performed_job) do
+ job.tap(&:perform_now)
+ end
+
+ subject(:job_status) do
+ JobStatus::Status.find_by(job_id: job.job_id)
+ end
+
+ describe 'with special characters in the project title' do
+ let(:project) { FactoryBot.create(:project, name: 'Foo Bla. Report No. 4/2021 with/for Case 42') }
+
+ it 'exports the job correctly, renaming the result' do
+ expect { performed_job }.not_to raise_error
+
+ expect(job_status.status).to eq 'success'
+
+ attachment = export.attachments.last
+ expect(attachment.filename).to eq "Foo_Bla._Report_No._4-2021_with-for_Case_42_-_Query_report_04-2021_äöü.pdf"
+ end
+ end
+end