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 ) 57 58if success then 59if code == 200 then 60local size =header [ "content-length" ] 61progress_channel : push ( { id =id , event ={ size =tonumber ( header [ "content-length" ]) }} ) 62-- we have a valid url and size 63return url , size 64elseif code == 302 then 65-- url redirected 66local location =header [ "location" ] 67-- get the redirected url size 68return getFinalUrlAndSize ( id , location ) 69else 70progress_channel : push ( { id =id , event ={ code =code , header =header }} ) 71end 72else 73-- in case of failure, the error message is stored in the second return value 74progress_channel : push ( { id =id , event ={ finished =true , error =code }} ) 75end 76 77-- Failed to get a url and or content length 78return nil 79end 80 81while true do 82-- wait for a request to arrive 83local demand =requests_channel : demand () 84 85if demand == false then break end 86 87local id , request =demand . id , demand . request 88 89-- extract the url and output filename 90local 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 96url , size =getFinalUrlAndSize ( id , url ) 97 98-- now we start the actual download 99if url then 100-- this is the sink that will capture the downloaded data. 101local 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. 105local request ={ 106url =url , 107sink =progress_sink ( id , output_sink ), 108method ="GET" , 109} 110 111local success , code , header =https . request ( request ) 112 113if success then 114progress_channel : push ( { id =id , event ={ code =code , header =header }} ) 115else 116-- in case of failure, the error message is stored in the second return value 117progress_channel : push ( { id =id , event ={ error =code }} ) 118end 119end 120 121end