2
« on: September 06, 2012, 02:25:36 PM »
Problem was that you were saving the response header into beginning of your image file. I solved this by saving the buffer once the header was done which clears the buffer and gets overwritten with the actual data.
Also, this code has no problem with chunked responses (HTTP 1.1), so changed that back.
Cheers!
package ServerPreview
{
function ConnectToServer(%a, %b, %c, %d)
{
echo("triggered");
%value = parent::ConnectToServer(%a, %b, %c, %d);
%ip = getSubStr(%a,0,strPos(%a,":"));
%port = getSubStr(%a,strPos(%a,":")+1,strLen(%a));
%address = strreplace(%ip,".","-") @ "_" @ %port;
new tcpObject(FileDownloader);
FileDownloader.downloadFile("image.blockland.us", "/detail.php?q=" @ %address, "base/client/ui/loadingBG.png");
echo("ended");
return %value;
}
};
activatePackage(ServerPreview);
function FileDownloader::DownloadFile(%this, %addr, %file, %saveto)
{
%this.addr = %addr;
%this.file = %file;
%this.saveTo = %saveto;
%this.connect(%addr @ ":80");
}
function FileDownloader::onConnected(%this)
{
%this.send("GET " @ %this.file @ " HTTP/1.1\r\nHost: "@ %this.addr @"\r\n\r\n");
}
function FileDownloader::onLine(%this, %line)
{
if(%line $= "" && %this.binSize)
{
%this.setBinarySize(%this.binSize);
// flush the header out of the buffer
%this.saveBufferToFile(%saveto);
}
if(getWord(%line,0) $= "Content-Length:")
%this.binSize = getWord(%line,1);
}
function FileDownloader::onBinChunk(%this)
{
cancel(%this.saveSched);
%this.saveSched = %this.schedule(1000, onSaveFinal, %this.saveto);
}
function FileDownloader::onSaveFinal(%this, %saveto)
{
%this.saveBufferToFile(%saveto);
echo("done loading");
schedule(500, 0, resetLoading);
}
function resetLoading()
{
canvas.popDialog(LoadingGui);
canvas.pushDialog(LoadingGui);
}