need some help with libcurl.mod

BlitzMax Modules Forums/Brucey's Modules/need some help with libcurl.mod

I've been trying to get some ftp uploading going with libcurl. But somehow I can't get it to work. Connecting to remote server works fine and I also manage to download a file from the server, thanks to the great examples. But when it comes to uploading I get stuck - no example to copy and paste from :-(
I read through the help file half a dozen times and know that I have to create a
readFunction(buffer:Byte Ptr, size:Int, data:Object)and then hook it up with
curl.setReadCallback(myReadFunction, myData).
But I can't figure out what needs to happen inside the readFunction()...

Thanx for any help!

Funnily enough, I've never actually tried uploading until now, so apologies for a lack of examples.

Anyhoo, here's one which I've tested on my server and it seemed to work :-)

I created a "helper" Type to look after the streaming, all in the name of good code re-use.
Not sure if we need to close the stream afterwards, or if it is closed when the variable is GC'd...

Here it is :
SuperStrict

Framework BaH.libcurl
Import BRL.StandardIO
Import BRL.FileSystem

Local curl:TCurlEasy = TCurlEasy.Create()

' Just here for keeping an eye on things
curl.setOptInt(CURLOPT_VERBOSE, 1)
curl.setDebugCallback(debugFunction)


' Create our upload-helper :-)
Local upload:UploadHelper = UploadHelper.Create("ftptest.bmx")


' ftp connect and initial directory
curl.setOptString(CURLOPT_URL, "<full FTP URL including filename to upload>")
curl.setOptString(CURLOPT_USERPWD, "user:password")
curl.setOptInt(CURLOPT_UPLOAD, True)

' set the upload size
curl.setOptLong(CURLOPT_INFILESIZE_LARGE, upload.size)

' set the callback
curl.setReadCallback(upload.Read, upload)

' do the UPLOAD !
Local res:Int = curl.perform()

If res Then
	Print "***** " + CurlError(res) + " *****"
End If

curl.cleanup()

End  ' Fini!

' for our debugging
Function debugFunction:Int(data:Object, msgType:Int, message:String)

	' we only care about server info
	If msgType = CURLINFO_HEADER_IN Then
		Print message
	End If

End Function

' This just processes a filestream for us.
' DoRead, does the hard work of feeding the callback buffer with data from the stream.
Type UploadHelper

	Field name:String
	Field size:Int
	Field currentPos:Int
	
	Field stream:TStream

	Function Create:UploadHelper(file:String)
		Local this:UploadHelper = New UploadHelper
		
		this.name = file
		this.size = FileSize(file)
		
		Return this
	End Function
	
	Method DoRead:Int(buffer:Byte Ptr, size:Int)
		If Not stream Then
			stream = ReadStream(name)
			currentPos = 0
		End If
		
		Local count:Int = Stream.Read( buffer, size )
		currentPos :+ count
		
		Return count
	End Method

	Function Read:Int(buffer:Byte Ptr, size:Int, data:Object)
		Return UploadHelper(data).DoRead(buffer, size)
	End Function

End Type


Thanx very much Brucey, it works like a charm! I did put a close stream method into UploadHelper just to be on the save side. I've uploaded a file of 10MB and it finished without problems. But I will try now to to build in some functionality which will resume the download if it crashes...

You should add the above sample to the libcurl docs.

I really appreciate the fantastic support!