Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- uld
- -- Simple upload program for Computercraft
- -- ============================================
- -- == config
- -- ============================================
- URL = "http://zzz.yyy.example.com/ulddld.php"
- PASSWORD = "xxxxxx"
- -- ============================================
- -- == utilities
- -- ============================================
- local function fileReadAll(filePath)
- local hFile = fs.open(filePath, "r")
- local txt = hFile.readAll()
- hFile.close()
- return txt
- end
- local function makePostText(tbl)
- rslt = ""
- for key, val in pairs(tbl) do
- if ("" ~= rslt) then
- rslt = rslt .. "&"
- end
- rslt = rslt ..
- textutils.urlEncode(tostring(key)) ..
- "=" ..
- textutils.urlEncode(tostring(val))
- end
- return rslt
- end
- local function httpPost(url, data)
- local file = http.post(url, makePostText(data))
- local rslt
- if file ~= nil then
- rslt = file.readAll()
- file.close()
- else
- rslt = ""
- end
- return rslt
- end
- local function getOneLine(src, start)
- local nl = string.find(src, "\n", start)
- if nil == nl then
- return string.sub(src, start), nil
- else
- local next_pos = nl
- if string.len(src) < next_pos then
- next_pos = nil
- end
- return string.sub(src, start, nl - 1), next_pos
- end
- end
- -- ============================================
- -- == main
- -- ============================================
- args = { ... }
- if 0 == #args then
- print("usage")
- print(" uld <filename>")
- return
- end
- fileName = args[1]
- if false == fs.exists(fileName) then
- print("file not found.")
- return
- end
- data = {}
- data.command = "uld"
- data.password = PASSWORD
- data.fileName = fileName
- data.fileData = fileReadAll(fileName)
- response = httpPost(URL, data)
- -- line 1 : OK or NG
- -- line 2 : when NG -> error message
- line1, nxt = getOneLine(response, 1)
- if ("NG" == line1) then
- line2, nxt = getOneLine(response, nxt)
- return
- elseif ("OK" == line1) then
- print("upload success.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement