fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
7d1f500
settings-overhaul
1-- This code is dedicated to the public domain. 2017, Moritz Neikes 2 3local http =require ( "socket.https" ) 4 5-- these two channels are used to send requests and progress updates back 6-- and forth 7local requests_channel =love . thread . getChannel ( "dl-requests" ) 8local progress_channel =love . thread . getChannel ( "dl-progress" ) 9 10-- this is a re-implementation of the ltn12 file sink that uses love.filesystem 11-- instead of Lua's io functions 12local function love_file_sink ( filename ) 13-- create enclosing directories 14local dirname =string . gmatch ( filename , "(.*/)([^/]*)" )() 15if dirname then 16local dir_ok =love . filesystem . createDirectory ( dirname ) 17if not dir_ok then error ( "Could not create directory " .. dirname ) end 18end 19 20local file =love . filesystem . newFile ( filename ) 21local ok , err =file : open ( "w" ) 22if not ok then error ( err ) end 23 24return function ( chunk , err ) 25if chunk then 26local ok , err =file : write ( chunk , # chunk ) 27if not ok then return nil , err end 28else 29file : close () 30return nil , err 31end 32 33-- indicate that we accept more data 34return true 35end 36end 37 38-- this is a special "sink" that sends progress updates whenever it receives 39-- a new chunk of data. 40-- See http://lua-users.org/wiki/FiltersSourcesAndSinks for a bit of documentation 41local function progress_sink ( output_sink ) 42return function ( chunk , err ) 43if chunk then 44progress_channel : push ( { chunk =# chunk } ) 45else 46 47-- if chunk is nil, then the download is finished, or we have an error 48if err then 49progress_channel : push ( { error =err } ) 50else 51progress_channel : push ( { finished =true } ) 52end 53 54end 55 56-- forward chunk and err to the underlying sink 57return output_sink ( chunk , err ) 58end 59end 60 61while true do 62 63-- wait for a request to arrive 64local request =requests_channel : demand () 65 66-- extract the url and output filename 67local url , filename =request . url , request . filename 68 69-- we need to send two separate requests: one to determine the size 70-- of the thing we are downloading, and another one to actually download 71-- something. 72 73local proceed 74do 75local request ={ 76url =url , 77method ="HEAD" , -- this only retrieves the header, which contains the 78-- size that we're interested in. 79} 80local success , status_code , response_header =http . request ( request ) 81if success then 82if status_code == 200 then 83file_size =response_header [ "content-length" ] 84progress_channel : push ( { file_size =tonumber ( response_header [ "content-length" ]) } ) 85-- we only proceed to sending a second request when this worked fine. 86proceed =true 87else 88progress_channel : push ( { error =string . format ( "Received status code %d" , 89tonumber ( status_code )) } ) 90end 91else 92-- in case of failure, the error message is stored in the second return value 93local err =status_code 94progress_channel : push ( { error =err } ) 95end 96end 97 98-- now we start the actual download 99if proceed 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 ( output_sink ), 108method ="GET" , 109} 110 111local success , status_code =http . request ( request ) 112 113if success then 114if status_code ~= 200 then 115progress_channel : push ( { error =string . format ( "Received status code %d" , 116tonumber ( status_code )) } ) 117end 118else 119-- in case of failure, the error message is stored in the second return value 120local err =status_code 121progress_channel : push ( { error =err } ) 122end 123end 124 125end