--****************************************************************************
-- @file Movie script "SetFileNameSync"
-- @author Valentin Schmidt
-- @version 0.2
-- @requires xtra("Curl")
--
-- Usage:
-- res = setFileName(member("map"), "https://domain.com/foo/bar.w3d")
-- if res then
-- alert("Success!")
-- else
-- alert("Error:" && res)
-- end if
--****************************************************************************
----------------------------------------
-- Downloads and assigns URL to member or castlib synchronously.
-- You can optionally specify a timeout in seconds (default: no timeout)
-- Returns TRUE if successful, error string in case of failure
----------------------------------------
on setFileName (aMemOrCastlibRef, aUrl, timeoutSec)
-- extract filename from URL
od = _player.itemDelimiter
_player.itemDelimiter = "/"
localFile = curl_getEnvVar("TMP") & "\" & the last item of aUrl
_player.itemDelimiter = od
-- download the file
ch = xtra("Curl").new()
ch.setOption(10018, "Curl Xtra") -- USERAGENT
ch.setOption(10002, aUrl) -- URL
if integerP(timeoutSec) then ch.setOption(13, timeoutSec) -- TIMEOUT
ch.setDestinationFile(localFile)
res = ch.exec()
if res=0 then
response_code = ch.getInfo(2097154) -- RESPONSE_CODE
if response_code>=200 AND response_code<300 then
aMemOrCastlibRef.filename = localFile
return TRUE
else
return "Server returned HTTP status code" && response_code
end if
else
return curl_error(res)
end if
end