mirror of
https://github.com/Kiritow/OpenComputerScripts.git
synced 2024-03-22 13:10:46 +08:00
Add libdownloader
This commit is contained in:
parent
13f67e9773
commit
ae50142ab0
|
@ -1,56 +0,0 @@
|
|||
local component=require("component")
|
||||
|
||||
local function doRealDownload(url)
|
||||
if(component.internet==nil) then
|
||||
error("The downloader requires an Internet card.")
|
||||
end
|
||||
|
||||
local handle=component.internet.request(url)
|
||||
|
||||
while true do
|
||||
local ret,err=handle.finishConnect()
|
||||
if(ret==nil) then
|
||||
return false,err
|
||||
elseif(ret==true) then
|
||||
break
|
||||
end
|
||||
--os.sleep(0.1)
|
||||
end
|
||||
|
||||
local response_code=handle.response()
|
||||
|
||||
local ans=""
|
||||
while true do
|
||||
local tmp=handle.read()
|
||||
if(tmp==nil) then break end
|
||||
ans=ans .. tmp
|
||||
end
|
||||
handle.close()
|
||||
|
||||
return true,ans,response_code
|
||||
end
|
||||
|
||||
function DownloadFromGitHub(RepoName,Branch,FileAddress)
|
||||
local url="https://raw.githubusercontent.com/" .. RepoName .. "/" .. Branch .. "/" .. FileAddress
|
||||
return doRealDownload(url)
|
||||
end
|
||||
|
||||
function DownloadFromOCS(FileAddress)
|
||||
return DownloadFromGitHub("Kiritow/OpenComputerScripts","master",FileAddress)
|
||||
end
|
||||
|
||||
function WriteStringToFile(StringValue,FileName,IsAppend)
|
||||
if(IsAppend==nil) then IsAppend=false end
|
||||
local handle,err
|
||||
if(IsAppend) then
|
||||
handle,err=io.open(FileName,"a")
|
||||
else
|
||||
handle,err=io.open(FileName,"w")
|
||||
end
|
||||
if(handle==nil) then return false,err end
|
||||
|
||||
handle:write(StringValue)
|
||||
handle:close()
|
||||
|
||||
return true,"Success"
|
||||
end
|
47
libs/libdownload.lua
Normal file
47
libs/libdownload.lua
Normal file
|
@ -0,0 +1,47 @@
|
|||
local component=require("component")
|
||||
|
||||
local function Download(url,param)
|
||||
param = param or {}
|
||||
local device=param["device"] or component.internet
|
||||
if(device==nil) then
|
||||
error("The downloader requires an Internet card.")
|
||||
end
|
||||
local handle=device.request(url)
|
||||
while true do
|
||||
local ret,err=handle.finishConnect()
|
||||
if(ret==nil) then
|
||||
return false,err
|
||||
elseif(ret) then
|
||||
break
|
||||
end
|
||||
if(param["dosleep"]) then
|
||||
os.sleep(type(param["dosleep"]=="number") and param["dosleep"] or 0.05)
|
||||
end
|
||||
end
|
||||
local code,msg,headers=handle.response()
|
||||
if(param["onresponse"]) then
|
||||
if(param["onresponse"](code,msg,headers)) then
|
||||
handle.close()
|
||||
return false,"terminated by onresponse callback."
|
||||
end
|
||||
end
|
||||
local ans=''
|
||||
while true do
|
||||
local tmp=handle.read(type(param["readBuff"])=="number" and param["readBuff"] or 10240)
|
||||
if(tmp==nil) then break
|
||||
elseif(tmp~='') then
|
||||
if(param["ondata"]) then
|
||||
param["ondata"](tmp)
|
||||
else
|
||||
ans=ans .. tmp
|
||||
end
|
||||
end
|
||||
end
|
||||
handle.close()
|
||||
|
||||
return true,ans,code
|
||||
end
|
||||
|
||||
return {
|
||||
["download"]=Download
|
||||
}
|
|
@ -87,6 +87,13 @@
|
|||
"libkeepup"
|
||||
}
|
||||
},
|
||||
["libdownload"]={
|
||||
title="Download Library",
|
||||
info="Download library for general use",
|
||||
files={
|
||||
["libs/libdownload.lua"]="__lib__/"
|
||||
}
|
||||
},
|
||||
["libhuffman"]={
|
||||
title="Huffman compression library",
|
||||
info="Inflate and deflate in Huffman algorithm.",
|
||||
|
|
Loading…
Reference in New Issue
Block a user