1. --****************************************************************************
  2. -- @file     Movie script "SetFileNameSync"
  3. -- @author   Valentin Schmidt
  4. -- @version  0.2
  5. -- @requires xtra("Curl")
  6. --
  7. -- Usage:
  8. -- res = setFileName(member("map"), "https://domain.com/foo/bar.w3d")
  9. -- if res then
  10. --   alert("Success!")
  11. -- else
  12. --   alert("Error:" && res)
  13. -- end if
  14. --****************************************************************************
  15.  
  16. ----------------------------------------
  17. -- Downloads and assigns URL to member or castlib synchronously.
  18. -- You can optionally specify a timeout in seconds (default: no timeout)
  19. -- Returns TRUE if successful, error string in case of failure
  20. ----------------------------------------
  21. on setFileName (aMemOrCastlibRef, aUrl, timeoutSec)
  22.   -- extract filename from URL
  23.   od = _player.itemDelimiter
  24.   _player.itemDelimiter = "/"
  25.   localFile = curl_getEnvVar("TMP") & "\" & the last item of aUrl
  26.   _player.itemDelimiter = od
  27.   -- download the file
  28.   ch = xtra("Curl").new()
  29.   ch.setOption(10018, "Curl Xtra") -- USERAGENT
  30.   ch.setOption(10002, aUrl) -- URL
  31.   if integerP(timeoutSec) then ch.setOption(13, timeoutSec) -- TIMEOUT
  32.   ch.setDestinationFile(localFile)
  33.   res = ch.exec()
  34.   if res=0 then
  35.     response_code = ch.getInfo(2097154) -- RESPONSE_CODE
  36.     if response_code>=200 AND response_code<300 then
  37.       aMemOrCastlibRef.filename = localFile
  38.       return TRUE
  39.     else
  40.       return "Server returned HTTP status code" && response_code
  41.     end if
  42.   else
  43.     return curl_error(res)
  44.   end if
  45. end
  46.  
[raw code]