file_list.lua 961 B

12345678910111213141516171819202122232425
  1. return function (connection, req, 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. local remaining, used, total=file.fsinfo()
  9. connection:send("<b>Total size: </b> " .. total .. " bytes<br/>\n" ..
  10. "<b>In Use: </b> " .. used .. " bytes<br/>\n" ..
  11. "<b>Free: </b> " .. remaining .. " bytes<br/>\n" ..
  12. "<p>\n<b>Files:</b><br/>\n<ul>\n")
  13. for name, size in pairs(file.list()) do
  14. local isHttpFile = string.match(name, "(http/)") ~= nil
  15. if isHttpFile then
  16. local url = string.match(name, ".*/(.*)")
  17. connection:send(' <li><a href="' .. url .. '">' .. url .. "</a> (" .. size .. " bytes)</li>\n")
  18. end
  19. end
  20. connection:send("</ul>\n</p>\n</body></html>")
  21. end