Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- File Server by PaymentOption for NeXuS
- VERSION = 1.0
- ---------------------------------------------
- serverID = os.getComputerID()
- rednet.open("top")
- rednet.open("right")
- rednet.open("left")
- rednet.open("back")
- rednet.open("bottom")
- -- Create File Directories
- shell.run("mkdir", "NeXuS/FileServer")
- shell.run("mkdir", "NeXuS/FileServer/Files")
- --
- tFiles = {}
- sFiles = "" --For sending
- -- LUA FUNCTIONS NOT WRITTEN BY ME --
- function TBLval_to_str ( v )
- if "string" == type( v ) then
- v = string.gsub( v, "\n", "\\n" )
- if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
- return "'" .. v .. "'"
- end
- return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
- else
- return "table" == type( v ) and TBLtostring( v ) or
- tostring( v )
- end
- end
- function TBLkey_to_str ( k )
- if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
- return k
- else
- return "[" .. TBLval_to_str( k ) .. "]"
- end
- end
- function TBLtostring( tbl )
- local result, done = {}, {}
- for k, v in ipairs( tbl ) do
- table.insert( result, TBLval_to_str( v ) )
- done[ k ] = true
- end
- for k, v in pairs( tbl ) do
- if not done[ k ] then
- table.insert( result,
- TBLkey_to_str( k ) .. "=" .. TBLval_to_str( v ) )
- end
- end
- return table.concat( result, "\n" )
- end
- -- END --
- function downloadFile(filename, origSender)
- if fs.exists("NeXuS/FileServer/Files/"..filename) == false then
- cPrint(10, "Failed: File does not exist")
- rednet.send(origSender, "failed")
- sleep(2)
- else
- file = fs.open("NeXuS/FileServer/Files/"..filename, "r")
- fileContents = file.readAll()
- file.close()
- rednet.send(origSender, fileContents)
- cPrint(10, "Successfull send")
- sleep(2)
- end
- end
- function uploadFile(origSender)
- rednet.send(origSender, "ready")
- sender, filename = rednet.receive(0.8)
- if sender == origSender then
- local file = fs.open("NeXuS/FileServer/Files/"..filename, "w")
- rednet.send(origSender, "ready")
- sender, fileContents = rednet.receive(0.8)
- if sender == origSender then
- file.write(tostring(fileContents))
- file.close()
- else cPrint(7, "Failure: Request conjestion"); sleep(0.5); end
- else cPrint(7, "Failure: Request conjestion"); sleep(0.5); end
- end
- function cPrint(height, string)
- local w, h = term.getSize()
- local xPosition = w/2 - string.len(string)/2
- term.setCursorPos(xPosition, height)
- term.write(string)
- end
- function rPrint(height, string)
- local w,h = term.getSize()
- local xPos = w - string.len(string)
- term.setCursorPos(xPos, height)
- term.write(string)
- end
- function printMenu()
- rPrint(4, "NeXuS File Server ")
- rPrint(5, "Version: "..VERSION.." ")
- end
- function printLogo()
- term.clear(); term.setCursorPos(1,1)
- print(" _______ ____ ___ _________")
- print(" \\ \\ ____ \\ \\/ /__ __/ _____/")
- print(" / | \\_/ __ \\ \\ /| | \\_____ \\ ")
- print(" / | \\ ___/ / \\| | / \\")
- print(" \\____|__ /\\___ >/___/\\ \\____/_______ /")
- print(" \\/ \\/ \\_/ \\/ ")
- cPrint(15, "File Server by PaymentOption"); sleep(3)
- end
- printLogo()
- while true do
- term.clear()
- term.setCursorPos(1,1)
- tFiles = fs.list("NeXuS/FileServer/Files/")
- sFiles = TBLtostring(tFiles)
- printMenu()
- cPrint(8, "Awaiting requests...")
- sender, message = rednet.receive()
- if message == "download" then
- rednet.send(sender, "ready")
- sender, filename = rednet.receive(0.8)
- downloadFile(filename, sender)
- end
- if message == "upload" then
- cPrint(7, "Upload request from "..sender)
- uploadFile(sender)
- end
- if message == "files" then
- rednet.send(sender, sFiles)
- end
- end
Add Comment
Please, Sign In to add comment