【问题标题】:Dragonfly and Short URLs蜻蜓和短网址
【发布时间】:2011-08-18 00:00:16
【问题描述】:

我在一个返回大量照片流的项目中使用 Dragonfly,并希望优化 URL。我目前正在获取如下图像 URL:

http://localhost:3000/media/BAhbCFsHOgZmSSJgZmRlL2ZkZTAxYzQ0LTM4Y2UtNGU0ZS1iOWRlLWUwZmUxNWUwN2JmMC83Mzk1NmZlMC05ZTA5LTQzNWUtODUyMC00MzFlYzQxMzQ1OTQvb3JpZ2luYWwuanBlZwY6BkVUWwg6BnA6CnRodW1iSSIMMjQweDI0MAY7BkZbCTsHOgxjb252ZXJ0SSIQLXF1YWxpdHkgODAGOwZGMA/240x240.jpg

超过 256 个字节。我想要类似的东西:

http://localhost:3000/media/1024/240x240_medium.jpg

即符合:

/media/:id/:format

在使用 Dragonfly 和 Rails 时,我将如何添加它,以便 :format 映射到一系列操作,:id 用于查找模型或图像?谢谢!

编辑:

我为我需要的每种格式添加了自定义Mime::Type,并且具有以下功能:

# 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
<%= image_tag media_path(id: @photo.id, style: 'small', format: 'png') %>

但是,这会为每个图像生成一个302(但其他方面工作正常)。是否可以将其作为渲染处理或以某种方式进行内部重定向(即不需要客户端进行重复请求)?

【问题讨论】:

    标签: ruby-on-rails ruby dragonfly-gem


    【解决方案1】:

    您不需要使用控制器操作 - 您可以使用蜻蜓端点 - 请参阅 http://markevans.github.com/dragonfly/file.URLs.html#Routed_Endpoints

    例如

    match '/photos/:id/:style.:format' => Dragonfly[:images].endpoint { |params, app|
      Photo.find(params[:id]).image.thumb(params[:style]).encode(params[:format])
    }
    

    或类似的东西 (没有尝试过上面的代码,但它会是类似的东西)

    【讨论】:

      【解决方案2】:

      我遇到过类似的情况,客户需要一个短网址来下载可下载的 pdf。

      基于 Mark 的回答并查看 dragonfly docs 我想出了这个:

      #file.rb
      class File < ActiveRecord::Base
        dragonfly_accessor :pdf
      end
      
      def pdf_link
        return "/pdf/#{self.filename}.pdf"
      end  
      
      #routes.rb
      get '/pdf/:filename' => Dragonfly.app.endpoint { |params, app|
        File.where(filename: params[:filename]).first.pdf
      }
      

      这不是问题的直接答案,但也许它仍然可以帮助寻找缩短蜻蜓网址的方法的人

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多