2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-17 10:47:01 -06:00
|
|
|
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
|
2017-07-27 20:20:09 -05:00
|
|
|
def initialize(app, settings = {})
|
2014-01-17 10:47:01 -06:00
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2023-01-20 12:52:49 -06:00
|
|
|
if (env["REQUEST_PATH"] =~ %r{\A/uploads/default/avatars})
|
2023-01-09 06:10:19 -06:00
|
|
|
path = "#{Rails.root}/public#{env["REQUEST_PATH"]}"
|
2017-07-27 20:20:09 -05:00
|
|
|
unless File.exist?(path)
|
2014-01-17 10:47:01 -06:00
|
|
|
default_image = "#{Rails.root}/public/images/d-logo-sketch-small.png"
|
2023-01-09 06:10:19 -06:00
|
|
|
return 200, { "Content-Type" => "image/png" }, [File.read(default_image)]
|
2014-01-17 10:47:01 -06:00
|
|
|
end
|
|
|
|
end
|
2017-07-27 20:20:09 -05:00
|
|
|
|
2014-01-17 10:47:01 -06:00
|
|
|
status, headers, response = @app.call(env)
|
|
|
|
[status, headers, response]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|