开发者

using ruby/rails to catch out going file request

开发者 https://www.devze.com 2022-12-29 13:26 出处:网络
I\'m working with a rails app which plays mp3s. Is there a way to catch the request for the mp3 file with ruby, reformat the request(url), and pass th开发者_运维问答e request along?

I'm working with a rails app which plays mp3s. Is there a way to catch the request for the mp3 file with ruby, reformat the request(url), and pass th开发者_运维问答e request along?

I'm really just trying to protect the urls...


I'm not sure I exactly understand what you want to do, but wouldn't just catching the path in your Routes file and passing it to an action (or doing it directly in the Routes file in Rails 3) which will redirect_to the path you want?

Take 2

So to encrypt you might want to do something like this: In song.rb (the model):

class Song < ActiveRecord::Base

  before_create :create_secret_param

  private
  def create_secret_param
    self.secret = rand(100000)
  end

end

This will create a secret param that you can use to access it.

In routes.rb

map.secret_song ':id:secret', :controller => 'songs', :action => 'show', :id => /\d+/, :secret => /\d{5}/

In song_controller.rb

def show
  if param[:secret]
    @song = Song.find param[:id], :conditions => {:secret => params[:secret]}
  elsif # check if is user is ok or whatever
    @song = Song.find param[:id]
    redirect_to secret_song_url(:id => @song.id, :secret => @song.secret)
  end
end

This should point you in the right direction, though you will have to get the details right for your app.

0

精彩评论

暂无评论...
验证码 换一张
取 消