Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Server for gFile. By Reinified.
- log = fs.open("log", "a")
- rednet.open("bottom")
- term.clear()
- term.setCursorPos(1,1)
- print("Hosting server. CTRL + T to stop.")
- while true do
- local uploaded = false
- log.close()
- a1, a3 = rednet.receive("gfile")
- -- Uploading
- if a3 == "upload" then
- print(a1)
- rednet.send(a1, "310", "gfile")
- print("Sent continue 310")
- id, msg = rednet.receive(15)
- if msg then
- if fs.exists(msg) then
- rednet.send(a1, "560", "gfile")
- else
- rednet.send(a1, "311", "gfile")
- fName = msg
- id, msg = rednet.receive(5)
- hUP = fs.open(fName, "w")
- hUP.write(msg)
- hUP.close()
- rednet.send(a1, "200", "gfile")
- print(a1.." Successfully uploaded file "..fName)
- uploaded = true
- end
- elseif id ~= nil then
- print("Disconnected from "..a1.." due to inactivity.")
- end
- -- Downloading
- else
- log = fs.open("log", "a")
- if uploaded == false then
- aTime = os.time().."-"..os.day()
- print(aTime.." - Serving request from "..a1.." for file "..a3)
- reqfile = a3
- if fs.exists(reqfile) then
- if not fs.isDir(reqfile) then
- hRF = fs.open(reqfile, "r")
- cont = hRF.readAll()
- hRF.close()
- rednet.send(a1, cont, "gfile")
- log.writeLine(aTime.." - Successfully served "..a1.." file "..a3)
- else
- rednet.send(a1, "404a", "gfile")
- log.writeLine(aTime.." - Denied service to "..a1..", Tried to request directory")
- end
- else
- rednet.send(a1, "404", "gfile")
- log.writeLine(aTime.." - Denied service to "..a1..", Reason 404")
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment