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

Fix/filename on direct upload (#8928)

ulferts4ad9ebc6eed3
diff --git a/app/models/attachment.rb b/app/models/attachment.rb
index 7be64b9dcd8..424a20f8c05 100644
--- a/app/models/attachment.rb
+++ b/app/models/attachment.rb
@@ -267,7 +267,7 @@ class Attachment < ApplicationRecord
# 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] = file_name
+ a[:file] = pending_direct_upload_filename(file_name)
a.save!
a.reload # necessary so that the fog file uploader path is correct
@@ -275,6 +275,17 @@ class Attachment < ApplicationRecord
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 == -1
end
diff --git a/app/workers/attachments/finish_direct_upload_job.rb b/app/workers/attachments/finish_direct_upload_job.rb
index f4b0cd874af..b916d5eff57 100644
--- a/app/workers/attachments/finish_direct_upload_job.rb
+++ b/app/workers/attachments/finish_direct_upload_job.rb
@@ -32,7 +32,10 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
queue_with_priority :high
def perform(attachment_id)
- attachment = Attachment.pending_direct_uploads.where(id: attachment_id).first
+ attachment = Attachment.pending_direct_uploads.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?
diff --git a/spec/models/attachment_spec.rb b/spec/models/attachment_spec.rb
index 637345cb227..4993ad7944f 100644
--- a/spec/models/attachment_spec.rb
+++ b/spec/models/attachment_spec.rb
@@ -40,17 +40,17 @@ describe Attachment, type: :model do
let(:attachment) do
FactoryBot.build(
:attachment,
- author: author,
- container: container,
+ author: author,
+ container: container,
content_type: nil, # so that it is detected
- file: file
+ file: file
)
end
let(:stubbed_attachment) do
FactoryBot.build_stubbed(
:attachment,
- author: stubbed_author,
- container: container
+ author: stubbed_author,
+ container: container
)
end
@@ -206,6 +206,64 @@ describe Attachment, type: :model do
end
end
+ describe '.create_pending_direct_upload' do
+ let(:file_size) { 6 }
+ let(:file_name) { 'document.png' }
+ let(:content_type) { "application/octet-stream" }
+
+ subject do
+ described_class.create_pending_direct_upload(file_name: file_name,
+ author: author,
+ container: container,
+ content_type: content_type,
+ file_size: file_size)
+ end
+
+ it 'returns the attachment' do
+ expect(subject)
+ .to be_a(Attachment)
+ end
+
+ it 'sets the content_type' do
+ expect(subject.content_type)
+ .to eql content_type
+ end
+
+ it 'sets the file_size' do
+ expect(subject.filesize)
+ .to eql file_size
+ end
+
+ it 'sets the file for carrierwave' do
+ expect(subject.file.file.path)
+ .to end_with "attachment/file/#{subject.id}/#{file_name}"
+ end
+
+ it 'sets the author' do
+ expect(subject.author)
+ .to eql author
+ end
+
+ it 'sets the digest to empty string' do
+ expect(subject.digest)
+ .to eql ""
+ end
+
+ it 'sets the download count to -1' do
+ expect(subject.downloads)
+ .to eql -1
+ end
+
+ context 'with a special character in the filename' do
+ let(:file_name) { "document=number 5.png" }
+
+ it 'sets the file for carrierwave' do
+ expect(subject.file.file.path)
+ .to end_with "attachment/file/#{subject.id}/document_number_5.png"
+ end
+ end
+ end
+
##
# The tests assumes the default, file-based storage is configured and tests against that.
# I.e. it does not test fog attachments being deleted from the cloud storage (such as S3).