Remote File Engine

Community Forums/Showcase/Remote File Engine

Andres(Posted 2006) [#1]
Use the commands as you would reading local files :) Except that RemoteFileSize%(file%) can be used only on opened files.

* 19.09.2006: Added Post request possibility
If POSTvariables$ has a value then the request will be automatically turned into post one and the variables will be added too Example: POSTvariables$ = "name=Andres&age=18"

* 20.09.2006: Added HTTP header changing
You can now add cookies and stuff to header request: HTTPheader$ = "Cookie: username=my_name; password=my_pass"
Also many lines of header is addable by using line break: chr(13) + chr(10)

* 29.09.2006: Multi protocol
Now supporting FTP protocol. Usage:
file% = OpenRemoteFile%("ftp://www.domain.com/file", 21, "username", "password")

* 12.10.2006: Better server detection and File info funcs
Now opening TCP streams to host too. Added Some file info functions: RemoteFileName$(file%), RemoteFileCode%(bank%), RemoteFileProtocol$(bank%). If the file name is sent in header then you can receive it with RemoteFileName$(file%) because the file name isn't always in URL. Improved EORF()

Any suggestion or criticism is welcome!

Function list:
file% = OpenRemoteFile%(url$[, port%, POSTvariables$, HTTPheader$])
CloseRemoteFile(file%)
EORF(file%)
RemoteFileSize%(file%) - if no size known then it's -1
RemoteFileName$(file%)
RemoteFileCode%(file%) - Server response code (HTTP only)
RemoteFileProtocol$(file%)
ReadRemoteLine$(file%)
ReadRemoteString$(file%)
ReadRemoteInt%(file%)
ReadRemoteShort%(file%)
ReadRemoteByte%(file%)
WriteRemoteBytes(file%, save%, offset%, count%)
RemoteReadAvail(file%)

Example:
Include "transporter.bb"

file% = OpenRemoteFile("http://battleempire.kylm.com/files/clip.avi")
save% = WriteFile("received.avi")

Graphics 400, 60, 32, 2
SetBuffer BackBuffer()

While Not KeyHit(1)
	Cls
	
	Text 10, 10, "Downloaded: " + total% + "/" + RemoteFileSize(file%) + "(" + (Int 100 * (Float total% / RemoteFileSize(file%))) + "%)"
	
	If Not EORF(file%)
		N% = WriteRemoteBytes(file%, save%, 0, RemoteReadAvail(file%))
		total% = total% + N%
	Else
		Text 10, 30, "FINISHED!"
	EndIf
	
	Flip
Wend

CloseFile save%
CloseRemoteFile(file%)


Engine to include:



Andres(Posted 2006) [#2]
An example how to load TGA files directly from internet:
Include "transporter.bb"

file% = OpenRemoteFile("http://www.oat.ee/andres/oval.tga")
If Not file% Then RuntimeError "Unable to open the file!"

Graphics 800, 600, 32, 2
SetBuffer BackBuffer()

; Read TGA header
IDLenght% = ReadRemoteByte(file%)
ColorMapType% = ReadRemoteByte(file%)
ImageType% = ReadRemoteByte(file%)
ColorMapIndex% = ReadRemoteShort(file%)
ColorMapEntries% = ReadRemoteShort(file%)
ColorMapSize% = ReadRemoteByte(file%)
Xhandle% = ReadRemoteShort(file%)
YHandle% = ReadRemoteShort(file%)
Width% = ReadRemoteShort(file%)
Height% = ReadRemoteShort(file%)
BPP% = ReadRemoteByte(file%)
Attributes% = ReadRemoteByte(file%)

ImageID$ = ""
For i = 1 To IDLenght
	ImageID$ = ImageID$ + Chr(ReadRemoteByte(file%))
Next

; Read TGA body
image% = CreateImage(Width%, Height%)
LockBuffer ImageBuffer(image%)
For y = 0 To Height% - 1
	For x = 0 To Width% - 1
		b% = ReadRemoteByte(file%)
		g% = ReadRemoteByte(file%)
		r% = ReadRemoteByte(file%)
		a% = ReadRemoteByte(file%)
		WritePixelFast x, height% - y - 1, (b% Or (g% Shl 8) Or (r% Shl 16) Or ($FF000000)), ImageBuffer(image%)
	Next
Next
UnlockBuffer ImageBuffer(image%)

CloseRemoteFile(file%)

; Display the received image
While Not KeyHit(1)
	Cls
	DrawImage image%, MouseX(), MouseY()
	Flip
Wend



Beaker(Posted 2006) [#3]
Looks useful. Thanks.


Andres(Posted 2006) [#4]
You can read blitzbasic.com's content as logged in by adding cookies:
Include "transporter.bb"

file% = OpenRemoteFile("http://www.blitzbasic.com/Account/_index_.php", 80, "", "Cookie: _i=user_id; _p=user_pass")
If Not file% Then RuntimeError "Unable to open the file!"

While Not KeyHit(1)
	If Not EORF(file%) Then Print ReadRemoteLine$(file%)
Wend

CloseRemoteFile(file%)


For example my user_id is 8421
user_pass is just string of your password (or URL_Encoded if needed)


jfk EO-11110(Posted 2006) [#5]
Really useful. Could be used to update/patch things.


Andres(Posted 2006) [#6]
Now supporting FTP protocol and debug mode :)


Andres(Posted 2006) [#7]
Now:
1. OpenRemoteFile("http://sub.domain.com") will firstly try to connect to sub.domain.com rather than www.domain.com
2. Reads file name and some other info from HTTP header


@rtur(Posted 2006) [#8]
Cool, thanks!