Advertisement
Guest User

CCloud

a guest
Feb 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. main = "http://cloud.welcomer.fun:2082/"
  2. fallback = "http://192.187.100.194:2082/"
  3. os.loadAPI("json")
  4.  
  5. logf = fs.open("log.txt","a")
  6.  
  7. function log(line)
  8.     if line ~= "" and line then
  9.         logf.writeLine("[" .. os.time() .. "]" .. tostring(line))
  10.         logf.flush()
  11.     end
  12. end
  13.  
  14. log("Start")
  15.  
  16. _G.ccloud = {}
  17. _G.ccloud.username = ""
  18. _G.ccloud.token = ""
  19. _G.ccloud.id = ""
  20. _G.ccloud.fallback = true
  21. _G.ccloud.log = log
  22.  
  23. function _G.ccloud.post(_sPath,_sData,_tHeader)
  24.     local _URL
  25.     local _HTTP_HANDLE
  26.     local _HTTP_RETURN
  27.     if _G.ccloud.fallback == true then
  28.         _URL = fallback .. _sPath
  29.     else
  30.         _URL = main .. _sPath
  31.     end
  32.  log("Retrieving " .. _URL)
  33.     _HTTP_HANDLE,err = http.post(_URL,_sData,_tHeader)
  34.     if _HTTP_HANDLE == nil then
  35.   log("Failed to retrieve")
  36.   log(err)
  37.         if _G.ccloud.fallback == true then
  38.             return '{"error": true, "reason": "Unable to contact the server"}'
  39.         end
  40.         _G.ccloud.fallback = true
  41.         return _G.ccloud.post(_sPath,_sData,_tHeader)
  42.     else
  43.         _HTTP_RETURN = _HTTP_HANDLE.readAll()
  44.         _HTTP_HANDLE.close()
  45.   log("Success")
  46.   log(_HTTP_RETURN)
  47.         return _HTTP_RETURN
  48.     end
  49. end
  50.  
  51. function _G.ccloud.login(_sUsername,_sPassword)
  52.     local _RETURN_DEC
  53.     local _HTTP_RETURN
  54.     _HTTP_RETURN = _G.ccloud.post("go","username=" .. _sUsername .. "&password=" .. _sPassword,{})
  55.     _RETURN_DEC = json.decode(_HTTP_RETURN)
  56.     if _RETURN_DEC['error'] == true then
  57.         return false, _RETURN_DEC['reason']
  58.     else
  59.         _G.ccloud.username = _sUsername
  60.         _G.ccloud.id = _RETURN_DEC['id']
  61.         _G.ccloud.token =  _RETURN_DEC['token']
  62.         return true, _RETURN_DEC['id']
  63.     end
  64. end
  65.  
  66. function _G.ccloud.directory(_sPath)
  67.     local _RETURN_DEC
  68.     local _HTTP_RETURN
  69.     local _HEADER = {}
  70.     _HEADER['Authorization'] = _G.ccloud.token
  71.     _HTTP_RETURN = _G.ccloud.post("dir","path=" .. _sPath,_HEADER)
  72.     _RETURN_DEC = json.decode(_HTTP_RETURN)
  73.     if _RETURN_DEC['error'] == true then
  74.         return false, _RETURN_DEC['reason']
  75.     else
  76.         return true, _RETURN_DEC['content']
  77.     end
  78. end
  79.  
  80. function _G.ccloud.getsize(_sPath)
  81.     local _RETURN_DEC
  82.     local _HTTP_RETURN
  83.     local _HEADER = {}
  84.     _HEADER['Authorization'] = _G.ccloud.token
  85.     _HTTP_RETURN = _G.ccloud.post("size","path=" .. _sPath,_HEADER)
  86.     _RETURN_DEC = json.decode(_HTTP_RETURN)
  87.     if _RETURN_DEC['error'] == true then
  88.         return false, _RETURN_DEC['reason']
  89.     else
  90.         return true, _RETURN_DEC['size']
  91.     end
  92. end
  93.  
  94. function _G.ccloud.getfile(_sPath)
  95.     local _RETURN_DEC
  96.     local _HTTP_RETURN
  97.     local _HEADER = {}
  98.     _HEADER['Authorization'] = _G.ccloud.token
  99.     _HTTP_RETURN = _G.ccloud.post("get","path=" .. _sPath,_HEADER)
  100.     _RETURN_DEC = json.decode(_HTTP_RETURN)
  101.     if _RETURN_DEC['error'] == true then
  102.         return false, _RETURN_DEC['reason']
  103.     else
  104.         return true, _RETURN_DEC['content']
  105.     end
  106. end
  107.  
  108. function _G.ccloud.deletefile(_sPath)
  109.     local _RETURN_DEC
  110.     local _HTTP_RETURN
  111.     local _HEADER = {}
  112.     _HEADER['Authorization'] = _G.ccloud.token
  113.     _HTTP_RETURN = _G.ccloud.post("delete","path=" .. _sPath,_HEADER)
  114.     _RETURN_DEC = json.decode(_HTTP_RETURN)
  115.     if _RETURN_DEC['error'] == true then
  116.         return false, _RETURN_DEC['reason']
  117.     else
  118.         return true, ""
  119.     end
  120. end
  121.  
  122. function _G.ccloud.uploadfile(_sPath,_sUploadFrom)
  123.     local _RETURN_DEC
  124.     local _HTTP_RETURN
  125.     local _FILE_DATA
  126.     local _HEADER = {}
  127.     local _FILE = fs.open(_sUploadFrom,"r")
  128.     _FILE_DATA = _FILE.readAll()
  129.     _FILE.close()
  130.     _HEADER['Authorization'] = _G.ccloud.token
  131.     _HTTP_RETURN = _G.ccloud.post("upload","path=" .. _sPath .. "&data=" .. textutils.urlEncode(_FILE_DATA),_HEADER)
  132.     _RETURN_DEC = json.decode(_HTTP_RETURN)
  133.     if _RETURN_DEC['error'] == true then
  134.         return false, _RETURN_DEC['reason']
  135.     else
  136.         return true, ""
  137.     end
  138. end
  139.  
  140. function _G.ccloud.uploadstring(_sPath,_sString)
  141.     local _RETURN_DEC
  142.     local _HTTP_RETURN
  143.     local _HEADER = {}
  144.     _HEADER['Authorization'] = _G.ccloud.token
  145.     _HTTP_RETURN = _G.ccloud.post("upload","path=" .. _sPath .. "&data=" .. textutils.urlEncode(_sString),_HEADER)
  146.     _RETURN_DEC = json.decode(_HTTP_RETURN)
  147.     if _RETURN_DEC['error'] == true then
  148.         return false, _RETURN_DEC['reason']
  149.     else
  150.         return true, ""
  151.     end
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement