## Downloading from Dropbox using cURL in Lua

pixelstuff
Posts: 41
Joined: Sun Jul 15, 2018 11:50 am
Been thanked: 1 time

### Downloading from Dropbox using cURL in Lua

I couldn't find reference to cURL file downloading and saving, but I did find the Reactor download.lua example.

I have adapted it to work with a function, its working it currently, downloads from a dropbox link writes to the folder I want, and the correct file name, but its doing something to the file format rather than just copying the file as is. Is there an easier way to just download a file to a specified location, or a way to correct this.

Code: Select all

  -- Download the file from the "sourceURL" address
print('[Downloading] ' .. sourceURL)
ok, err = req:perform()
if ok then
-- Write the file to disk
local file = io.open(fuDestFile, "w")
file:write(table.concat(body));
file:close();

-- Show the file we just downloaded in the default HTML viewer on your system:

AboutWindow()

--print('[Opening File] ' .. fuDestFile)
--bmd.openfileexternal('Open', fuDestFile)
end
end

function win.On.update.Clicked(ev)
download([[https://www.dropbox.com/s/ln6p9ga68enfji4/Test.fuse?dl=0]],"Fuses:/","Test.fuse")
end
When I open the new written file it like this:

<!DOCTYPE html>
<html class="" lang="en">
<head prefix="og: http://ogp.me/ns#">
<meta charset="utf-8">

I'm just not sure whats happening between it being read and written to file?

Or maybe this is the wrong approach as the download.lua example is written to preview to file in html format?

The original referenced .lua file here

-

Code: Select all

-- The URL for the cURL based download:
local sourceURL = [[https://www.steakunderwater.com/wesuckless/index.php]]

-- The filepath for saving the downloaded asset
local fuDestFile = comp:MapPath("Temp:/") .. "we-suck-less.html"

-- Set up cURL to work with Fusion 9.0.1
ffi = require "ffi"
curl = require "lj2curl"
ezreq = require "lj2curl.CRLEasyRequest"
local req = ezreq(sourceURL)
local body = {}
req:setOption(curl.CURLOPT_SSL_VERIFYPEER, 0)
req:setOption(curl.CURLOPT_WRITEFUNCTION, ffi.cast("curl_write_callback",
function(buffer, size, nitems, userdata)
table.insert(body, ffi.string(buffer, size*nitems))
return nitems
end))

-- Download the file from the "sourceURL" address
print('[Downloading] ' .. sourceURL)
ok, err = req:perform()
if ok then
-- Write the file to disk
local file = io.open(fuDestFile, "w")
file:write(table.concat(body));
file:close();

-- Show the file we just downloaded in the default HTML viewer on your system:
print('[Opening File] ' .. fuDestFile)
bmd.openfileexternal('Open', fuDestFile)
end


pixelstuff
Posts: 41
Joined: Sun Jul 15, 2018 11:50 am
Been thanked: 1 time

### Reading and Writing files with cURL

I found the solution to this, something I overlooked. That is Dropbox and other's don't give you a direct link to the file, so the file in held in htlm and other rubbish you don't need. It also index's the files rather than giving a direct path so thats another problem with DB.

If you search for Dropbox raw or Google drive raw you can find solutions to get direct raw links to the files( for Dropbox you change ?dl=0 to ?raw=1 )

So this way when everything is read and written it just uses what is in the raw file.

SecondMan
Site Admin
Posts: 3383
Joined: Thu Jul 31, 2014 5:31 pm
Answers: 2
Location: Vancouver, Canada
Been thanked: 70 times
Contact:

### Re: Downloading from Dropbox using cURL in Lua

@pixelstuff

Please try to make sure to post in a relevant topic, or if there isn't one, make your own?

Thanks!

pixelstuff
Posts: 41
Joined: Sun Jul 15, 2018 11:50 am
Been thanked: 1 time

### Re: Downloading from Dropbox using cURL in Lua

Sure I'll make a new topic in future so it can be found easier