From d10c9d7d758fe8b9ced3042bb24bdf3fc009285a Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 13 Aug 2018 10:55:06 +0800 Subject: [PATCH] FIX: Missing extensions for non-image uploads due to https://github.com/tgxworld/discourse/commit/2b5723938920647c45f7fc526e1edd19e8fad09e. --- lib/upload_creator.rb | 2 +- spec/requests/uploads_controller_spec.rb | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb index a063bdb361f..10b5902a001 100644 --- a/lib/upload_creator.rb +++ b/lib/upload_creator.rb @@ -79,7 +79,7 @@ class UploadCreator @upload.sha1 = sha1 @upload.url = "" @upload.origin = @opts[:origin][0...1000] if @opts[:origin] - @upload.extension = image_type + @upload.extension = image_type || File.extname(@filename)[1..10] if FileHelper.is_image?(@filename) @upload.width, @upload.height = ImageSizer.resize(*@image_info.size) diff --git a/spec/requests/uploads_controller_spec.rb b/spec/requests/uploads_controller_spec.rb index 63475547710..62ba2562435 100644 --- a/spec/requests/uploads_controller_spec.rb +++ b/spec/requests/uploads_controller_spec.rb @@ -164,7 +164,29 @@ describe UploadsController do expect(message).to contain_exactly(I18n.t("upload.images.size_not_found")) end - describe 'when filename has the wrong extension' do + describe 'when upload is not an image' do + before do + SiteSetting.authorized_extensions = 'txt' + end + + let(:file) do + Rack::Test::UploadedFile.new(file_from_fixtures("utf-8.txt", "encodings")) + end + + it 'should store the upload with the right extension' do + expect do + post "/uploads.json", params: { file: file, type: "composer" } + end.to change { Upload.count }.by(1) + + upload = Upload.last + + expect(upload.extension).to eq('txt') + expect(File.extname(upload.url)).to eq('.txt') + expect(upload.original_filename).to eq('utf-8.txt') + end + end + + describe 'when image has the wrong extension' do let(:file) do Rack::Test::UploadedFile.new(file_from_fixtures("png_as.jpg")) end