FIX: Better error handling if a file cannot be sent

If for some reason `Discourse.store.path_for` returns `nil`, the
forum would throw an error rather than returning 404.

Why would it be `nil`? One cause could be changing the type of
file store and having the `url` field no longer be relative.
This commit is contained in:
Robin Ward 2019-01-29 16:47:25 -05:00
parent 0d0303e7ea
commit 6f656f6e7d
2 changed files with 13 additions and 1 deletions

View File

@ -75,7 +75,11 @@ class UploadsController < ApplicationController
}
opts[:disposition] = "inline" if params[:inline]
opts[:disposition] ||= "attachment" unless FileHelper.is_supported_image?(upload.original_filename)
send_file(Discourse.store.path_for(upload), opts)
file_path = Discourse.store.path_for(upload)
return render_404 unless file_path
send_file(file_path, opts)
else
render_404
end

View File

@ -226,6 +226,14 @@ describe UploadsController do
expect(response.status).to eq(404)
end
it "returns 404 when the path is nil" do
upload = upload_file("logo.png")
upload.update_column(:url, "invalid-url")
get "/uploads/#{site}/#{upload.sha1}.#{upload.extension}"
expect(response.status).to eq(404)
end
it 'uses send_file' do
upload = upload_file("logo.png")
get "/uploads/#{site}/#{upload.sha1}.#{upload.extension}"