A platform for community discussion. Free, open, simple.
FIX: Be able to handle long file extensions (#12375)
Blake Erickson44153cde18e4
diff --git a/config/routes.rb b/config/routes.rbindex 615f1f6a88a..8e27a365b08 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -528,7 +528,7 @@ Discourse::Application.routes.draw do# used to download original imagesget "uploads/:site/:sha(.:extension)" => "uploads#show", constraints: { site: /\w+/, sha: /\h{40}/, extension: /[a-z0-9\._]+/i }- get "uploads/short-url/:base62(.:extension)" => "uploads#show_short", constraints: { site: /\w+/, base62: /[a-zA-Z0-9]+/, extension: /[a-z0-9\._]+/i }, as: :upload_short+ get "uploads/short-url/:base62(.:extension)" => "uploads#show_short", constraints: { site: /\w+/, base62: /[a-zA-Z0-9]+/, extension: /[a-zA-Z0-9\._-]+/i }, as: :upload_short# used to download attachmentsget "uploads/:site/original/:tree:sha(.:extension)" => "uploads#show", constraints: { site: /\w+/, tree: /([a-z0-9]+\/)+/i, sha: /\h{40}/, extension: /[a-z0-9\._]+/i }if Rails.env.test?diff --git a/spec/fixtures/images/fake.long-FileExtension b/spec/fixtures/images/fake.long-FileExtensionnew file mode 100644index 00000000000..a6a874af9ee--- /dev/null+++ b/spec/fixtures/images/fake.long-FileExtension@@ -0,0 +1 @@+https://meta.discourse.org/t/uploading-files-with-longer-file-extensions-gets-truncated/182824diff --git a/spec/lib/upload_creator_spec.rb b/spec/lib/upload_creator_spec.rbindex fdf08347b4d..67e67dac490 100644--- a/spec/lib/upload_creator_spec.rb+++ b/spec/lib/upload_creator_spec.rb@@ -9,7 +9,7 @@ RSpec.describe UploadCreator dodescribe '#create_for' dodescribe 'when upload is not an image' dobefore do- SiteSetting.authorized_extensions = 'txt'+ SiteSetting.authorized_extensions = 'txt|long-FileExtension'endlet(:filename) { "utf-8.txt" }@@ -38,6 +38,19 @@ RSpec.describe UploadCreator doexpect(user2.user_uploads.count).to eq(1)expect(upload.user_uploads.count).to eq(2)end++ let(:longextension) { "fake.long-FileExtension" }+ let(:file2) { file_from_fixtures(longextension) }++ it 'should truncate long extension names' do+ expect do+ UploadCreator.new(file2, "fake.long-FileExtension").create_for(user.id)+ end.to change { Upload.count }.by(1)++ upload = Upload.last++ expect(upload.extension).to eq('long-FileE')+ endenddescribe 'when image is not authorized' dodiff --git a/spec/requests/uploads_controller_spec.rb b/spec/requests/uploads_controller_spec.rbindex 1e7769c1c69..9b7201ad78c 100644--- a/spec/requests/uploads_controller_spec.rb+++ b/spec/requests/uploads_controller_spec.rb@@ -365,6 +365,13 @@ describe UploadsController doexpect(response.status).to eq(200)end+ it "returns uploads with a dash and uppercase in extension correctly" do+ fake_upload = upload_file("fake.long-FileExtension")+ get fake_upload.short_path++ expect(response.status).to eq(200)+ end+it "returns the right response when anon tries to download a file " \"when prevent_anons_from_downloading_files is true" do