--!movie
--!encoding=utf-8
global $
global gCh
global gFile
----------------------------------------
-- Download file with ftp (asynchronous/non-blocking mode)
-- If local file exists already (partly), download is resumed
----------------------------------------
on startMovie
_player.debugPlaybackEnabled = 1
-- libs
$.import("curl")
$.import("file")
-- config
tRemoteFile = "ftp://ftp.fu-berlin.de/pub/du-k.gz"
tLocalFile = $.PATH & "du-k.gz"
tLocalSize = $.file.size(tLocalFile)
if tLocalSize>0 then
gFile = $.file.fopen(tLocalFile, "ab") -- append data to existing file
else
gFile = $.file.fopen(tLocalFile, "wb")
end if
-- get a CURL handle (xtra instance)
gCh = $.curl.init()
-- specify options
gCh.setOption($.curl.CURLOPT.URL, tRemoteFile)
--gCh.setOption($.curl.CURLOPT.USERNAME, tUsername)
--gCh.setOption($.curl.CURLOPT.PASSWORD, tPassword)
gCh.setOption($.curl.CURLOPT.NOPROGRESS, 0)
if tLocalSize>0 then
put "FILE EXISTS, RESUMING DOWNLOAD"
gCh.setOption($.curl.CURLOPT.RESUME_FROM, tLocalSize)
-- for files > 2 GB you have to use RESUME_FROM_LARGE with a float value instead
-- (note that handling such large files also requires using $.binfile instead of $.file)
-- gCh.setOption($.curl.CURLOPT.RESUME_FROM_LARGE, float(tLocalSize))
end if
gCh.setProgressCallback(#slotCurlProgress)
-- returnMode: 0=return error code (=default), 1=return data, 2=return chunks immediately
$.curl.execAsyncDetached(gCh, #slotCurlData, _movie, 2)
end
----------------------------------------
-- @callback
----------------------------------------
on slotCurlData (res)
if ilk(res)=#bytearray then -- next data chunk arrived
$.file.fwritebytes(gFile, res)
else -- either an error occured or download is finished
put "DOWNLOAD FINISHED"
put "ERROR:" && curl_error(res)
--put "CURLINFO_CONTENT_LENGTH_DOWNLOAD:" && gCh.getInfo($.curl.CURLINFO.CONTENT_LENGTH_DOWNLOAD)
--put "SPEED_DOWNLOAD:" && gCh.getInfo($.curl.CURLINFO.SPEED_DOWNLOAD)
--put "TOTAL_TIME:" && gCh.getInfo($.curl.CURLINFO.TOTAL_TIME)
-- clean up
$.file.fclose(gFile)
gFile = VOID
gCh = VOID
end if
end
----------------------------------------
-- @callback
----------------------------------------
on slotCurlProgress (dltotal, dlnow, ultotal, ulnow)
--put dltotal, dlnow, ultotal, ulnow
if dltotal>0 then put (dlnow*100/dltotal) && "%"
end