BUGFIX: 500 error on some invalid uploads

This commit is contained in:
Sam 2014-05-14 10:51:09 +10:00
parent b329e23f85
commit ca4c72e648
2 changed files with 17 additions and 26 deletions

View file

@ -16,8 +16,10 @@ class UploadsController < ApplicationController
end end
def show def show
return render_404 if !RailsMultisite::ConnectionManagement.has_db?(params[:site])
RailsMultisite::ConnectionManagement.with_connection(params[:site]) do |db| RailsMultisite::ConnectionManagement.with_connection(params[:site]) do |db|
return render nothing: true, status: 404 unless Discourse.store.internal? return render_404 unless Discourse.store.internal?
id = params[:id].to_i id = params[:id].to_i
url = request.fullpath url = request.fullpath
@ -26,9 +28,15 @@ class UploadsController < ApplicationController
if upload = Upload.find_by(id: id, url: url) if upload = Upload.find_by(id: id, url: url)
send_file(Discourse.store.path_for(upload), filename: upload.original_filename) send_file(Discourse.store.path_for(upload), filename: upload.original_filename)
else else
render nothing: true, status: 404 render_404
end end
end end
end end
protected
def render_404
render nothing: true, status: 404
end
end end

View file

@ -1,13 +1,19 @@
module RailsMultisite module RailsMultisite
class ConnectionManagement class ConnectionManagement
CONFIG_FILE = 'config/multisite.yml' CONFIG_FILE = 'config/multisite.yml'
DEFAULT = 'default'.freeze
def self.has_db?(db)
return true if db == DEFAULT
(defined? @@db_spec_cache) && @@db_spec_cache && @@db_spec_cache[db]
end
def self.rails4? def self.rails4?
!!(Rails.version =~ /^4/) !!(Rails.version =~ /^4/)
end end
def self.establish_connection(opts) def self.establish_connection(opts)
if opts[:db] == "default" && (!defined?(@@default_spec) || !@@default_spec) if opts[:db] == DEFAULT && (!defined?(@@default_spec) || !@@default_spec)
# don't do anything .. handled implicitly # don't do anything .. handled implicitly
else else
spec = connection_spec(opts) || @@default_spec spec = connection_spec(opts) || @@default_spec
@ -119,33 +125,10 @@ module RailsMultisite
@@default_connection_handler = ActiveRecord::Base.connection_handler @@default_connection_handler = ActiveRecord::Base.connection_handler
# inject our connection_handler pool
# WARNING MONKEY PATCH
#
# see: https://github.com/rails/rails/issues/8344#issuecomment-10800848
if ActiveRecord::VERSION::MAJOR == 3
ActiveRecord::Base.send :include, NewConnectionHandler
ActiveRecord::Base.connection_handler = @@default_connection_handler
end
@@connection_handlers = {} @@connection_handlers = {}
@@established_default = false @@established_default = false
end end
module NewConnectionHandler
def self.included(klass)
klass.class_eval do
define_singleton_method :connection_handler do
Thread.current[:connection_handler] || @connection_handler
end
define_singleton_method :connection_handler= do |handler|
@connection_handler ||= handler
Thread.current[:connection_handler] = handler
end
end
end
end
def initialize(app, config = nil) def initialize(app, config = nil)
@app = app @app = app