I am trying to download a file from url = https://dropbox.com/xxxxxxxxxxxdl=1 and save the file on the gateway to a local file using tlsv1_2 or tlsv1_3. From the above code it seems the following should work but I do not understand how sendString should be configured.
– downloadfile - downloads a file to the gateway
– Returns:
– result - result of this function
– filename - name of file
– fulllocalpath - path where file is stored locally
– resultstr - status info
local function downloadfile(filename, url, isbinary)
local string1 = " Downloading file from: " … url
–print(“UMONIoT”,“INFO”, string1)
-- retrieve the content of a URL
local rawname = url
local fname = filename --rawname:match("[%w+.%w]+$")
local fulllocalpath = FTPATH .. fname
log("UMONIoT","INFO", "Starting download to local directory: " .. fulllocalpath)
local resultstr = "Starting download to local directory: " .. fulllocalpath .."\r"
--local response, resultcode
local response_body = {}
local response, resultcode, respheaders, respstatus;
if rawname:match("https://") then
local https = require'ssl.https'
--response, resultcode = https.request(url) --TLZ commented out v1.4.6 5/9/23, replaced with following code to enable file transfer using tlsv1_2
response, resultcode, respheaders, respstatus = https.request{
--response, resultcode = https.request{
url = rawname,
protocol = 'tlsv1_2',
method = 'POST',
headers = {
[“content-type”] = “application/x-www-form-urlencoded”,
[“content-length”] = string.len(sendString) --<---What is sendString????
},
sink = ltn12.sink.table(response_body),
source = ltn12.sourcee.string(sendString)
}
--p(response, resultcode)
log("UMONIoT","INFO", "File download response [" .. response .. "].")
log("UMONIoT","INFO", "File download resultcode [" .. resultcode .. "].")
--log("UMONIoT","INFO", "File download respheaders [" .. respheaders .. "].") causes error
log("UMONIoT","INFO", "File download repstatus [" .. respstatus .. "].")
--log("UMONIoT","INFO", "File download response_body [" .. response_body .. "].") causes error
else
local http = require("socket.http")
response, resultcode = http.request(url)
--p(response, resultcode)
end
if resultcode ~= 200 then
log("UMONIoT","ERROR", "Failed to download file. Error code [" .. resultcode .. "].")
resultstr = resultstr .. "Failed to download file. Error code [" .. resultcode .. "]."
return false, nil, nil, resultstr
else
-- save the content to a file
if isbinary == true then
local f = assert(io.open(fulllocalpath, 'wb')) -- open in "binary" mode
f:write(response)
f:close()
else
local f = assert(io.open(fulllocalpath, 'w'))
if fname:match("SETUP") then
local newresponse = updateIPandCredentials(response)
f:write(newresponse)
f:close()
else
f:write(response)
f:close()
end
end
log("UMONIoT","INFO", "Successfully downloaded file: " .. fname .. " to gateway.")
resultstr = resultstr .. "Successfully downloaded file: " .. fname .. " to gateway."
return true, filename ,fulllocalpath, resultstr
end
end
Can you provide actual source code for this example please? For example, how to build sendString? The code shown above seems out of order. I am a true newbie.