Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main = "http://cloud.welcomer.fun:2082"
- fallback = "http://192.187.100.194:2082"
- os.loadAPI("json")
- _G.ccloud = {}
- _G.ccloud.username = ""
- _G.ccloud.token = ""
- _G.ccloud.id = ""
- _G.ccloud.fallback = true
- function _G.ccloud.post(_sPath,_sData,_tHeader)
- local _URL
- local _HTTP_HANDLE
- local _HTTP_RETURN
- if _G.ccloud.fallback == true then
- _URL = fallback .. _sPath
- else
- _URL = main .. _sPath
- end
- _HTTP_HANDLE = http.post(_URL,_sData,_tHeader)
- if _HTTP_HANDLE == nil then
- if _G.ccloud.fallback == true then
- return '{"error": true, "reason": "Unable to contact the server"}'
- end
- _G.ccloud.fallback = true
- return _G.ccloud.post(_sPath,_sData,_tHeader)
- else
- _HTTP_RETURN = _HTTP_HANDLE.readAll()
- _HTTP_HANDLE.close()
- return _HTTP_RETURN
- end
- end
- function _G.ccloud.login(_sUsername,_sPassword)
- local _RETURN_DEC
- local _HTTP_RETURN
- _HTTP_RETURN = _G.ccloud.post("go","username=" .. _sUsername .. "&password=" .. _sPassword,{})
- _RETURN_DEC = json.decode(_HTTP_RETURN)
- if _RETURN_DEC['error'] == true then
- return false, _RETURN_DEC['reason']
- else
- _G.ccloud.username = _sUsername
- _G.ccloud.id = _RETURN_DEC['id']
- _G.ccloud.token = _RETURN_DEC['token']
- return true, _RETURN_DEC['id']
- end
- end
- function _G.ccloud.directory(_sPath)
- local _RETURN_DEC
- local _HTTP_RETURN
- local _HEADER = {}
- _HEADER['Authorization'] = _G.ccloud.token
- _HTTP_RETURN = _G.ccloud.post("dir","path=" .. _sPath,_HEADER)
- _RETURN_DEC = json.decode(_HTTP_RETURN)
- if _RETURN_DEC['error'] == true then
- return false, _RETURN_DEC['reason']
- else
- return true, _RETURN_DEC['content']
- end
- end
- function _G.ccloud.getsize(_sPath)
- local _RETURN_DEC
- local _HTTP_RETURN
- local _HEADER = {}
- _HEADER['Authorization'] = _G.ccloud.token
- _HTTP_RETURN = _G.ccloud.post("size","path=" .. _sPath,_HEADER)
- _RETURN_DEC = json.decode(_HTTP_RETURN)
- if _RETURN_DEC['error'] == true then
- return false, _RETURN_DEC['reason']
- else
- return true, _RETURN_DEC['size']
- end
- end
- function _G.ccloud.getfile(_sPath)
- local _RETURN_DEC
- local _HTTP_RETURN
- local _HEADER = {}
- _HEADER['Authorization'] = _G.ccloud.token
- _HTTP_RETURN = _G.ccloud.post("get","path=" .. _sPath,_HEADER)
- _RETURN_DEC = json.decode(_HTTP_RETURN)
- if _RETURN_DEC['error'] == true then
- return false, _RETURN_DEC['reason']
- else
- return true, _RETURN_DEC['content']
- end
- end
- function _G.ccloud.deletefile(_sPath)
- local _RETURN_DEC
- local _HTTP_RETURN
- local _HEADER = {}
- _HEADER['Authorization'] = _G.ccloud.token
- _HTTP_RETURN = _G.ccloud.post("delete","path=" .. _sPath,_HEADER)
- _RETURN_DEC = json.decode(_HTTP_RETURN)
- if _RETURN_DEC['error'] == true then
- return false, _RETURN_DEC['reason']
- else
- return true, ""
- end
- end
- function _G.ccloud.uploadfile(_sPath,_sUploadFrom)
- local _RETURN_DEC
- local _HTTP_RETURN
- local _FILE_DATA
- local _HEADER = {}
- local _FILE = fs.open(_sUploadFrom,"r")
- _FILE_DATA = _FILE.readAll()
- _FILE.close()
- _HEADER['Authorization'] = _G.ccloud.token
- _HTTP_RETURN = _G.ccloud.post("upload","path=" .. _sPath .. "&data=" .. textutils.urlEncode(_FILE_DATA),_HEADER)
- _RETURN_DEC = json.decode(_HTTP_RETURN)
- if _RETURN_DEC['error'] == true then
- return false, _RETURN_DEC['reason']
- else
- return true, ""
- end
- end
- function _G.ccloud.uploadstring(_sPath,_sString)
- local _RETURN_DEC
- local _HTTP_RETURN
- local _HEADER = {}
- _HEADER['Authorization'] = _G.ccloud.token
- _HTTP_RETURN = _G.ccloud.post("upload","path=" .. _sPath .. "&data=" .. textutils.urlEncode(_sString),_HEADER)
- _RETURN_DEC = json.decode(_HTTP_RETURN)
- if _RETURN_DEC['error'] == true then
- return false, _RETURN_DEC['reason']
- else
- return true, ""
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement