From 18e719b0afeec8d54f29140e9aa7fcfb6e0863bd Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 17 Jan 2014 11:47:01 -0500 Subject: [PATCH] In development mode, if an avatar is missing just serve up a placeholder --- config/environments/development.rb | 2 ++ lib/middleware/missing_avatars.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 lib/middleware/missing_avatars.rb diff --git a/config/environments/development.rb b/config/environments/development.rb index bacf09f955c..62d44db4e35 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -43,7 +43,9 @@ Discourse::Application.configure do config.enable_mini_profiler = true require 'middleware/turbo_dev' + require 'middleware/missing_avatars' config.middleware.insert 0, Middleware::TurboDev + config.middleware.insert 1, Middleware::MissingAvatars config.enable_anon_caching = false end diff --git a/lib/middleware/missing_avatars.rb b/lib/middleware/missing_avatars.rb new file mode 100644 index 00000000000..1d315688046 --- /dev/null +++ b/lib/middleware/missing_avatars.rb @@ -0,0 +1,26 @@ +module Middleware + + # In development mode, it is common to use a database from a production site for testing + # with their data. Unfortunately, you can end up with dozens of missing avatar requests + # due to the files not being present locally. This middleware, only enabled in development + # mode, will replace those with an appropriate image. + class MissingAvatars + def initialize(app, settings={}) + @app = app + end + + def call(env) + if (env['REQUEST_PATH'] =~ /^\/uploads\/default\/avatars/) + path = "#{Rails.root}/public#{env['REQUEST_PATH']}" + unless File.exist?(path) + default_image = "#{Rails.root}/public/images/d-logo-sketch-small.png" + return [ 200, { 'Content-Type' => 'image/png' }, [ File.read(default_image)] ] + end + end + + status, headers, response = @app.call(env) + [status, headers, response] + end + end + +end