Class: DownloadablesController

Inherits:
Spree::BaseController
  • Object
show all
Defined in:
app/controllers/downloadables_controller.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) show



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/downloadables_controller.rb', line 7

def show
  item = LineItem.find(params[:id])
  if(item.download_limit.nil? || (item.download_limit > 0))
    item.decrement!(:download_limit) if (!item.download_limit.nil?)
    
    filepath = ""
    if !item.product.downloadables.empty?
      filepath = item.product.downloadables.first.attachment.path
    elsif !item.variant.downloadables.empty?
      filepath = item.variant.downloadables.first.attachment.path
    end
    
    # In pratical use, enabled X-sendfile in your server flavor ie. Apache, lighty, etc.. 
    # DON'T use mongrel/webrick, since files are static. Resources will be wasted since it'll go thru the rails stack to 
    # fetch the file. Uncomment the line below.
    send_file filepath #, :x_sendfile => true
  else
    flash[:error] = "Reached download limit."
    redirect_to order_url(item.order)
  end
end