class PictureController < ApplicationController
  skip_before_action :get_global_session_data
  skip_after_action :set_global_session_data

  def show # GET /pictures/:basename
    id, extension = params[:basename].split('.')
    picture = Picture.find_by_id(id)
    if picture && picture.extension == extension
      render_picture_content(picture)
    else
      head :not_found
    end
  end

  private

  def render_picture_content(picture)
    response.headers['Cache-Control'] = "public"
    response.headers['Content-Type'] = "image/#{picture.extension}"
    response.headers['Content-Disposition'] = "inline"
    render :body => picture.content
  end
end

class PictureController < ApplicationController
  skip_before_action :get_global_session_data
  skip_after_action :set_global_session_data

  def show # GET /pictures/:basename
    id, extension = params[:basename].split('.')

    picture = Picture.where(:extension => extension).find_by(:id => id)
    return head(:not_found) unless picture

    fresh_when(:etag => picture.md5, :public => true)

    send_data(picture.content, :type => "image/#{extension}", :disposition => 'inline') if stale?
  end
end

Use standard rails methods to serve up photos
Use standard rails methods to serve up photos

- Don't fetch the photo from the db if it is the wrong extension
- fresh_when could calculate the etags for us, but used our md5 instead