Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. ##
  2. # Send a protected file using the web server (via the x-sendfile header).
  3. # Takes the absolute file system path to the file and, optionally, a MIME type.
  4. #
  5. def send_file(filepath, options = {})
  6. options[:content_type] ||= "application/force-download"
  7. response.headers['Content-Type'] = options[:content_type]
  8. response.headers['Content-Disposition'] = "attachment; filename="#{File.basename(filepath)}""
  9. response.headers['X-Sendfile'] = filepath
  10. response.headers['Content-length'] = File.size(filepath)
  11. render :nothing => true
  12. end
  13.  
  14. ##
  15. # Private file download: check permission first.
  16. #
  17. def download
  18. product = Product.find_by_filename!(params[:filename])
  19. if current_user.has_bought?(product) or current_user.is_superuser?
  20. if File.exist?(path = product.filepath)
  21. send_file path, :content_type => "application/pdf"
  22. else
  23. not_found
  24. end
  25. else
  26. not_authorized
  27. end
  28. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement