file_list.lua 1.0 KB

12345678910111213141516171819202122232425262728
  1. return function (connection, args)
  2. dofile("httpserver-header.lc")(connection, 200, 'html')
  3. connection:send([===[
  4. <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Server File Listing</title></head>
  5. <body>
  6. <h1>Server File Listing</h1>
  7. ]===])
  8. coroutine.yield()
  9. local remaining, used, total=file.fsinfo()
  10. connection:send("<b>Total size: </b> " .. total .. " bytes<br/>\n" ..
  11. "<b>In Use: </b> " .. used .. " bytes<br/>\n" ..
  12. "<b>Free: </b> " .. remaining .. " bytes<br/>\n" ..
  13. "<p>\n<b>Files:</b><br/>\n<ul>\n")
  14. coroutine.yield()
  15. for name, size in pairs(file.list()) do
  16. local isHttpFile = string.match(name, "(http/)") ~= nil
  17. if isHttpFile then
  18. local url = string.match(name, ".*/(.*)")
  19. connection:send(' <li><a href="' .. url .. '">' .. url .. "</a> (" .. size .. " bytes)</li>\n")
  20. coroutine.yield()
  21. end
  22. end
  23. connection:send("</ul>\n</p>\n</body></html>")
  24. end