How to create a rails application that makes a file accessible through unique, automatically generated links?

I have a file that I would like to make available for online download, but only for selected users.

This is a typical scenario, which I mean

The person who wants the file usually visits the site and fills out the form to request access to the file.

If I would like to share a file with him, I would allow the user to create a unique link that will be sent to the user by e-mail. The link will be valid only for a certain period of time.

I would use rails for this. What I'm looking for is responsible for:

  • What is the right way to create a unique download link?
  • How to start downloading a file when a user clicks a link within a valid time interval?
+6
source share
1 answer

First you want to set up a model for storing tokens:

rails g model DownloadToken token:string expires_at:timestamp 

download_token.rb

 class DownloadToken < ActiveRecord::Base attr_accessible :token, :expires_at before_create :generate_token def generate_token self.token = SecureRandom.base64(15).tr('+/=lIO0', 'abc123') end end 

Then configure the controller to process the submitted form (or make changes to the existing action) and create a token, send an email, etc.

 class FooController < ApplicationController def create #process submitted form ... #create a token that expires in 24 hours @token = DownloadToken.create(:expires_at => Time.now + 24.hours) #send email and redirect.. end end 

You want to make sure your postal view has something like:

 <%= link_to "Click Me", "/files/downloads?token=#{@token.token}" %> 

You will also want to configure the controller responsible for the boot, and it should look something like this:

 class FileController < ApplicationController before_filter :check_token def check_token redirect_to :back, :flash => {:error => "Bad link"} if DownloadToken.where("token = ? and expires_at > ?", params[:token], Time.now).nil? end def download send_file '/home/your_app/downloads/yourfile.zip', :type=>"application/zip", :x_sendfile=>true end end 

routes.rb (if Foo is already configured as a RESTful resource)

 match 'files/download' => 'files#download' 

This code has not been verified, but it should cover most of what you need and give you an idea of ​​which direction you want to take.

Additional Information:

+10
source

All Articles