1. --!movie
  2. --!encoding=utf-8
  3.  
  4. global $
  5.  
  6. global gCh
  7. global gFile
  8.  
  9. ----------------------------------------
  10. -- Download file with ftp (asynchronous/non-blocking mode)
  11. -- If local file exists already (partly), download is resumed
  12. ----------------------------------------
  13. on startMovie
  14.     _player.debugPlaybackEnabled = 1
  15.  
  16.     -- libs
  17.     $.import("curl")
  18.     $.import("file")
  19.  
  20.     -- config
  21.     tRemoteFile = "ftp://ftp.fu-berlin.de/pub/du-k.gz"
  22.     tLocalFile = $.PATH & "du-k.gz"
  23.  
  24.     tLocalSize = $.file.size(tLocalFile)
  25.     if tLocalSize>0 then
  26.         gFile = $.file.fopen(tLocalFile, "ab") -- append data to existing file
  27.     else
  28.         gFile = $.file.fopen(tLocalFile, "wb")
  29.     end if
  30.  
  31.     -- get a CURL handle (xtra instance)
  32.     gCh = $.curl.init()
  33.  
  34.     -- specify options
  35.     gCh.setOption($.curl.CURLOPT.URL, tRemoteFile)
  36.  
  37.     --gCh.setOption($.curl.CURLOPT.USERNAME, tUsername)
  38.     --gCh.setOption($.curl.CURLOPT.PASSWORD, tPassword)
  39.  
  40.     gCh.setOption($.curl.CURLOPT.NOPROGRESS, 0)
  41.  
  42.     if tLocalSize>0 then
  43.         put "FILE EXISTS, RESUMING DOWNLOAD"
  44.         gCh.setOption($.curl.CURLOPT.RESUME_FROM, tLocalSize)
  45.  
  46.         -- for files > 2 GB you have to use RESUME_FROM_LARGE with a float value instead
  47.         -- (note that handling such large files also requires using $.binfile instead of $.file)
  48.         -- gCh.setOption($.curl.CURLOPT.RESUME_FROM_LARGE, float(tLocalSize))
  49.     end if
  50.  
  51.     gCh.setProgressCallback(#slotCurlProgress)
  52.  
  53.     -- returnMode: 0=return error code (=default), 1=return data, 2=return chunks immediately
  54.     $.curl.execAsyncDetached(gCh, #slotCurlData, _movie, 2)
  55. end
  56.  
  57. ----------------------------------------
  58. -- @callback
  59. ----------------------------------------
  60. on slotCurlData (res)
  61.     if ilk(res)=#bytearray then -- next data chunk arrived
  62.         $.file.fwritebytes(gFile, res)
  63.  
  64.     else -- either an error occured or download is finished
  65.         put "DOWNLOAD FINISHED"
  66.         put "ERROR:" && curl_error(res)
  67.  
  68.         --put "CURLINFO_CONTENT_LENGTH_DOWNLOAD:" && gCh.getInfo($.curl.CURLINFO.CONTENT_LENGTH_DOWNLOAD)
  69.         --put "SPEED_DOWNLOAD:" && gCh.getInfo($.curl.CURLINFO.SPEED_DOWNLOAD)
  70.         --put "TOTAL_TIME:" && gCh.getInfo($.curl.CURLINFO.TOTAL_TIME)
  71.  
  72.         -- clean up
  73.         $.file.fclose(gFile)
  74.         gFile = VOID
  75.         gCh = VOID
  76.     end if
  77. end
  78.  
  79. ----------------------------------------
  80. -- @callback
  81. ----------------------------------------
  82. on slotCurlProgress (dltotal, dlnow, ultotal, ulnow)
  83.     --put dltotal, dlnow, ultotal, ulnow
  84.     if dltotal>0 then put (dlnow*100/dltotal) && "%"
  85. end
  86.  
[raw code]