title: Dragonfly and Short URLs
body: <p>I'm using Dragonfly in a project that returns a large stream of photos and was looking to optimize the URLs. I'm currently getting image URLs like:</p>
  
  <p><a href="http://localhost:3000/media/BAhbCFsHOgZmSSJgZmRlL2ZkZTAxYzQ0LTM4Y2UtNGU0ZS1iOWRlLWUwZmUxNWUwN2JmMC83Mzk1NmZlMC05ZTA5LTQzNWUtODUyMC00MzFlYzQxMzQ1OTQvb3JpZ2luYWwuanBlZwY6BkVUWwg6BnA6CnRodW1iSSIMMjQweDI0MAY7BkZbCTsHOgxjb252ZXJ0SSIQLXF1YWxpdHkgODAGOwZGMA/240x240.jpg" rel="nofollow">http://localhost:3000/media/BAhbCFsHOgZmSSJgZmRlL2ZkZTAxYzQ0LTM4Y2UtNGU0ZS1iOWRlLWUwZmUxNWUwN2JmMC83Mzk1NmZlMC05ZTA5LTQzNWUtODUyMC00MzFlYzQxMzQ1OTQvb3JpZ2luYWwuanBlZwY6BkVUWwg6BnA6CnRodW1iSSIMMjQweDI0MAY7BkZbCTsHOgxjb252ZXJ0SSIQLXF1YWxpdHkgODAGOwZGMA/240x240.jpg</a></p>
  
  <p>Which is over 256 bytes. I'd like something like:</p>
  
  <p><a href="http://localhost:3000/media/1024/240x240_medium.jpg" rel="nofollow">http://localhost:3000/media/1024/240x240_medium.jpg</a></p>
  
  <p>That is conforming to:</p>
  
  <pre><code>/media/:id/:format
  </code></pre>
  
  <p>How would I go about adding this when using Dragonfly and Rails such that <code>:format</code> maps to a chain of operations and <code>:id</code> is used to find the model or image? Thanks!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I've added custom <code>Mime::Type</code>for each of the formats I need and have the following working:</p>
  
  <pre><code># config/routes.rb
  match "/photos/:id/:style", to: "photos#show", as: :media
  
  # app/controllers/photos_controller.rb
  def show
    @photo = Photo.find(params[:id])
  
    respond_to do |format|
      format.html # show.html.erb
      format.jpg { cache('public', 86400); redirect_to @photo.url(params[:style], 'jpg') }
      format.png { cache('public', 86400); redirect_to @photo.url(params[:style], 'png') }
      format.gif { cache('public', 86400); redirect_to @photo.url(params[:style], 'gif') }
    end
  end
  
  # app/views/photos/show.html.erb
  &lt;%= image_tag media_path(id: @photo.id, style: 'small', format: 'png') %&gt;
  </code></pre>
  
  <p>However this results in a <code>302</code> for each image (but otherwise works fine). Is it possible to handle this as a render or somehow do an internal redirect (i.e. not require the client to make duplicate requests)?</p>