file_list.lua 1.3 KB

123456789101112131415161718192021222324252627282930
  1. return function (connection, args)
  2. connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n")
  3. connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Server File Listing</title></head>')
  4. connection:send('<body>')
  5. coroutine.yield()
  6. connection:send('<h1>Server File Listing</h1>')
  7. local remaining, used, total=file.fsinfo()
  8. connection:send("<b>Total size: </b> " .. total .. " bytes<br/>\n")
  9. connection:send("<b>In Use: </b> " .. used .. " bytes<br/>\n")
  10. connection:send("<b>Free: </b> " .. remaining .. " bytes<br/>\n")
  11. connection:send("<p>\n")
  12. connection:send("<b>Files:</b><br/>\n")
  13. connection:send("<ul>\n")
  14. for name, size in pairs(file.list()) do
  15. local isHttpFile = string.match(name, "(http/)") ~= nil
  16. if isHttpFile then
  17. local url = string.match(name, ".*/(.*)")
  18. connection:send(' <li><a href="' .. url .. '">' .. url .. "</a> (" .. size .. " bytes)</li>\n")
  19. -- this list could be very long, so we'll yield in order to avoid overflowing the send buffer.
  20. coroutine.yield()
  21. end
  22. end
  23. connection:send("</ul>\n")
  24. connection:send("</p>\n")
  25. connection:send('</body></html>')
  26. end