Sha256: 21f7a81330e257a1bcecfd834b4a5dac21f14b80c2a26d2d22a77ccfaa892885

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require 'nitro/render'

module Nitro

module Render

  # Send a file download to the client.
  #
  # Like render and redirect, the action is exited upon calling
  #
  # [+fname+]  That name of the file
  # [+path+]   Specifying true mean fname contains the full path.
  #     The default, false, uses Server.public_root as the path.
  #
  # [+return+] true on success, false on failure
  #
  # === Examples
  #
  # require 'nitro/cgi/sendfile'
  # class MyController < Nitro:Controller
  #   def download(fname)
  #     sendfile(fname)
  #   end
  # end
  #
  # class MyController < Nitro:Controller
  #   def download()
  #     sendfile('/etc/password', true)
  #   end
  # end

  def sendfile(fname=nil, fullpath=false)
    fname = fullpath ? fname : "#{Server.public_root}/#{fname}"
    f = File.open(fname, "rb")
    @context.response_headers["Cache-control"] = 'private'
    @context.response_headers["Content-Length"] = "#{File.size?(f) || 0}"
    @context.response_headers["Content-Type"] = 'application/force-download'
    @context.out = f
    raise RenderExit
  end

end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nitro-0.31.0 lib/nitro/cgi/sendfile.rb