From ca4c72e648cb4d580e87818ed834a0f5287b604f Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 14 May 2014 10:51:09 +1000 Subject: [PATCH] BUGFIX: 500 error on some invalid uploads --- app/controllers/uploads_controller.rb | 12 +++++-- .../rails_multisite/connection_management.rb | 31 +++++-------------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 2b7bffa06..b380804d0 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -16,8 +16,10 @@ class UploadsController < ApplicationController end def show + return render_404 if !RailsMultisite::ConnectionManagement.has_db?(params[:site]) + 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 url = request.fullpath @@ -26,9 +28,15 @@ class UploadsController < ApplicationController if upload = Upload.find_by(id: id, url: url) send_file(Discourse.store.path_for(upload), filename: upload.original_filename) else - render nothing: true, status: 404 + render_404 end end end + protected + + def render_404 + render nothing: true, status: 404 + end + end diff --git a/vendor/gems/rails_multisite/lib/rails_multisite/connection_management.rb b/vendor/gems/rails_multisite/lib/rails_multisite/connection_management.rb index 50e4843fb..10c916c23 100644 --- a/vendor/gems/rails_multisite/lib/rails_multisite/connection_management.rb +++ b/vendor/gems/rails_multisite/lib/rails_multisite/connection_management.rb @@ -1,13 +1,19 @@ module RailsMultisite class ConnectionManagement 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? !!(Rails.version =~ /^4/) end 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 else spec = connection_spec(opts) || @@default_spec @@ -119,33 +125,10 @@ module RailsMultisite @@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 = {} @@established_default = false 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) @app = app