Advertisement
kssr3951

uld

May 1st, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.97 KB | None | 0 0
  1. -- uld
  2. -- Simple upload program for Computercraft
  3. -- ============================================
  4. -- == config
  5. -- ============================================
  6. URL = "http://zzz.yyy.example.com/ulddld.php"
  7. PASSWORD = "xxxxxx"
  8. -- ============================================
  9. -- == utilities
  10. -- ============================================
  11. local function fileReadAll(filePath)
  12.   local hFile = fs.open(filePath, "r")
  13.   local txt = hFile.readAll()
  14.   hFile.close()
  15.   return txt
  16. end
  17. local function makePostText(tbl)
  18.   rslt = ""
  19.   for key, val in pairs(tbl) do
  20.     if ("" ~= rslt) then
  21.       rslt = rslt .. "&"
  22.     end
  23.     rslt = rslt ..
  24.       textutils.urlEncode(tostring(key)) ..
  25.       "=" ..
  26.       textutils.urlEncode(tostring(val))
  27.   end
  28.   return rslt
  29. end
  30. local function httpPost(url, data)
  31.   local file = http.post(url, makePostText(data))
  32.   local rslt
  33.   if file ~= nil then
  34.     rslt = file.readAll()
  35.     file.close()
  36.   else
  37.     rslt = ""
  38.   end
  39.   return rslt
  40. end
  41. local function getOneLine(src, start)
  42.   local nl = string.find(src, "\n", start)
  43.   if nil == nl then
  44.     return string.sub(src, start), nil
  45.   else
  46.     local next_pos = nl
  47.     if string.len(src) < next_pos then
  48.       next_pos = nil
  49.       end
  50.     return string.sub(src, start, nl - 1), next_pos
  51.   end
  52. end
  53. -- ============================================
  54. -- == main
  55. -- ============================================
  56. args = { ... }
  57. if 0 == #args then
  58.   print("usage")
  59.   print("  uld <filename>")
  60.   return
  61. end
  62. fileName = args[1]
  63. if false == fs.exists(fileName) then
  64.   print("file not found.")
  65.   return
  66. end
  67. data = {}
  68. data.command = "uld"
  69. data.password = PASSWORD
  70. data.fileName = fileName
  71. data.fileData = fileReadAll(fileName)
  72. response = httpPost(URL, data)
  73. -- line 1 : OK or NG
  74. -- line 2 : when NG -> error message
  75. line1, nxt = getOneLine(response, 1)
  76. if ("NG" == line1) then
  77.   line2, nxt = getOneLine(response, nxt)
  78.   return
  79. elseif ("OK" == line1) then
  80.   print("upload success.")
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement