fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosApplication update checker7e0ab3b

v2.1.3
3.7 KiB121 linesraw
1local https = require("https")
2
3-- these two channels are used to send requests and progress updates back and forth
4local requests_channel = love.thread.getChannel("dl-requests")
5local progress_channel = love.thread.getChannel("dl-progress")
6
7-- this is a re-implementation of the ltn12 file sink that uses love.filesystem instead of Lua's io functions
8local function love_file_sink(filename)
9	-- create enclosing directories
10	local dirname = string.gmatch(filename, "(.*/)([^/]*)")()
11	if dirname then
12		local dir_ok = love.filesystem.createDirectory(dirname)
13		if not dir_ok then error("could not create directory "..dirname) end
14	end
15
16	local file = love.filesystem.newFile(filename)
17	local ok, err = file:open("w")
18	if not ok then error(err) end
19
20	return function(chunk, err)
21		if chunk then
22			local ok, err = file:write(chunk, #chunk)
23			if not ok then return nil, err end
24		else
25			file:close()
26			return nil, err
27		end
28
29		-- indicate that we accept more data
30		return true
31	end
32end
33
34-- this is a special "sink" that sends progress updates whenever it receives a new chunk of data.
35-- See http://lua-users.org/wiki/FiltersSourcesAndSinks for a bit of documentation
36local function progress_sink(id, output_sink)
37	return function(chunk, err)
38		if chunk then
39			progress_channel:push({id = id, event = {chunk = #chunk}})
40		elseif er then
41			-- if chunk is nil, then the download is finished, or we have an error
42			progress_channel:push({id = id, event = {error = err}})
43		end
44
45		-- forward chunk and err to the underlying sink
46		return output_sink(chunk, err)
47	end
48end
49
50-- checks if the file has a redirect and gets the content length
51local function getFinalUrlAndSize(id, url)
52	local request = {
53		url = url,
54		method = "HEAD", -- this only retrieves the header, which contains the size that we're interested in.
55	}
56	local success, code, header = https.request(request)
57
58	if success then
59		if code == 200 then
60			local size = header["content-length"]
61			progress_channel:push({id = id, event = {size = tonumber(header["content-length"])}})
62			-- we have a valid url and size
63			return url, size
64		elseif code == 302 then
65			-- url redirected
66			local location = header["location"]
67			-- get the redirected url size
68			return getFinalUrlAndSize(id, location)
69		else
70			progress_channel:push({id = id, event = {code = code, header = header}})
71		end
72	else
73		-- in case of failure, the error message is stored in the second return value
74		progress_channel:push({id = id, event = {finished = true, error = code}})
75	end
76
77	-- Failed to get a url and or content length
78	return nil
79end
80
81while true do
82	-- wait for a request to arrive
83	local demand = requests_channel:demand()
84
85	if demand == false then break end
86
87	local id, request = demand.id, demand.request
88
89	-- extract the url and output filename
90	local url, filename = request.url, request.filename
91
92	-- we need to send two separate requests: one to determine the size
93	-- of the thing we are downloading, and another one to actually download
94	-- something.
95
96	url, size = getFinalUrlAndSize(id, url)
97
98	-- now we start the actual download
99	if url then
100		-- this is the sink that will capture the downloaded data.
101		local output_sink = love_file_sink(filename)
102
103		-- this time we send an actual GET request.
104		-- note also that we use our custom sink to capture the downloaded data.
105		local request = {
106			url = url,
107			sink = progress_sink(id, output_sink),
108			method = "GET",
109		}
110
111		local success, code, header = https.request(request)
112
113		if success then
114			progress_channel:push({id = id, event = {code = code, header = header}})
115		else
116			-- in case of failure, the error message is stored in the second return value
117			progress_channel:push({id = id, event = {error = code}})
118		end
119	end
120
121end