fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
7e0ab3b
v2.1.4
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 10local dirname =string . gmatch ( filename , "(.*/)([^/]*)" )() 11if dirname then 12local dir_ok =love . filesystem . createDirectory ( dirname ) 13if not dir_ok then error ( "could not create directory " .. dirname ) end 14end 15 16local file =love . filesystem . newFile ( filename ) 17local ok , err =file : open ( "w" ) 18if not ok then error ( err ) end 19 20return function ( chunk , err ) 21if chunk then 22local ok , err =file : write ( chunk , # chunk ) 23if not ok then return nil , err end 24else 25file : close () 26return nil , err 27end 28 29-- indicate that we accept more data 30return true 31end 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 ) 37return function ( chunk , err ) 38if chunk then 39progress_channel : push ( { id =id , event ={ chunk =# chunk }} ) 40elseif er then 41-- if chunk is nil, then the download is finished, or we have an error 42progress_channel : push ( { id =id , event ={ error =err }} ) 43end 44 45-- forward chunk and err to the underlying sink 46return output_sink ( chunk , err ) 47end 48end 49 50-- checks if the file has a redirect and gets the content length 51local function getFinalUrlAndSize ( id , url ) 52local request ={ 53url =url , 54method ="HEAD" , -- this only retrieves the header, which contains the size that we're interested in. 55} 56local success , code , header =https . request ( request ) 57if success then 58if code == 200 then 59local size =header [ "content-length" ] 60progress_channel : push ( { id =id , event ={ size =tonumber ( header [ "content-length" ]) }} ) 61-- we have a valid url and size 62return url , size 63elseif code == 302 then -- check if we already were redirected to prevent infinite redirect loops 64-- url redirected 65local location =header [ "location" ] 66-- get the redirected url size 67return getFinalUrlAndSize ( id , location ) 68else 69progress_channel : push ( { id =id , event ={ code =code , header =header }} ) 70end 71else 72-- in case of failure, the error message is stored in the second return value 73progress_channel : push ( { id =id , event ={ finished =true , error =code }} ) 74end 75 76-- Failed to get a url and or content length 77return nil 78end 79 80while true do 81-- wait for a request to arrive 82local demand =requests_channel : demand () 83 84if demand == false then break end 85 86local id , request =demand . id , demand . request 87 88-- extract the url and output filename 89local url , filename =request . url , request . filename 90 91-- we need to send two separate requests: one to determine the size 92-- of the thing we are downloading, and another one to actually download 93-- something. 94 95url , size =getFinalUrlAndSize ( id , url ) 96 97-- now we start the actual download 98if url then 99-- this is the sink that will capture the downloaded data. 100local output_sink =love_file_sink ( filename ) 101 102-- this time we send an actual GET request. 103-- note also that we use our custom sink to capture the downloaded data. 104local request ={ 105url =url , 106sink =progress_sink ( id , output_sink ), 107method ="GET" , 108} 109 110local success , code , header =https . request ( request ) 111 112if success then 113progress_channel : push ( { id =id , event ={ code =code , header =header }} ) 114else 115-- in case of failure, the error message is stored in the second return value 116progress_channel : push ( { id =id , event ={ error =code }} ) 117end 118end 119 120end