Reinified

gFile - Server

Jun 20th, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Server for gFile. By Reinified.
  2. log = fs.open("log", "a")
  3. rednet.open("bottom")
  4. term.clear()
  5. term.setCursorPos(1,1)
  6. print("Hosting server. CTRL + T to stop.")
  7. while true do
  8. local uploaded = false
  9. log.close()
  10. a1, a3 = rednet.receive("gfile")
  11.  
  12. -- Uploading
  13. if a3 == "upload" then
  14.     print(a1)
  15.     rednet.send(a1, "310", "gfile")
  16.     print("Sent continue 310")
  17.     id, msg = rednet.receive(15)
  18.     if msg then
  19.         if fs.exists(msg) then
  20.             rednet.send(a1, "560", "gfile")
  21.         else
  22.             rednet.send(a1, "311", "gfile")
  23.             fName = msg
  24.             id, msg = rednet.receive(5)
  25.             hUP = fs.open(fName, "w")
  26.             hUP.write(msg)
  27.             hUP.close()
  28.             rednet.send(a1, "200", "gfile")
  29.             print(a1.." Successfully uploaded file "..fName)
  30.             uploaded = true
  31.             end
  32.     elseif id ~= nil then
  33.         print("Disconnected from "..a1.." due to inactivity.")
  34.         end
  35.    
  36. -- Downloading
  37. else
  38. log = fs.open("log", "a")
  39. if uploaded == false then
  40. aTime = os.time().."-"..os.day()
  41. print(aTime.." - Serving request from "..a1.." for file "..a3)
  42. reqfile = a3
  43. if fs.exists(reqfile) then
  44. if not fs.isDir(reqfile) then
  45. hRF = fs.open(reqfile, "r")
  46. cont = hRF.readAll()
  47. hRF.close()
  48. rednet.send(a1, cont, "gfile")
  49. log.writeLine(aTime.." - Successfully served "..a1.." file "..a3)
  50. else
  51. rednet.send(a1, "404a", "gfile")
  52. log.writeLine(aTime.." - Denied service to "..a1..", Tried to request directory")
  53. end
  54. else
  55. rednet.send(a1, "404", "gfile")
  56. log.writeLine(aTime.." - Denied service to "..a1..", Reason 404")
  57. end
  58. end
  59. end
  60. end
Advertisement
Add Comment
Please, Sign In to add comment