Class: Zget::FileHandler
- Inherits:
-
WEBrick::HTTPServlet::DefaultFileHandler
- Object
- WEBrick::HTTPServlet::DefaultFileHandler
- Zget::FileHandler
- Defined in:
- lib/zget/file_handler.rb
Overview
We want this server to shutdown as soon as the file is downloaded
Instance Method Summary collapse
- #do_GET(req, res) ⇒ Object
-
#open(path, mode) ⇒ Object
We want to trick ‘Kernel::open` in order to display the upload progress.
Instance Method Details
#do_GET(req, res) ⇒ Object
21 22 23 24 25 |
# File 'lib/zget/file_handler.rb', line 21 def do_GET(req, res) res["Content-Disposition"] = "inline; filename=#{File.basename @local_path}" super @server.shutdown end |
#open(path, mode) ⇒ Object
We want to trick ‘Kernel::open` in order to display the upload progress. We do so by reading the file in chunks rather than the whole file. Why trick `Kernel::open`? See: github.com/ruby/ruby/blob/trunk/lib/webrick/httpservlet/filehandler.rb#L61
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/zget/file_handler.rb', line 7 def open(path, mode) st = File::stat(@local_path) Kernel.open(path, mode) do |io| data = "" while d = io.read(1024 * 8) data << d percentage = data.size * 100 / st.size print "\rUploading: #{percentage}%" puts "\n" if percentage == 100 end data end end |